frontlabs / g2apay
G2APay PHP 库,用于创建和发送付款。
v1.1.4
2019-06-13 12:33 UTC
Requires
- php: >=7.0.0
This package is not auto-updated.
Last update: 2024-09-28 18:53:22 UTC
README
使用 G2APay 支付网关创建和发送付款。
安装
运行 composer require frontlabs/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