PHP code example of hansajith18 / laravel-paycorp

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

    

hansajith18 / laravel-paycorp example snippets


use Hansajith18\LaravelPaycorp\Enums\PaymentPurposeEnum;
use Hansajith18\LaravelPaycorp\Services\PaycenterPaymentService;

$service = app(PaycenterPaymentService::class);

$payment = $service->initializePayment(
    userId:      auth()->id(),
    amountCents: 50000,                      // 500.00 LKR
    purpose:     PaymentPurposeEnum::PURCHASE,
    currency:    'LKR',
    payableType: \App\Models\Order::class,   // optional: link to a model
    payableId:   $order->id,
);

// Redirect the user to the Paycorp-hosted payment page
return redirect($payment->payment_page_url);

use Hansajith18\LaravelPaycorp\Services\PaycenterPaymentService;

$service = app(PaycenterPaymentService::class);

// reqid comes from the Paycorp redirect query string
$payment = $service->completePayment($request->query('reqid'));

if ($payment->isCompleted()) {
    // payment approved — fulfil the order
} else {
    // payment failed or declined
    // $payment->gateway_response_code, $payment->gateway_response_text
}

use Hansajith18\LaravelPaycorp\Enums\PaymentPurposeEnum;
use Hansajith18\LaravelPaycorp\Services\PaycenterPaymentService;

$service = app(PaycenterPaymentService::class);

$payment = $service->initializePayment(
    userId:      auth()->id(),
    amountCents: 100,                               // small verification amount
    purpose:     PaymentPurposeEnum::CARD_REGISTRATION,
    currency:    'LKR',
    tokenize:    true,                              // instruct Paycorp to tokenize the card
);

return redirect($payment->payment_page_url);

$payment = $service->completePayment($request->query('reqid'));

// After completePayment:
// ✓ Card token saved to saved_payments table
// ✓ VoidTokenizationCharge job dispatched automatically (reverses the charge)
// ✓ PaymentSucceeded event fired

use Hansajith18\LaravelPaycorp\Enums\PaymentPurposeEnum;
use Hansajith18\LaravelPaycorp\Models\SavedPayment;
use Hansajith18\LaravelPaycorp\Services\PaycenterPaymentService;

$service      = app(PaycenterPaymentService::class);
$savedPayment = SavedPayment::where('user_id', auth()->id())->firstOrFail();

$payment = $service->chargeWithSavedPayment(
    userId:       auth()->id(),
    amountCents:  50000,                       // 500.00 LKR
    purpose:      PaymentPurposeEnum::PURCHASE,
    savedPayment: $savedPayment,
    currency:     'LKR',
    payableType:  \App\Models\Order::class,    // optional
    payableId:    $order->id,
);

if ($payment->isCompleted()) {
    // charge approved — fulfil the order immediately (no callback needed)
} else {
    // charge failed or declined
}

use Hansajith18\LaravelPaycorp\PaycorpClient;

$client = app(PaycorpClient::class);

$response = $client->refund(
    originalTxnReference: $payment->gateway_txn_reference,
    amountCents:          50000,
    clientRef:            'REFUND-' . $refundId,
    currency:             'LKR',
    comment:              'Customer request',
);

if ($response->isApproved()) {
    // refund approved
}

use Hansajith18\LaravelPaycorp\PaycorpClient;

$client = app(PaycorpClient::class);
// or: $client = app('paycorp');

// Hosted-page init (returns only the payment page URL, no DB record)
$initResponse = $client->initPayment(
    amountCents: 50000,
    clientRef:   'ORDER-' . $orderId,
    returnUrl:   route('payments.callback'),
    currency:    'LKR',
);
return redirect($initResponse->paymentPageUrl);

// Complete (returns only the gateway response, no DB update)
$completeResponse = $client->completePayment($reqId, 'LKR');

// Real-time charge against a token
$rtResponse = $client->realTimePayment(
    cardToken:   $savedPayment->token,
    cardExpiry:  $savedPayment->card_expiry,
    amountCents: 50000,
    clientRef:   'PAY-' . $orderId,
    currency:    'LKR',
);

use Hansajith18\LaravelPaycorp\Events\PaymentSucceeded;
use Hansajith18\LaravelPaycorp\Events\PaymentFailed;

Event::listen(PaymentSucceeded::class, function ($event) {
    $payment = $event->payment;
    // fulfil order, send receipt, etc.
    // $payment->gateway_txn_reference, $payment->card_last4, etc.
});

Event::listen(PaymentFailed::class, function ($event) {
    $payment = $event->payment;
    // notify user, release reservation, etc.
    // $payment->gateway_response_code, $payment->gateway_response_text
});

// app/Enums/PaymentPurpose.php
namespace App\Enums;

use Hansajith18\LaravelPaycorp\Contracts\PaymentPurposeContract;

enum PaymentPurpose: string implements PaymentPurposeContract
{
    case ORDER_PAYMENT  = 'order_payment';
    case TOP_UP         = 'top_up';
    case CARD_SAVE      = 'card_save';      // your card-registration purpose

    public function label(): string
    {
        return match ($this) {
            self::ORDER_PAYMENT => 'Order Payment',
            self::TOP_UP        => 'Wallet Top-up',
            self::CARD_SAVE     => 'Card Registration',
        };
    }
}

'purpose_class' => \App\Enums\PaymentPurpose::class,

// Tell the package which value represents card registration so it knows
// when to dispatch the auto-void job after tokenisation completes:
'card_registration_purpose' => 'card_save',

$payment = $service->initializePayment(
    userId:      auth()->id(),
    amountCents: 50000,
    purpose:     PaymentPurposeEnum::PURCHASE,
    payableType: \App\Models\Order::class,   // must be a real, existing class
    payableId:   $order->id,
);

// Which model class base-names to update
'payable_status_classes' => ['Order', 'Booking'],

// Which column to write 'paid' / 'failed' into (default: payment_status)
'payable_status_column'  => 'payment_status',
bash
php artisan paycorp:publish
bash
php artisan migrate