Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
atomjoy committed Feb 8, 2023
1 parent c9d5c2a commit 7b4355f
Show file tree
Hide file tree
Showing 23 changed files with 777 additions and 772 deletions.
114 changes: 64 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Płatności PayU w Laravel. Jak utworzyć link do płatności za zamówienie w p

## Payu dokumentacja, sandbox

https://developers.payu.com/pl/overview.html#sandbox
<https://developers.payu.com/pl/overview.html#sandbox>

## Instalacja pakietu Laravela

Zainstaluj php composera ze strony https://getcomposer.org/download
Zainstaluj php composera ze strony <https://getcomposer.org/download>

```sh
composer require atomjoy/payu 2.0.*
composer require atomjoy/payu "^3.0.0"
composer update
composer dump-autoload -o
```
Expand All @@ -27,6 +27,13 @@ CREATE DATABASE IF NOT EXISTS laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unic
GRANT ALL PRIVILEGES ON laravel.* TO root@localhost IDENTIFIED BY 'toor' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON laravel.* TO root@127.0.0.1 IDENTIFIED BY 'toor' WITH GRANT OPTION;
FLUSH PRIVILEGES;

# Clear or change password
SET PASSWORD FOR root@localhost=PASSWORD('');

# Change password
ALTER USER 'testing'@'localhost' IDENTIFIED BY 'toor';
FLUSH PRIVILEGES;
```

### Konfiguracja .env
Expand All @@ -46,7 +53,9 @@ DB_PASSWORD=toor
php artisan make:model Order -a
```

### Dodaj kolumny w tabeli
### Migracja tabeli klasy Order

Dodaj kolumny w tabeli.

```php
<?php
Expand All @@ -56,28 +65,28 @@ use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->decimal('cost', 15, 2)->nullable()->default(0.00);
$table->enum('payment_method', ['money', 'card', 'online'])->nullable()->default('money');
$table->enum('payment_gateway', ['payu'])->nullable(true);
$table->string('firstname');
$table->string('lastname');
$table->string('phone');
$table->string('email');
$table->timestamps();
$table->softDeletes();
$table->unsignedBigInteger('user_id')->nullable(true);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}

public function down()
{
Schema::dropIfExists('orders');
}
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->decimal('cost', 15, 2)->nullable()->default(0.00);
$table->enum('payment_method', ['money', 'card', 'online', 'cashback'])->nullable()->default('money');
$table->enum('payment_gateway', ['payu'])->nullable(true);
$table->string('firstname');
$table->string('lastname');
$table->string('phone');
$table->string('email');
$table->timestamps();
$table->softDeletes();
$table->unsignedBigInteger('user_id')->nullable(true);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}

public function down()
{
Schema::dropIfExists('orders');
}
};
```

Expand Down Expand Up @@ -113,32 +122,32 @@ class Order extends Model implements PayuOrderInterface
}

// Wymagane metody poniżej
function order_id()
function orderId()
{
return $this->id;
}

function order_cost()
function orderCost()
{
return $this->cost;
}

function order_firstname()
function orderFirstname()
{
return $this->firstname;
}

function order_lastname()
function orderLastname()
{
return $this->lastname;
}

function order_phone()
function orderPhone()
{
return $this->phone;
}

function order_email()
function orderEmail()
{
return $this->email;
}
Expand All @@ -161,7 +170,7 @@ config/payu.php
php artisan vendor:publish --tag=payu-config
```

### Aktualizacja cache dir linux (if errors)
### Aktualizacja cache dir linux (gdy błędy)

```sh
sudo mkdir -p storage/framework/cache/payu
Expand All @@ -185,23 +194,25 @@ public/vendor/payu
php artisan vendor:publish --tag=payu-public --force
```

## Testuj
## Testy

### Dodaj w phpunit.xml

```xml
<testsuite name="Payu">
<directory suffix="Test.php">./vendor/atomjoy/payu/tests/Payu</directory>
<directory suffix="Test.php">./vendor/atomjoy/payu/tests/Payu</directory>
</testsuite>
```

### Tests only with config(['payu.env' => 'sandbox'])
### Tests tylko dla sandbox config(['payu.env' => 'sandbox'])

Utworzy link do płatności w bazie danych w tabeli payments (do przekierowania klienta sklepu).

```sh
php artisan test --testsuite=Payu --stop-on-failure
```

# Laravel PayU Api
## Laravel PayU Api

Wyłączyć w panelu administracyjnym PayU automatyczny odbiór płatności jeśli chcesz potwierdzać płatności ręcznie dla statusu WAITING_FOR_CONFIRMATION na COMPLETED lub CANCELED.

Expand All @@ -210,9 +221,10 @@ Wyłączyć w panelu administracyjnym PayU automatyczny odbiór płatności jeś
Numer zamówienia {orders.id} => 1, 2, 3, ...

```sh
# Utwórz zamowienie a następnie
# Utwórz zamowienie i link do płatności
https://{your.domain.here}/web/payment/create

