PHP code example of moneybag / moneybag-sdk-php

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

    

moneybag / moneybag-sdk-php example snippets


use Moneybag\MoneybagClient;

// Using default base URL (staging)
$client = new MoneybagClient('your_merchant_api_key');

// Or with custom base URL (e.g., from environment variable)
$client = new MoneybagClient('your_merchant_api_key', [
    'base_url' => $_ENV['MONEYBAG_API_URL'] ?? 'https://staging.api.moneybag.com.bd/api/v2'
]);

use Moneybag\Models\CheckoutRequest;

$checkoutData = [
    'order_id' => 'order123',
    'currency' => 'BDT',
    'order_amount' => '1280.00',
    'order_description' => 'Online purchase',
    'success_url' => 'https://yourdomain.com/payment/success',
    'cancel_url' => 'https://yourdomain.com/payment/cancel',
    'fail_url' => 'https://yourdomain.com/payment/fail',
    'customer' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'address' => '123 Main Street',
        'city' => 'Dhaka',
        'postcode' => '1000',
        'country' => 'Bangladesh',
        'phone' => '+8801700000000'
    ]
];

try {
    $request = new CheckoutRequest($checkoutData);
    $response = $client->createCheckout($request);
    
    // Redirect customer to checkout URL
    header('Location: ' . $response->getCheckoutUrl());
} catch (MoneybagException $e) {
    // Handle error
    echo 'Error: ' . $e->getMessage();
}

$transactionId = $_GET['transaction_id']; // Get from callback

try {
    $response = $client->verifyPayment($transactionId);
    
    if ($response->isSuccessful()) {
        // Payment successful
        echo 'Payment completed for order: ' . $response->getOrderId();
    } else {
        // Payment failed
        echo 'Payment failed with status: ' . $response->getStatus();
    }
} catch (MoneybagException $e) {
    // Handle error
    echo 'Error: ' . $e->getMessage();
}

$client = new MoneybagClient('your_api_key', [
    'base_url' => 'https://api.moneybag.com.bd/api/v2',  // API base URL
    'timeout' => 30,                                      // Request timeout in seconds
    'verify_ssl' => true,                                 // SSL certificate verification
]);

// Or set base URL after initialization
$client->setBaseUrl('https://api.moneybag.com.bd/api/v2');

$checkoutData['order_items'] = [
    [
        'sku' => 'PROD001',
        'product_name' => 'iPhone 15',
        'product_category' => 'Electronic',
        'quantity' => 1,
        'unit_price' => '1200.00',
        'vat' => '120.00',
        'net_amount' => '1320.00'
    ]
];

$checkoutData['shipping'] = [
    'name' => 'John Doe',
    'address' => '123 Main Street',
    'city' => 'Dhaka',
    'postcode' => '1000',
    'country' => 'Bangladesh'
];

$checkoutData['payment_info'] = [
    'is_recurring' => false,
    'installments' => 0,
    'allowed_payment_methods' => ['card', 'mobile_banking'],
    '

use Moneybag\Exceptions\ValidationException;
use Moneybag\Exceptions\ApiException;
use Moneybag\Exceptions\MoneybagException;

try {
    // SDK operations
} catch (ValidationException $e) {
    // Handle validation errors
} catch (ApiException $e) {
    // Handle API errors
} catch (MoneybagException $e) {
    // Handle general SDK errors
}