轻松将 Paygate Global Platform 集成到您的 Flutter 应用中

特点

  • 使用 Paygate Global 平台实现支付。
  • 支持两种支付提供商:T-Money 和 Moov Money。

定义术语

  • 交易标识符:您自己生成(或让此插件生成)的标识符,您可以使用它来识别交易并检查其状态。
  • 交易金额:您想从用户手机钱包账户中扣除的金额。
  • 交易回调 URL:交易完成后您希望被调用的 URL。
  • 交易提供商:提供商是(目前)MoovAfrica (MoovMoney) 和 Togocom (TMoney)。
  • 交易参考号:Paygate Global 本身生成的参考号。您也可以用它来检查交易状态。

入门

在使用此包之前,您需要拥有一个 Paygate API 密钥。请在 Paygate 网站上创建账户,填写您的电子商务平台信息,然后您将收到您的账户 API 密钥。

用法

首先,您需要初始化该插件。只需调用 Paygate.init() 方法。此方法需要您的生产 Paygate API 密钥。您还可以在调用 *pay* 方法时配置您想默认使用的 API 版本,而无需指定版本(如果未指定,则默认使用第二个方法)。

Paygate.init(
  apiKey: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
  apiVersion: PaygateVersion.v1, // default PaygateVersion.v2
  apiKeyDebug: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', // optional
  identifierLength: 20, // optional, default 20
);

您可以使用用于调试的附加 API 密钥进行初始化。您还可以指定插件生成的标识符的默认长度(默认为 20)。

初始化新交易

Paygate 有两个 API 版本。取决于您何时发起请求(post),或使用预配置的支付页面(get 请求)。

  1. 您发起一个 post 请求,Paygate 会返回交易参考号,您之后可以用它来检查交易状态。

NewTransactionResponse response = await Paygate.payV1(
    amount: 1000,
    provider: PaygateProvider.moovMoney, // required : PaygateProvider.moovMoney or PaygateProvider.tMoney
    identifier: 'TR000000010', // optional : if empty, the transaction identifier will be generated by the plugin.
    description: 'My awesome transaction', // optional : description of the transaction
    phoneNumber: '90010203', // required : phone number of the user
);
  1. 您发起一个 get 请求到 Paygate 提供和保护的支付页面。

NewTransactionResponse response = await Paygate.payV2(
    amount: 1000,
    callbackUrl: 'https://myapp.com/callback',
    provider: PaygateProvider.moovMoney, // required : PaygateProvider.moovMoney or PaygateProvider.tMoney
    identifier: 'TR000000010', // optional : if empty, the transaction identifier will be generated by the plugin.
    description: 'My awesome transaction', // optional : description of the transaction
    phoneNumber: '90010203', // required : phone number of the user
);

客户付款后,PayGateGlobal 将向电子商务返回 URL(如果之前提供)发送确认消息。消息是 HTTP POST 方法,带有 JSON 载荷,结构如下:

属性 描述
tx_reference PayGateGlobal 为交易生成的唯一标识符
identifier 电子商务交易的内部标识符。
payment_reference Flooz 生成的支付参考代码。
amount 客户支付的金额
datetime 付款日期和时间
phone_number 进行付款的客户的电话号码。
payment_method 客户使用的支付方式。可能的值:FLOOZ, T-Money。

通过方法 1 或 2,用户都不会退出应用程序。当您使用 API 版本 2 时,此包使用 custom_tabs 包打开 Paygate Global 支付页面。

检查交易状态

在初始化交易后,您可以通过 `verify` 方法(在 `NewTransactionResponse` 对象上调用)来检查交易状态。或者,您也可以直接使用静态方法 `verifyTransaction`,该方法需要交易参考号或标识符。

if (response.ok) {
    saveReference(response.txReference); // or save the identifier in your database saveIdentifier(response.identifier);
} else {
    // Retry or debug (a message will be displayed in the console).
    /// You can access the exception with response.exception .
}

// After a delay, you can check the transaction status.
Transaction transaction = await response.verify(); 

// or 

Transaction transaction = await Paygate.verifyTransaction(response.txReference); // or Paygate.verifyTransaction(response.identifier);

请查看示例目录,其中包含带合适 UI 实现的完整示例。

附加信息

我在 我的博客上写了一篇关于此包的文章,其中包含更详细的信息。

如果您使用此包,欢迎贡献代码或提交问题。

GitHub

查看 Github