PHP code example of gcorpllc / paypey

1. Go to this page and download the library: Download gcorpllc/paypey 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/ */

    

gcorpllc / paypey example snippets


return [
    'default' => env('PAYPEY_DEFAULT_GATEWAY', 'zarinpal'),
    'currency' => env('PAYPEY_CURRENCY', 'toman'), // 'toman' or 'rial'
    'database_logging' => env('PAYPEY_DB_LOGGING', true),
    'sandbox' => env('PAYPEY_SANDBOX', true),

    'gateways' => [
        'zarinpal' => [
            'merchant_id' => env('ZARINPAL_MERCHANT_ID', ''),
            'sandbox' => env('ZARINPAL_SANDBOX', true),
            'currency' => 'toman',
            'mode' => 'normal', // 'normal' or 'zaringate'
        ],
        'mellat' => [
            'terminal_id' => env('MELLAT_TERMINAL_ID', ''),
            'username' => env('MELLAT_USERNAME', ''),
            'password' => env('MELLAT_PASSWORD', ''),
            'currency' => 'rial',
        ],
        // ... additional gateway configs
    ],
];

use Gcorpllc\Paypey\Facades\Paypey;

// Using default gateway or specific driver via via() / driver()
$response = Paypey::via('zarinpal')
    ->amount(10000) // Amount in global currency (Toman by default)
    ->callbackUrl(route('payment.callback'))
    ->description('Order #1024')
    ->with(['order_id' => 1024, 'user_id' => 42])
    ->request(); // purchase() is an identical alias

if ($response->isSuccessful()) {
    // Redirect user to gateway payment page
    return $response->redirect();
}

return back()->with('error', $response->getErrorMessage());

use Gcorpllc\Paypey\Facades\Paypey;
use Gcorpllc\Paypey\Exceptions\VerificationFailedException;

try {
    // Auto-detects driver from request query/POST params and database
    $receipt = Paypey::verify();

    $transactionId = $receipt->getTransactionId();
    $refId = $receipt->getRefId();
    $cardNumber = $receipt->getCardNumber();

    return view('payment.success', compact('receipt'));
} catch (VerificationFailedException $e) {
    return view('payment.failed', ['message' => $e->getMessage()]);
}

use Gcorpllc\Paypey\Facades\Paypey;

public function test_checkout_flow()
{
    // Default success fake for all drivers
    Paypey::fake();

    $response = Paypey::via('zarinpal')->amount(5000)->request();
    $this->assertTrue($response->isSuccessful());

    $receipt = Paypey::verify(['Authority' => $response->getAuthority()]);
    $this->assertTrue($receipt->isSuccessful());
}

public function test_failed_payment_scenario()
{
    Paypey::fake([
        'zarinpal' => Paypey::fakeFailed('Card balance insufficient'),
    ]);

    $this->expectException(\Gcorpllc\Paypey\Exceptions\VerificationFailedException::class);

    Paypey::via('zarinpal')->verify(['Authority' => 'INVALID_AUTH']);
}

use Gcorpllc\Paypey\Facades\Paypey;
use Gcorpllc\Paypey\Drivers\AbstractDriver;

Paypey::extend('my_custom_gateway', function ($app) {
    return new MyCustomDriver();
});
bash
php artisan vendor:publish --provider="Gcorpllc\Paypey\Providers\PaypeyServiceProvider"
bash
php artisan migrate