thetestcoder / moneris-api
升级 craigpaul/moneris-api 版本。更简单的方式消费 Moneris eSELECTplus API。
Requires
- php: >=7.3
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- fzaninotto/faker: ^1.6
- mockery/mockery: ~1.5.0
- phpunit/phpunit: ~9.0
- squizlabs/php_codesniffer: ^2.7
- symfony/var-dumper: ^3.1
README
介绍
craigpaul/moneris-api 的新版本,所有荣誉归于 Craig Paul。
要求
PHP 7.3 及更高版本
Composer
要开始使用,请通过 Composer 包管理器安装此包
composer require thetestcoder/moneris-api
实例化
创建一个新的 Moneris 实例非常简单直接。
use CraigPaul\Moneris\Moneris; ... $id = 'store1'; $token = 'yesguy'; // optional $params = [ 'environment' => Moneris::ENV_TESTING, // default: Moneris::ENV_LIVE 'avs' => true, // default: false 'cvd' => true, // default: false 'cof' => true, // default: false ]; $gateway = (new Moneris($id, $token, $params))->connect();
use CraigPaul\Moneris\Moneris; ... $id = 'store1'; $token = 'yesguy'; // optional $params = [ 'environment' => Moneris::ENV_TESTING, // default: Moneris::ENV_LIVE 'avs' => true, // default: false 'cvd' => true, // default: false 'cof' => true, // default: false ]; $gateway = Moneris::create($id, $token, $params);
注意:请注意,Moneris 商店 ID 和 API 令牌始终需要传递给 Moneris 构造函数或静态 create 方法。
交易
一旦您的网关实例化后(见上方),进行购买、预授权卡片、取消交易等操作都非常简单。
购买
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->purchase($params);
预授权
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->preauth($params);
捕获(预授权完成)
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->preauth($params); $response = $gateway->capture($response->transaction);
取消(购买纠正)
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->purchase($params); $response = $gateway->void($response->transaction);
退款
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->purchase($params); $response = $gateway->refund($response->transaction);
卡片验证
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->verify($params);
CVD 和 AVS
要利用 Moneris 提供的卡片验证数字和/或地址验证服务,您需要在实例化时通知 Moneris(见上方)。
在执行 CVD 保护的购买、预授权或卡片验证时,您需要将以下参数传递给您正在使用的网关方法。
$params = [ // `cvd` needs to be included in your transaction parameters. 'cvd' => '111', 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => $this->visa, 'expdate' => '2012', ]; $response = $gateway->verify($params); // could be purchase, preauth, etc.
在执行 AVS 保护的购买、预授权或卡片验证时,您需要将以下参数传递给您正在使用的网关方法。
$params = [ // `avs_*` keys need to be included in your transaction parameters. 'avs_street_number' => '123', 'avs_street_name' => 'Fake Street', 'avs_zipcode' => 'X0X0X0', 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => $this->visa, 'expdate' => '2012', ]; $response = $gateway->verify($params); // could be purchase, preauth, etc.
注意:在执行 AVS 或 CVD 保护的交易时,即使 AVS 或 CVD 失败,您仍必须取消交易(该死 Moneris!)。有两种简单的方法可以解决这个问题。
首先验证卡片。使用这种方法,有一个额外的注意事项(让我再重复一次……该死 Moneris!)您的验证交易和购买交易必须有不同的 order_id
参数。一个解决方案是在验证订单 ID 的前面添加一个特定的前缀。
$response = $gateway->verify($params); if ($response->successful && !$response->failedAvs && !$response->failedCvd) { $response = $gateway->purchase($params); if ($response->successful) { $receipt = $response->receipt(); } else { $errors = $response->errors; } }
取消交易。
$response = $gateway->purchase($params); if ($response->successful && ($response->failedAvs || $response->failedCvd)) { $errors = $response->errors; $response = $gateway->void($response->transaction); } elseif (!$response->successful) { $errors = $response->errors; } else { $receipt = $response->receipt(); }
文件凭证
文件凭证是 Visa 新要求的组成部分,以通过 CVD/CVV2 数据进行交易。
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'data_key' => $key, 'payment_indicator' => 'U', 'payment_information' => '2', 'issuer_id' => $issuer_id // this is optional ]; $response = $vault->purchase($params); // could be purchase, preauth, etc.
保险库
Moneris 保险库允许您在 Moneris 服务器上创建和保留信用卡资料,而不是在自己的服务器上。要访问保险库,您需要具有实例化的网关(见上方)。
$vault = $gateway->cards();
添加一张卡
注意:传递给信用卡的有效期格式为 YYMM,因为 Moneris 是这样接受的。
use CraigPaul\Moneris\CreditCard; ... $card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card);
更新一张卡
为了维护您的信用卡资料,Moneris 将发送一个唯一的键来跟踪您的数据库中的资料。您可以在收到收据后检索该键(见下文)。
$card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card); $key = $response->receipt()->read('key'); $card->expiry = '2112'; $response = $vault->update($key, $card);
删除一张卡
$card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card); $key = $response->receipt()->read('key'); $response = $vault->delete($key);
附加客户
为了同步您的客户信息与保险库中存储的信用卡,我们可以将一个基本的 Customer
对象附加到 CreditCard
。
添加一张卡
use CraigPaul\Moneris\Customer; ... $params = [ 'id' => uniqid('customer-', true), 'email' => 'example@email.com', 'phone' => '555-555-5555', 'note' => 'Customer note', ]; $customer = Customer::create($params); $card = CreditCard::create('4242424242424242', '2012'); $card = $card->attach($customer); $response = $vault->add($card);
更新一张卡和客户
use CraigPaul\Moneris\Customer; ... $params = [ 'id' => uniqid('customer-', true), 'email' => 'example@email.com', 'phone' => '555-555-5555', 'note' => 'Customer note', ]; $customer = Customer::create($params); $card = CreditCard::create('4242424242424242', '2012'); $card = $card->attach($customer); $response = $vault->add($card); $key = $response->receipt()->read('key'); $card->customer->email = 'example2@email.com'; $response = $vault->update($key, $card);
对之前的交易进行令牌化
用于根据之前的交易创建信用卡资料。
$params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'credit_card' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '20', ]; $response = $gateway->purchase($params); $response = $vault->tokenize($response->transaction);
查看保险库
如果您需要查找掩码的信用卡号码,您可以查看保险库。
$card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card); $key = $response->receipt()->read('key'); $response = $vault->peek($key); $receipt = $response->receipt(); $masked = $receipt->read('data')['masked_pan'];
检索即将到期的卡片
根据Moneris API规定,这笔交易在任何给定日历日最多只能执行2次。
$response = $vault->expiring();
交易
存储在Moneris保险库中的信用卡在购买和预授权流程上略有不同。其他交易与上述操作完全相同。
保险库购买
$card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card); $key = $response->receipt()->read('key'); $params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'data_key' => $key, ]; $response = $vault->purchase($params); //
注意:此处使用保险库进行交易,而不是基本网关对象。
保险库预授权
$card = CreditCard::create('4242424242424242', '2012'); $response = $vault->add($card); $key = $response->receipt()->read('key'); $params = [ 'order_id' => uniqid('1234-56789', true), 'amount' => '1.00', 'data_key' => $key, ]; $response = $vault->preauth($params); //
响应和收据
Response
和Receipt
对象允许您了解API调用中的所有情况。交易处理完成后,Response
将进行验证,并返回所有相关信息。
响应
在Response
对象上可用的信息如下
错误
$errors = $response->errors;
交易过程中可能出现的任何错误将以以下格式返回给您。以这种格式返回,以便您可以利用每个错误中的唯一title
和field
键在自己的应用程序中处理任何翻译逻辑。
// The following example would be returned when you forget to set the `order_id` on your transaction. $errors = [ [ 'field' => 'order_id', 'code' => self::PARAMETER_NOT_SET, // 2 'title' => 'not_set' ], ];
状态
$status = $response->status;
状态将返回与返回的适当错误相匹配的状态代码。以下为可能返回的状态示例。
ERROR = -23; INVALID_TRANSACTION_DATA = 0; FAILED_ATTEMPT = -1; CREATE_TRANSACTION_RECORD = -2; GLOBAL_ERROR_RECEIPT = -3; SYSTEM_UNAVAILABLE = -14; CARD_EXPIRED = -15; INVALID_CARD = -16; INSUFFICIENT_FUNDS = -17; PREAUTH_FULL = -18; DUPLICATE_TRANSACTION = -19; DECLINED = -20; NOT_AUTHORIZED = -21; INVALID_EXPIRY_DATE = -22; CVD = -4; CVD_NO_MATCH = -5; CVD_NOT_PROCESSED = -6; CVD_MISSING = -7; CVD_NOT_SUPPORTED = -8; AVS = -9; AVS_POSTAL_CODE = -10; AVS_ADDRESS = -11; AVS_NO_MATCH = -12; AVS_TIMEOUT = -13; POST_FRAUD = -22;
成功
$success = $response->successful
成功的属性仅告知您交易是否已成功处理。
收据
Receipt
对象是您提交的交易中任何相关信息的记录。要检索收据,请参阅以下内容。
$response = $gateway->purchase($params); $receipt = $response->receipt();
根据交易类型,您的Receipt
上将有不同的条目可供读取。
$amount = $receipt->read('amount');
有关可读收据项的完整列表,请参阅以下内容。
amount - The amount of the transaction. (string) authorization - The authorization code for the transaction. (string) avs_result - The avs result code for the transaction. (string) card - The card type used for the transaction. (string) code - The response code for the transaction. (string) complete - Whether the transaction had completed correctly or not. (boolean) cvd_result - The cvd result code. (string) data - The data related to the customer and card for the transaction. (array) date - The date of the transaction. (string) id - The Moneris id of the receipt. (string) iso - The ISO code for the transaction. (string) key - The data key used for vault transactions. (string) message - Any relevant message provided for the transaction. (string) reference - The reference number for the transaction. (string) time - The time of the transaction. (string) transaction - The Moneris id of the transaction. (string) type - The transaction type. (string)
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
鸣谢
许可证
Moneris API是开源软件,许可协议为MIT许可证。