PHP code example of ctechpay / ctechpay-php

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

    

ctechpay / ctechpay-php example snippets


use CtechPay\CtechPay;

$ctechpay = CtechPay::client('YOUR_SERVICE_TOKEN');

$ctechpay = CtechPay::client('YOUR_SERVICE_TOKEN', [
    'base_url' => 'https://new-api.ctechpay.com',
    'timeout' => 30,
]);

$payment = $ctechpay->hostedPayments()->create([
    'amount' => 100,
    'customer_reference' => 'INV-1001',
    'customer_message' => 'Invoice payment',
    'customer_name' => 'Jane Doe',
    'customer_email' => '[email protected]',
    'redirectUrl' => 'https://example.com/payments/success',
    'cancelUrl' => 'https://example.com/payments/cancelled',
]);

header('Location: ' . $payment['data']['hosted_payment_url']);
exit;

$reference = $_GET['reference'];

$status = $ctechpay->cards()->status($reference);

$details = $ctechpay->airtel()->details($reference);

$payment = $ctechpay->airtel()->pay([
    'amount' => 100,
    'phone' => '0999123456',
    'customer_reference' => 'INV-1001',
    'customer_message' => 'Invoice payment',
]);

$transactionId = $payment['data']['transaction']['id'];

$status = $ctechpay->airtel()->status($transactionId);

$details = $ctechpay->airtel()->details($transactionId);

$reference = $ctechpay->airtel()->reference('AIRTEL_MONEY_ID');

$order = $ctechpay->cards()->createPaymentPage([
    'amount' => 100,
    'merchantAttributes' => true,
    'redirectUrl' => 'https://example.com/payments/success',
    'cancelUrl' => 'https://example.com/payments/cancelled',
    'customer_reference' => 'INV-1001',
    'customer_message' => 'Invoice payment',
]);

header('Location: ' . $order['payment_page_URL']);
exit;

$status = $ctechpay->cards()->status($order['order_reference']);

use CtechPay\CtechPay;

$ctechpay = CtechPay::client(config('services.ctechpay.token'));

$payment = $ctechpay->hostedPayments()->create([
    'amount' => 100,
    'customer_reference' => 'ORDER-1001',
    'redirectUrl' => route('payments.success'),
    'cancelUrl' => route('payments.cancelled'),
]);

return redirect($payment['data']['hosted_payment_url']);

'ctechpay' => [
    'token' => env('CTECHPAY_TOKEN'),
],

use CtechPay\Exceptions\CtechPayException;

try {
    $payment = $ctechpay->hostedPayments()->create([
        'amount' => 100,
    ]);
} catch (CtechPayException $e) {
    echo $e->getMessage();
    print_r($e->response);
}
bash
composer 
text
https://example.com/payments/success?reference=TRANSACTION_OR_ORDER_REFERENCE