# Utwórz link do płatności
# Lub utwórz link do płatności z id zamówienia
https://{your.domain.here}/web/payment/url/payu/{orders.id}

# Pobierz dane płatności
Expand All @@ -230,9 +242,9 @@ https://{your.domain.here}/web/payment/cancel/payu/{orders.id}

### Lista routes do obsługi płatności (sandbox)

atomjoy/payu/routes/admin.php
atomjoy/payu/routes/sandbox.php

# Przykłady Api w Php
## Przykłady Payu Api w Laravel

### Utwórz link płatności dla zamówienia (produkcja)

Expand All @@ -242,6 +254,7 @@ use App\Models\Order;
use Payu\Facades\Payu;

try {

// Create order here or get from db with id
$id = 'orders.id';

Expand All @@ -251,8 +264,12 @@ try {
// Redirect client to payment page
return redirect($url);

} catch (\Exception $e) {
return $e->getMessage();
} catch (QueryException | PDOException $e) {
report($e);
return response('Database Error.', 422);
} catch (Exception $e) {
report($e);
return response($e->getMessage(), 422);
}
```

Expand Down Expand Up @@ -382,29 +399,25 @@ try {
}
```

## Eventy Payu
## Zdarzenia Payu (events)

```php
<?php

use Payu\Events\PayuPaymentCreated;
use Payu\Events\PayuPaymentNotCreated;
use Payu\Events\PayuPaymentCanceled;
use Payu\Events\PayuPaymentConfirmed;
use Payu\Events\PayuPaymentRefunded;
use Payu\Events\PayuPaymentNotified;
```

## Listenery
## Przechwytywanie zdarzeń (listeners)

```sh
php artisan make:listener PaymentNotCreatedNotification --event=PayuPaymentNotCreated
php artisan make:listener PaymentCreatedNotification --event=PayuPaymentCreated
php artisan make:listener PaymentCanceledNotification --event=PayuPaymentCanceled
php artisan make:listener PaymentConfirmedNotification --event=PayuPaymentConfirmed
```

## Tworzenie klas modeli
## Tworzenie klas dla modeli

```sh
php artisan make:model Order -a
Expand All @@ -420,6 +433,7 @@ php artisan make:resource OrderCollection
use Illuminate\Support\Facades\Route;
use App\Models\Order;

// Przykład
Route::get('/orders', function () {
// Zamówienia z płatnościami
return Order::with('payment')->orderBy('created_at', 'desc')->get();
Expand Down
30 changes: 12 additions & 18 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
<?php

return [
// Enable payu payments
'enable' => true,

// Load payu routes
'routes' => true,

// Load payu db migrations
'migrations' => true,

// Enable payu logs
'logs' => [
'notify' => false,
'errors' => true,
],

// Payu api credentials

// Set environment: 'sandbox' or 'secure'
'env' => 'sandbox',

Expand All @@ -30,5 +13,16 @@
'client_secret' => '',

// Currency
'currency' => 'PLN'
'currency' => 'PLN',

// Settings

// Enable payu payments
'enable' => true,

// Load payu routes
'routes' => true,

// Load payu db migrations
'migrations' => true,
];
Empty file added database/factories/.gitkeep
Empty file.
21 changes: 0 additions & 21 deletions database/factories/PayuLogFactory.php

This file was deleted.

43 changes: 43 additions & 0 deletions database/migrations/9000_12_31_1_create_orders_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('orders')) {
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->enum('payment_method', ['money', 'card', 'online', 'cashback'])->nullable()->default('money');
$table->enum('payment_gateway', ['payu'])->nullable(true);
$table->decimal('cost', 15, 2)->nullable()->default(0.00);
$table->string('firstname');
$table->string('lastname');
$table->string('phone');
$table->string('email');
$table->timestamps();
$table->softDeletes();
$table->unsignedBigInteger('user_id')->nullable(true);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePaymentsTable extends Migration
return new class extends Migration
{
/**
* Run the migrations.
Expand All @@ -27,7 +27,7 @@ public function up()
$table->timestamps();
$table->softDeletes();

$table->unsignedBigInteger('order_id')->index();
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onUpdate('cascade')->onDelete('cascade');
});
}
Expand All @@ -41,4 +41,4 @@ public function down()
{
Schema::dropIfExists('payments');
}
}
};
Loading

0 comments on commit 7b4355f

Please sign in to comment.