PHP code example of frontlabs / g2apay

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

    

frontlabs / g2apay example snippets


use G2APay\G2APay;

// Set / Get it from G2APay
$secret = 'your-api-secret'; // Get it from G2APay
$email = '[email protected]'; // Your G2APay store email
$success = 'http://example.com/success/'; // URL for successful callback;
$fail = 'http://example.com/failed/'; // URL for failed callback;
$order = 2234; // Choose your order id or invoice number, can be anything

// Optional
$currency = 'USD'; // Pass currency, if no given will use "USD"

// Create payment instance
$payment = new G2APay($hash, $secret, $email, $success, $fail, $order, $currency);

// Set item parameters
$sku = 1; // Item number (In most cases $sku can be same as $id)
$name = 'My Game';
$quantity = 1; // Must be integer
$id = 1; // Your items' identifier
$price = 9.95; // Must be float
$url = 'http://example.com/my-game/';

// Optional
$extra = '';
$type = '';

// Add item to payment
$payment->addItem($sku, $name, $quantity, $id, $price, $url, $extra, $type);


$payment->addItem(114, 'Game', 1, 114, 9.95, 'http://example.com/i/game/')
        ->addItem(115, 'Gift', 2, 115, 1.50, 'http://example.com/i/gift/')
        ->addItem(116, 'Key', 1, 116, 9.50, 'http://example.com/i/key/');

$orderId = 1; // Generate or save in your database
$extras = []; // Optional extras passed to order (Please refer G2APay docs)
// Create payment against G2APay
$response = $payment->createOrder($orderId, $extras);

// Or if you want to create sandbox payment (for testing only)
$response = $payment->test()->createOrder($orderId, $extras);

// Check if successful
if ($response['success']) {
    header('Location: '.$response['url']); // redirect
} else {
    echo $response['message']; // print out error message
}