getloy/getloy-php

PHP 库,用于 GetLoy 支付集成 API。

1.0.3 2020-04-20 08:48 UTC

This package is auto-updated.

Last update: 2024-09-20 18:33:55 UTC


README

Latest Stable Version Scrutinizer Code Quality Total Downloads License

GetLoy 集成库提供了一种简单的方法,从使用 PHP 编写的应用程序中访问 GetLoy API。

该库目前支持以下支付方式

  • iPay88 柬埔寨(所有支持的支付方式)
  • Pi Pay
  • ABA 银行 PayWay(仅限借记/信用卡,仅限 ABA Pay,仅限借记/信用卡,或两者都支持)

要求

PHP 5.6.0 或更高版本。

Composer

您可以通过 Composer 安装绑定。运行以下命令

composer require getloy/getloy-php

要使用绑定,请使用 Composer 的 自动加载

require_once('vendor/autoload.php');

手动安装

如果您不想使用 Composer,您可以下载 最新版本。然后,为了使用绑定,包括 init.php 文件。

require_once('/path/to/getloy-php/init.php');

入门指南

简单使用示例

$gateway = new \Getloy\Gateway('YOUR GETLOY TOKEN');
$gateway->registerPaymentProvider(
    \Getloy\PaymentProviders::PIPAY_KH,
    [
        'testMode' => true,
        'merchantId' => 'YOUR PIPAY MERCHANT ID',
        'storeId' => 'YOUR PIPAY STORE ID',
        'deviceId' => 'YOUR PIPAY DEVICE ID',
    ]
);

$order = new \Getloy\TransactionDetails\OrderDetails(99.99, 'USD');
$payee = new \Getloy\TransactionDetails\PayeeDetails('John', 'Doe', 'j.doe@test.com', '012345678');

echo $gateway->widgetHtml(
    'ORDER-123',
    \Getloy\PaymentProviders::PIPAY_KH,
    $order,
    $payee,
    'https://mysite.com/payment-callback.php',
    'https://mysite.com/pages/payment-complete/'
    'https://mysite.com/pages/payment-failed/'
);

传递额外的订单详情

大多数支付系统将在支付界面中显示额外的订单详情,例如订单行项目。这些信息可以在 widgetHtml() 调用中提供。

$orderItems = new \Getloy\TransactionDetails\OrderItems();

$orderItems->add(
    new \Getloy\TransactionDetails\OrderItem('Item 1', 2, 19.98, 9.99)
);
$orderItems->add(
    new \Getloy\TransactionDetails\OrderItem('Item 2', 1, 12.50, 12.50)
);

$order = new \Getloy\TransactionDetails\OrderDetails(32.48, 'USD', null, $orderItems);

回调验证

一旦支付确认,但在用户被重定向到成功 URL 之前,GetLoy 将向在 widgetHtml() 调用中提供的回调 URL 发送 POST 请求。

回调内容将根据包含(秘密)GetLoy Token 的哈希值进行验证。

以下示例中,$orderlookupOrder() 是您的应用程序提供的对象/方法的占位符。

$gateway = new \Getloy\Gateway('YOUR GETLOY TOKEN');

if ('POST' !== $_SERVER['REQUEST_METHOD']) {
    // unsupported request type
    return;
}

try {
    $callbackDetails = $gateway->readCallbackBodyAndParse();
} catch (Exception $exception) {
    // log invalid callback
    return;
}

if (Getloy\CallbackDetails::STATUS_SUCCESS !== $callbackDetails->status()) {
    // log invalid transaction status
    return;
}

// look up the order details using the transaction ID
$order = lookupOrder($callbackDetails->transactionId());

if (
    abs($order->amountPaid - $callbackDetails->amountPaid()) > 0.01
    || $order->currency !== $callbackDetails->currency()
) {
    // log transaction amount/currency mismatch
    return;
}

// update the order status to paid
$order->updateStatus('paid');

如果您在本地开发应用程序,您可以使用类似 ngrok 的服务将 GetLoy 的回调重定向到您的本地 Web 服务器。