digitapeu / g2a

G2A Pay PHP 库,用于创建和发送支付。

维护者

详细信息

github.com/Digitapeu/g2a

源代码

v1.2.0 2020-01-14 20:01 UTC

This package is auto-updated.

Last update: 2024-09-15 06:50:30 UTC


README

G2APay

使用 G2APay 支付网关创建和发送支付。

安装

运行 composer require digitapeu/g2apay 通过 Composer 安装。

用法

注意:如果以下代码示例中的变量/参数让您感到困惑,您可以查看官方的 G2APay 文档

初始化

初始化 G2APay 库并设置所需参数

use G2APay\G2APay;

// Set required variables
$hash = 'your-api-hash'; // Get it from G2APay
$secret = 'your-api-secret'; // Get it from G2APay
$email = 'mail@example.com'; // 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/');

完成支付

创建支付并发送到 G2APay

$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);

可能有两种响应

// Success
[
    'success' => true,
    'url' => 'https://checkout.pay.g2a.com/index/gateway?token=abc123'
]

// Fail
[
    'success' => false,
    'message' => 'Generic error message.',
]

重定向到 G2APay 网站

接收到 $response 之后,您现在可以检查它是否成功,并将用户重定向到接收到的 G2APay 网站URL。

简单代码示例

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

回调

待办事项:当用户成功支付或取消时,G2APay 将将您重定向回之前提供的回调URL。

许可证

MIT