zjl606/moneris-api

Moneris API 修改了craigpaul/moneris-api的代码

0.8.3 2020-10-21 13:35 UTC

This package is auto-updated.

Last update: 2024-09-21 23:50:50 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

要求

PHP 5.6 及以上

Composer

要开始,请通过Composer包管理器安装此包

composer require craigpaul/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); // 

响应和收据

ResponseReceipt 对象允许您了解您的 API 调用情况。交易处理完成后,Response 将被验证并返回所有相关信息。

响应

您可以在 Response 对象上获得以下信息

错误

$errors = $response->errors;

您的交易过程中可能出现的任何错误将以以下格式返回。以这种格式返回是为了让您利用每个错误中的唯一 titlefield 键,在自己的应用程序中处理任何翻译逻辑。

// 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 许可