1. Go to this page and download the library: Download onpayio/php-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/ */
onpayio / php-sdk example snippets
final class FileTokenStorage implements \OnPay\TokenStorageInterface
{
public function __construct(private string $file) {}
public function getToken(): ?string
{
$token = is_file($this->file) ? file_get_contents($this->file) : false;
return $token === false ? null : $token;
}
public function saveToken(string $token): void
{
file_put_contents($this->file, $token);
}
}
final class SessionAuthStateStorage implements \OnPay\AuthStateStorageInterface
{
public function saveState(string $state): void { $_SESSION['onpay_state'] = $state; }
public function getState(): ?string { return $_SESSION['onpay_state'] ?? null; }
public function saveCodeVerifier(string $codeVerifier): void { $_SESSION['onpay_verifier'] = $codeVerifier; }
public function getCodeVerifier(): ?string { return $_SESSION['onpay_verifier'] ?? null; }
public function clear(): void { unset($_SESSION['onpay_state'], $_SESSION['onpay_verifier']); }
}
session_start();
$api = new \OnPay\OnPayAPI(
new FileTokenStorage(__DIR__ . '/../var/onpay-token.json'),
[
'client_id' => 'example.com', // the domain your integration runs on
'redirect_uri' => 'https://example.com/onpay/callback', // where OnPay sends the merchant back
'gateway_id' => 'YourGatewayId',
'platform' => 'my-shop-plugin/1.2.3', // optional: identifies your integration to OnPay
],
new SessionAuthStateStorage(),
);
// Step 1: send the merchant to OnPay to approve the integration.
header('Location: ' . $api->authorize());
// Step 2: on the redirect_uri, exchange the returned code for a token.
$api->finishAuthorize($_GET['code'], $_GET['state']);
$api = new \OnPay\OnPayAPI(new \OnPay\StaticToken('YourStaticToken'), [
'client_id' => 'example.com',
]);
use OnPay\API\Enum\PaymentMethod;
use OnPay\API\PaymentWindow;
use OnPay\API\PaymentWindow\PaymentInfo;
$window = new PaymentWindow();
$window->setCurrency('DKK');
$window->setAmount('12345');
$window->setReference('order-1001'); // unique per payment, e.g. the order number
$window->setWebsite('https://example.com');
$window->setAcceptUrl('https://example.com/onpay/accept');
$window->setDeclineUrl('https://example.com/onpay/decline');
$window->setCallbackUrl('https://example.com/onpay/callback');
$window->setMethod(PaymentMethod::CARD);
$window->setLanguage('en');
$window->setTestModeEnabled(true);
// Required when you have it (SCA and card-scheme rules): what you already know about the
// shopper, for 3-D Secure. Do not collect extra data just for this.
$info = new PaymentInfo();
$info->setName('Jane Doe');
$info->setEmail('[email protected]');
$info->setBillingAddressCountry('208');
$window->setInfo($info);
$payment = $api->payment()->createNewPayment($window);
header('Location: ' . $payment->getPaymentWindowLink());
$window = new \OnPay\API\PaymentWindow();
$window->setSecret('YourWindowSecret');
if (!$window->validatePayment($_GET)) {
http_response_code(400);
exit;
}
$transactions = $api->transaction();
$page = $transactions->getTransactions(page: 1, pageSize: 50, status: 'active');
foreach ($page->transactions as $transaction) {
echo $transaction->transactionNumber, ' ', $transaction->status, PHP_EOL;
}
$page->pagination->totalPages;
$transaction = $transactions->getTransaction('00000000-0000-0000-0000-000000000000');
// Capture everything that was authorised.
$transactions->captureTransaction($transaction->uuid);
// Partial capture or refund: state the total you want charged or refunded after the call,
// not the amount to add. Repeating the call is then harmless, and an earlier partial
// capture is accounted for.
$transactions->captureTransaction($transaction->uuid, postActionChargeAmount: 5000);
$transactions->refundTransaction($transaction->uuid, postActionRefundAmount: 2500);
$transactions->cancelTransaction($transaction->uuid);
$subscriptions = $api->subscription();
$page = $subscriptions->getSubscriptions(status: 'active');
$subscription = $subscriptions->getSubscription('00000000-0000-0000-0000-000000000000');
// MIT: charge the stored card; returns the new transaction.
$transaction = $subscriptions->createTransactionFromSubscription($subscription->uuid, 9900, 'order-1002');
$subscriptions->cancelSubscription($subscription->uuid);
$api->gateway()->getInformation(); // gateway id
$api->gateway()->getPaymentWindowDesigns(); // designs available to setDesign()
$api->gateway()->getPaymentWindowIntegrationSettings(); // payment window secret
try {
$api->transaction()->captureTransaction($uuid);
} catch (\OnPay\API\Exception\TokenException $e) {
// send the merchant through authorize() again
} catch (\OnPay\API\Exception\OnPayException $e) {
$request = $api->getLastHttpRequest(); // what was sent
$response = $api->getLastHttpResponse(); // what came back
}
$api = new \OnPay\OnPayAPI($tokenStorage, $options, $authStateStorage, logger: $logger);