9951 explained code solutions for 126 technologies


php-omnipayHow can I use PHP Omnipay to process a billing transaction?


PHP Omnipay is a library for processing payment transactions. It supports a wide range of popular payment gateways and provides an easy way to integrate with them. To process a billing transaction with PHP Omnipay:

  1. Install the library using Composer:
composer require omnipay/omnipay
  1. Initialize the gateway object with your credentials:
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername('your_username');
$gateway->setPassword('your_password');
$gateway->setSignature('your_signature');
  1. Set the transaction parameters:
$params = [
    'amount' => 10.00,
    'currency' => 'USD',
    'returnUrl' => 'https://www.example.com/return',
    'cancelUrl' => 'https://www.example.com/cancel',
];
  1. Send the transaction request:
$response = $gateway->purchase($params)->send();
  1. Check the response:
if ($response->isSuccessful()) {
    // Payment was successful
} elseif ($response->isRedirect()) {
    // Redirect to offsite payment gateway
    $response->redirect();
} else {
    // Payment failed
    echo $response->getMessage();
}
  1. Complete the transaction:
$purchaseResponse = $gateway->completePurchase($params)->send();

if ($purchaseResponse->isSuccessful()) {
    // Payment was successful
} else {
    // Payment failed
    echo $purchaseResponse->getMessage();
}
  1. Finally, check the transaction status:
$statusResponse = $gateway->fetchTransaction($params)->send();

if ($statusResponse->isSuccessful()) {
    // Payment status is successful
} else {
    // Payment status failed
    echo $statusResponse->getMessage();
}

For more information, please refer to the PHP Omnipay documentation.

Edit this code on GitHub