PHP code example of atomjoy / payu

1. Go to this page and download the library: Download atomjoy/payu library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

atomjoy / payu example snippets


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=toor


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
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', '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');
  }
};



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Payu\Interfaces\PayuOrderInterface;
use Payu\Models\Payment;

class Order extends Model implements PayuOrderInterface
{
  use HasFactory, SoftDeletes;

  protected $guarded = [];

  public function payments()
  {
    return $this->hasMany(Payment::class)->withTrashed();
  }

  public function paid_payment()
  {
    return $this->hasOne(Payment::class)->where('status', 'COMPLETED')->withTrashed()->latest();
  }

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

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

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

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

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

  function orderEmail()
  {
    return $this->email;
  }
}


use App\Models\Order;
use Payu\Facades\Payu;

try {

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

  // Create payment url
  $url = Payu::pay(Order::findOrFail($id));

  // Redirect client to payment page
  return redirect($url);

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


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $status = Payu::confirm(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $status = Payu::cancel(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $status = Payu::refresh(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $payment = Payu::retrive(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $transaction = Payu::transaction(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $status = Payu::refund(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use App\Models\Order;
use Payu\Facades\Payu;

try {
  $id = 'orders.id';
  $status = Payu::refunds(Order::findOrFail($id));

} catch (\Exception $e) {
  return $e->getMessage();
}


use Payu\Facades\Payu;

try {
  $pay_methods = Payu::payments('pl');

} catch (\Exception $e) {
  return $e->getMessage();
}



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



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();

  // Zamówienia z płatnościami i danymi klienta
  return Order::with('payment','client')->orderBy('created_at', 'desc')->get();

  // Filtruj kolumny
  return Order::with(['payment' => function($query) {
    $query->select('id','id','total','status','status_refund','created_at')->orderBy('created_at', 'desc');
  }])->orderBy('created_at', 'desc')->get();
});
sh
# Aktualizuj tabelki
php artisan migrate
php artisan migrate --env=testing
sh
php artisan vendor:publish --tag=payu-config
sh
php artisan vendor:publish --tag=payu-pages
sh
php artisan vendor:publish --tag=payu-public --force
xml
<testsuite name="Payu">
  <directory suffix="Test.php">./vendor/atomjoy/payu/tests/Payu</directory>
</testsuite>
sh
php artisan test --testsuite=Payu --stop-on-failure
sh
php artisan make:listener PaymentCreatedNotification --event=PayuPaymentCreated
sh
php artisan make:model Order -a
php artisan make:resource OrderResource
php artisan make:resource OrderCollection