PHP code example of midsonlajeanty / php-natcash-sdk
1. Go to this page and download the library: Download midsonlajeanty/php-natcash-sdk 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/ */
midsonlajeanty / php-natcash-sdk example snippets
use Mds\Natcash\Config;
use Mds\Natcash\Natcash;
use Mds\Natcash\PaymentRequest;
// Natcash Merchant Credentials
$config = new Config(PRIVATE_KEY, PARTNER_CODE, FUNCTION_CODE, USERNAME, PASSWORD, CALLBACK_URL);
// Payment Request
$payment = new PaymentRequest('ORDER-001', 10);
// Init SDK with config
$natcash = new Natcash($config, DEBUG);
// Make Payment with payment request
$response = $natcash->makePayment($payment);
// Get Payment URL (Natcash Checkout)
$response->getRedirect();
use Mds\Natcash\Config;
use Mds\Natcash\Natcash;
// Natcash Merchant Credentials
$config = new Config(PRIVATE_KEY, PARTNER_CODE, FUNCTION_CODE, USERNAME, PASSWORD, CALLBACK_URL);
// Init SDK with config
$natcash = new Natcash($config, DEBUG);
// Verify Webhook Payload Signature
$isValid = $natcash->verifyPayloadSignature('WEBHOOK_ORDER_NUMBER', 'WEBHOOK_CODE', 'WEBHOOK_SIGNATURE');
if ($isValid) {
print_r("Signature is valid." . PHP_EOL . PHP_EOL);
// Get Payment Details with OrderId provided by your app.
$details = $natcash->getTransactionDetailsByOrderId($orderNumber);
} else {
print_r("Signature is invalid." . PHP_EOL);
}
use Mds\Natcash\Laravel\Facades\Natcash;
use Mds\Natcash\PaymentRequest;
$response = Natcash::makePayment(new PaymentRequest('order-1', 250, null, 'msisdn'));
return redirect($response->getRedirect());
use Mds\Natcash\NatcashInterface;
final class CheckoutController
{
public function __construct(private NatcashInterface $natcash) {}
}
use Mds\Natcash\NatcashInterface;
final class CheckoutService
{
public function __construct(private NatcashInterface $gateway) {}
public function pay(PaymentRequest $request): PaymentResponse
{
return $this->gateway->makePayment($request);
}
}
// Production: inject the real gateway.
new CheckoutService(new Natcash($config));
// Test: mock the interface, no need to hit the network.
$gateway = Mockery::mock(NatcashInterface::class);
$gateway->shouldReceive('makePayment')->once()->andReturn($fakeResponse);
$service = new CheckoutService($gateway);