zkelo/unitpay-sdk

此软件包已被放弃,不再维护。没有建议的替代软件包。

Unitpay SDK

1.0.3-beta 2021-05-10 15:34 UTC

This package is auto-updated.

Last update: 2022-09-18 23:43:36 UTC


README

Packagist PHP Version Support CodeFactor Grade Packagist Downloads Packagist License

此 SDK 允许您在 PHP 中与 Unitpay 支付系统进行交互。

手册

要求

  • PHP 7.1 或更高版本;
  • Composer。

安装

composer require zkelo/unitpay-sdk

初始配置

SDK 构造函数需要传入至少两个参数 - 它是您的密钥和公钥。可选的第三个参数是用于交互的域名。

use zkelo\Unitpay\Unitpay;

$secretKey = 'Your secret key';
$publicKey = 'Your public key';

$sdk = new Unitpay($secretKey, $publicKey);

快速入门

创建支付表单链接

以下示例创建支付表单链接并立即将用户重定向到它。

// Order amount
$amount = 5;

// Order ID
$orderId = 161;

// Order description
$description = "Order $orderId (test)";

// Creating form link
$url = $sdk->form($amount, $orderId, $description);

// Redirecting user to form
header("Location: $url");

处理传入请求

以下示例使用 SDK 处理传入的 Unitpay 请求并返回相应的响应(即使请求有误)。

// Retrieving IP
$remoteIp = $_SERVER['REMOTE_ADDR'];

// Retrieving request data from `$_GET` array using `filter_input()` function
$requestData = filter_input_array(INPUT_GET);

// Handling request using SDK
$response = $sdk->handleRequest($remoteIp, $requestData, $success);

// Returning response
echo json_encode($response);

请注意! SDK 方法返回数组,您必须将其编码为 JSON,因此您需要使用 json_encode() 函数。

如果您需要在请求成功或失败时执行某些操作,可以使用传递给方法的第三个参数,该参数引用用于存储请求状态布尔值的变量。

如您在上面的示例中看到,我们将 $success 变量作为方法的第三个参数传递,因此我们可以轻松检查请求状态。

// ...

// Handling request using SDK
$response = $sdk->handleRequest($remoteIp, $requestData, $success);

if ($success) {
    // Do something on success request
} else {
    // Do something on bad request
}

// Returning response
echo json_encode($response);

通过 API 请求创建支付

有时您需要通过请求 API 而不是使用 Unitpay 表单来初始化支付。

// Order amount
$amount = 5;

// Order ID
$orderId = 161;

// Order description
$description = "Order $orderId (test)";

// User IP (can be either IPv4 or IPv6)
$ip = '127.0.0.1';

// Creating payment
$paymentId = $sdk->initPayment('card', $orderId, $amount, $description, $ip);

在上面的示例中,支付方式被直接写成 "raw"。但您可以使用模型中的常量来替代手动编写支付方式。有关更多信息,请参阅模型部分。

检索支付信息

要检索支付信息,您应使用 getPayment() 方法,该方法以舒适的方式使用模型返回信息。

// Payment ID in Unitpay (this is not order ID in your app or something else!)
$paymentId = 7777777777;

// Retrieving information
$paymentInfo = $sdk->getPayment($paymentId);

// Display order amount and currency
echo "Order amount: $paymentInfo->orderSum (currency: $paymentInfo->orderCurrency)", PHP_EOL;

本地化

  • 待办事项。

参考

此部分将很快编写。

异常

  • 待办事项。

接口

  • 待办事项。

可用方法

  • 待办事项。

区域设置

  • 待办事项。

模型

  • 待办事项。

扩展

本地化

如果您需要将货币名称、支付方式或响应消息翻译成您的语言,您可以通过创建一个新的继承自基本抽象 Locale 类的本地化类来实现。

以下示例展示了本地化类的样子。

namespace App\Locales;

/**
 * My own locale
 */
class MyLocale extends Locale implements LocaleInterface
{
    /**
     * {@inheritDoc}
     */
    public static function messages(): array
    {
        return [
            // Write translations here.
            //
            // For example, if you want to translate some currencies names,
            // you just need to specify all its messages inside `currency` property
            // which will be array which has currencies IDs as keys
            // and translation messages for each of them as values.
            'currency' => [
                // Don't use raw names of currencies,
                // payment methods and etc. in your
                // locale class like here:
                //
                'RUB' => 'Russian rouble', // <-- This is wrong!
                // Instead of using RAWs
                // you should use model
                // constants like here:
                //
                Currency::RUB => 'Russian rouble' // <-- This is right!
            ]
        ];
    }
}

现在,当您有了您的本地化类,您需要通过使用 Locale::use() 方法将其添加到 Locale 模型中,使其可用。

use zkelo\Unitpay\Models\Locale;

// In this example `en_GB` is name of your locale
Locale::use('en_GB', App\Locales\MyLocale::class);

添加本地化后,您可以在 SDK 中使用它。您可以将其设置为默认值,或在需要的地方指定它。

// Specifying locale as default for SDK
$sdk->setDefaultLocale('en_GB');

// Or... specifying locale in "real-time"
$sdk->form(10, 6, 'Test payment', zkelo\Unitpay\Models\Payment::METHOD_CARD, zkelo\Unitpay\Models\Currency::RUB, 'en_GB');