PHP code example of rainwaves / paygate-payment

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

    

rainwaves / paygate-payment example snippets


composer 
 

// Create an instance of PayWebClient with your PayGate credentials
$client = new PayWeb('10011072130', 'secret');
$transactionDate = Carbon::now()->format('Y-m-d H:i:s');

// Initialize payment
$inputData = [
    'reference' => 'order_123',
    'amount' => 1599.00,
    'currency' => 'ZAR',
    'returnUrl' => 'https://example.com/return',
    'notifyUrl' => 'https://example.com/notify',
    'transactionDate'   => $transactionDate,
    'locale' => 'en-za',
    'country' => 'ZAF',
    'email' => '[email protected]',
];
$response = $client->initiatePayment($inputData);

// Generate the payment form
$formHtml = $client->createForm();

// Display the payment form to the user
echo $formHtml;


class PaymentController extends Controller
{
    public Collection $products;
    protected PayWebInterface $payWeb;
    protected PaySubsInterface $paySubs;

    public function __construct(PayWebInterface $payWeb, PaySubsInterface $paySubs)
    {
        $this->products = $this->setProducts();
        $this->payWeb = $payWeb;
        $this->paySubs = $paySubs;

    }

    public function products()
    {
        $products = $this->products->toArray();
        return view('welcome', compact('products'));
    }
    public function response(Request $request)
    {
        $response = new PayGateNotifyResponse($request->toArray());
        Log::debug(json_encode("========================Return response================================"));
        Log::debug(json_encode($request->all()));
    }

    public function notify(Request $request)
    {
        $response = new PayGateNotifyResponse($request->toArray());
        Log::debug(json_encode("========================Notify response================================"));
        Log::debug(json_encode($request->all()));
    }

    public function payment($id)
    {
        $transactionDate = Carbon::now()->format('Y-m-d H:i:s');

        $product = $this->products->first(function ($product) use ($id) {
            return $product['id'] === (int)$id;
        });
        $inputData = [
            'reference' => uniqid('pgtest_'),
            'amount' => $product['price'],
            'currency' => 'ZAR',
            'returnUrl' => config('paygate.return_url'),
            'notifyUrl' => config('paygate.notification_url'),
            'transactionDate'   => $transactionDate,
            'locale' => 'en-za',
            'country' => 'ZAF',
            'email' => '[email protected]',
        ];

        $this->payWeb->initiatePayment($inputData);
        $formHtml = $this->payWeb->createForm();

        return view('payment', compact('product', 'formHtml'));
    }

    public function subscription()
    {
        $transactionDate = Carbon::now()->format('Y-m-d H:i:s');
        $startDate = Carbon::now()->addMonth()->startOfMonth()->format('Y-m-d');
        $endDate = Carbon::now()->addYear()->endOfMonth()->format('Y-m-d');
        $data = array(
            'version'            => 21,
            'reference'          => 'pgtest_123456789',
            'amount'             => 20000.00,
            'currency'           => 'ZAR',
            'returnUrl'         => 'https://fec0-41-216-202-254.ngrok-free.app/sub-response',
            'transactionDate'   => $transactionDate,
            'subsStartDate'    => $startDate,
            'subsEndDate'      => $endDate,
            'subsFrequency'     => 228,
            'processNow'        => 'YES',
            'processNowAmount' => 32.99,
        );

        $htmlForm = $this->paySubs->createSubscriptionAndChain($data)->createForm();

        return view('form', compact('htmlForm'));
    }

    public function subResponse(Request $request)
    {
        Log::debug(json_encode("========================Subscription response================================"));
        Log::debug(json_encode($request->all()));
    }
    private function setProducts(): Collection
    {
        return collect([
             [
                'id'=> 1,
                'name' => 'Product 1',
                'description' => 'Description of Product 1',
                'price' => 321.99,
            ],
            [
                'id'=> 2,
                'name' => 'Product 2',
                'description' => 'Description of Product 2',
                'price' => 245.99,
            ],
            [
                'id'=> 3,
                'name' => 'Product 3',
                'description' => 'Description of Product 3',
                'price' => 159.99,
            ],
        ]);
    }
}