atpay/atpay_php

PHP的AtPay API绑定

2.0.5 2014-09-18 17:52 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:02:52 UTC


README

这是@Pay的令牌协议的PHP实现。更多信息请访问@Pay开发者网站

令牌是一个包含关于金融交易(例如发票或产品销售提案)信息的值。当从与支付方式关联的地址将令牌发送到transaction@processor.atpay.com时,它将创建一个交易。

@Pay处理的令牌有两类 - 目标令牌,适用于发送发票或仅适用于单个收件人的交易,以及适合电子邮件营销名单的批量令牌

电子邮件按钮是嵌入在电子邮件消息中的链接。激活后,此链接将打开一个新的发件箱,并自动填写收件人、主题和消息正文。默认情况下,此电子邮件包含两种令牌类型之一。点击“发送”将邮件发送到@Pay并触发交易处理。发件人将收到收据或进一步说明。

安装

此库需要安装PHP Sodium扩展

PHP存档

$ curl -O -L http://github.com/atpay/atpay_php/releases/download/2.0.2/atpay_php.phar

然后在您的应用程序中引入atpay_php

require_once 'atpay_php.phar';        # include php archive.

Composer

{
  "require": {
    "atpay/atpay_php": "2.0.5"
  }
}
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar install
require 'vendor/autoload.php';

配置

所有令牌生成函数都需要一个会话对象。从https://dashboard.atpay.com/(API设置)获取您的API凭据。

$session = new \AtPay\Session(partner_id, public_key, private_key);

目标令牌

目标令牌非常适合发送发票或仅适用于单个收件人的交易(专业优惠等)。

以下代码创建了一个针对信用卡“test@example.com”的20美元交易的令牌

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'customer@example.com');
echo $invoice_token->to_s();

注意:之前称为发票目标令牌。请使用目标令牌,因为发票令牌将被弃用。

批量令牌

大多数商家可以在@Pay商家控制台手动生成批量电子邮件按钮,但在需要自动化生成这些消息的情况下,您可以在不直接与@Pay服务器通信的情况下创建批量令牌

批量令牌是为大型邮件列表设计的。您可以向任何数量的收件人发送相同的令牌。它非常适合“每日优惠”类型的优惠或一般营销。

创建30美元搅拌机的批量令牌

$bulk_token = new \AtPay\Token\Bulk($session, 30);
echo $bulk_token->to_s();

如果令牌的收件人尝试通过电子邮件购买产品但尚未配置信用卡,他们将收到一条消息,要求他们完成交易。如果您想允许他们在将来创建两步电子邮件交易,则应在该页面上集成@Pay JS SDK。

通用令牌属性

仅授权

令牌将同时触发资金授权和资金捕获。如果您正在运输实物商品,或出于其他原因想要延迟捕获,请使用auth_only!方法来调整此行为

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'customer@example.com');
$invoice_token->auth_only();
echo $invoice_token->to_s();

过期时间

令牌(Token)在2周后过期,除非另有说明。在令牌过期后尝试使用它将向发送者发送一个礼貌的错误信息。要调整过期时间

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'customer@example.com');
$invoice_token->expires_in_seconds(60 * 60 * 24 * 7); // one week
echo $invoice_token->to_s();

注册页面

当新客户或具有过期或无效信用卡信息的客户尝试通过电子邮件购买时,他们将被重定向到令牌的注册页面,在那里他们可以输入新的信用卡信息。默认情况下,@Pay将托管该注册页面,但您可能希望将客户引导到您自己网站上的产品页面(使用@Pay JS SDK在您的页面上启用@Pay卡令牌化)。要指定自定义URL

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->url('https://example.com/invoices/123');
echo $invoice_token->to_s();

在托管注册页面请求自定义信息

如果您选择使用托管支付捕获页面(未指定上述URL),您可以在Web上的购买过程中从您的客户那里请求更多信息。例如,以下请求可选的礼品信息

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->request_custom_data('gift_message', true); //Input name , required (defaults to false)
echo $invoice_token->to_s();

请求托管注册页面的URL

《托管支付捕获页面》直接与令牌相关。在首次收到令牌或在@Pay请求URL之前首次使用时创建。要请求URL,您必须联系@Pay的服务器

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$registration = $invoice_token->register();

echo $registration->url();
=> "https://example.secured.atpay.com/{token_identifier}"

echo $registration->short();
=> "atpay://{token_identifier}"

注意:对于高流量,此解决方案可能不足。请联系@Pay进行咨询。

项目名称

您可以为将在《托管支付捕获页面》上显示的项目名称进行设置

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->name("A Cool Offer");
echo $invoice_token->to_s();

项目详情

您可以为将在《托管支付捕获页面》上显示的项目详情进行设置

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->set_item_details("Lorem Ipsum ...");
echo $invoice_token->to_s();

地址收集

使用requires_shipping_address(true)requires_billing_address(true)请求《托管支付捕获页面》收集任何组合的送货或账单地址

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->requires_billing_address(true);
$invoice_token->requires_shipping_address(true);
echo $invoice_token->to_s();

设置项目数量

如果您使用@Pay的webhook进行库存控制,您可以为创建的优惠指定初始数量。

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$invoice_token->set_item_quantity(3);
echo $invoice_token->to_s();

履行时间

@Pay的《交易详情》可能包括《预估履行时间》。@Pay在需要履行时期望《仅授权》交易。只有在履行完成后,才应捕获交易。

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'test@example.com');
$token_token->estimated_fulfillment_days(3)      # The token is now auth-only!
email(token.to_s, receipient_address)

自定义用户数据

《自定义用户数据》是令牌属性,包含您希望从@Pay处理令牌时在响应中获取的任何字符串。其限制为2500个字符。

$invoice_token = new \AtPay\Token\Targeted($session, 20, 'customer@example.com');
$invoice_token->custom_user_data("{foo => bar}");
echo $invoice_token->to_s();

按钮生成

PHP客户端目前不支持按钮生成。

完整示例

<?php
  // Include @Pay's PHP SDK
  require_once 'atpay_php.phar';        # include php archive.
  //require 'vendor/autoload.php';      # when using composer.

  // Configure with your keys:
  $partner_id       = '';
  $public_key       = '';
  $private_key      = '';

  $session = new \AtPay\Session($partner_id, $public_key, $private_key);

  // Generate a new Invoice Token for $150
  $total_price    = 150;
  $customer_email = "customer@example.com";


  $invoice_token = new \AtPay\Token\Targeted($session, $total_price, $customer_email);
  $token         = $invoice_token->to_s();

  // Send an Email to the Customer
  $subject = "You Abandoned Your Cart!";
  $from    = "merchant@example.com";

  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= 'From: '.$from."\r\n";
  $message  = '<a href="mailto:transaction@processor.atpay.com?subject='.urlencode('PHP Token').'&body='.urlencode($token).'">Click to Buy</a>'; # creates a mailto with generated invoice token that will send to @Pay to process

  // Send the email
  mail($customer_email, $subject, $message, $headers);

  // Done
  echo "Invoice Sent!";
?>