stymiee/authnetjson

这是一个库,它抽象了 Authorize.Net 的 JSON API。这包括高级集成方法 (AIM)、自动定期计费 (ARB)、客户信息管理器 (CIM)、交易报告、简单集成方法 (SIM) 和 Webhooks。

4.2.0 2023-02-13 15:42 UTC

README

Latest Stable Version Total Downloads Scrutinizer Code Quality Build Status Code Coverage Maintainability License

AuthnetJSON

这是一个库,它抽象了 Authorize.NetJSON API

要求

  • PHP 7.2+(支持 PHP 7.2.0 - 8.*)
  • cURL PHP 扩展
  • JSON PHP 扩展
  • 一个 Authorize.Net 账户

已从 master 分支中移除对 PHP 版本小于 7.2 的支持。对于开发,可以使用 PHP 5.6 兼容分支,并且只要可行,仍可以制作 5.6 版本的发布。

安装

如果使用 Composer 管理项目依赖项,只需将 stymiee/authnetjson 作为依赖项添加到项目的 composer.json 文件中。

以下是一个 composer.json 文件的示例,它仅定义了 AuthnetJSON 的依赖项

{
    "require": {
        "stymiee/authnetjson": "~4.1"
    }
}

基本用法

使用此库通常包括三个步骤

  1. 使用您的 Authorize.Net 账户登录凭证初始化库
  2. 通过数组传递任何所需参数进行 API 调用
  3. 检查结果并适当使用

注意:如果您正在浏览器中查看任何示例,您需要在使用之前将您的 Authorize.Net 凭证填写到 config.inc.php

简单用法

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getTransactionDetailsRequest([
    'transId' => '2162566217'
]);
if ($response->isSuccessful()) {
    echo $response->transaction->transactionStatus;
}

API 调用期间要传递的数组的格式遵循 Authorize.Net 集成指南 中的结构。

使用 Authorize.Net 开发服务器

Authorize.Net 为开发者提供了一个开发环境,以测试他们的集成。要使用此端点(与生产端点相比),将 AuthnetApiFactory::getJsonApiHandler() 的可选第三个参数设置为 1 或使用内置类常量 AuthnetApiFactory::USE_DEVELOPMENT_SERVER

$json = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY, 
                                                AuthnetApiFactory::USE_DEVELOPMENT_SERVER);

用法示例

为了帮助理解如何使用此库,在 example 目录中提供了示例 API 调用。代表了所有当前的 API 调用示例。您可能需要根据其他 API 调用生成的有效值进行调整(例如,一个取消交易在没有有效交易 ID 的情况下将无法工作)。

授权并捕获(基本)

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createTransactionRequest([
    'refId' => rand(1000000, 100000000),
    'transactionRequest' => [
        'transactionType' => 'authCaptureTransaction',
        'amount' => 5,
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4111111111111111',
                'expirationDate' => '122026',
                'cardCode' => '999',
            ]
        ]
    ]
]);

if ($response->isSuccessful()) {
    echo $response->transactionResponse->authCode;
}

授权并捕获(完整)

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createTransactionRequest([
    'refId' => rand(1000000, 100000000),
    'transactionRequest' => [
        'transactionType' => 'authCaptureTransaction',
        'amount' => 5,
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4111111111111111',
                'expirationDate' => '122026',
                'cardCode' => '999',
            ],
        ],
        'order' => [
            'invoiceNumber' => '1324567890',
            'description' => 'this is a test transaction',
        ],
        'lineItems' => [
            'lineItem' => [
                0 => [
                    'itemId' => '1',
                    'name' => 'vase',
                    'description' => 'Cannes logo',
                    'quantity' => '18',
                    'unitPrice' => '45.00'
                ],
                1 => [
                    'itemId' => '2',
                    'name' => 'desk',
                    'description' => 'Big Desk',
                    'quantity' => '10',
                    'unitPrice' => '85.00'
                ]
            ]
        ],
        'tax' => [
           'amount' => '4.26',
           'name' => 'level2 tax name',
           'description' => 'level2 tax',
        ],
        'duty' => [
           'amount' => '8.55',
           'name' => 'duty name',
           'description' => 'duty description',
        ],
        'shipping' => [
           'amount' => '4.26',
           'name' => 'level2 tax name',
           'description' => 'level2 tax',
        ],
        'poNumber' => '456654',
        'customer' => [
           'id' => '18',
           'email' => 'someone@blackhole.tv',
        ],
        'billTo' => [
           'firstName' => 'Ellen',
           'lastName' => 'Johnson',
           'company' => 'Souveniropolis',
           'address' => '14 Main Street',
           'city' => 'Pecan Springs',
           'state' => 'TX',
           'zip' => '44628',
           'country' => 'USA',
        ],
        'shipTo' => [
           'firstName' => 'China',
           'lastName' => 'Bayles',
           'company' => 'Thyme for Tea',
           'address' => '12 Main Street',
           'city' => 'Pecan Springs',
           'state' => 'TX',
           'zip' => '44628',
           'country' => 'USA',
        ],
        'customerIP' => '192.168.1.1',
        'transactionSettings' => [
            'setting' => [
                0 => [
                    'settingName' =>'allowPartialAuth',
                    'settingValue' => 'false'
                ],
                1 => [
                    'settingName' => 'duplicateWindow',
                    'settingValue' => '0'
                ],
                2 => [
                    'settingName' => 'emailCustomer',
                    'settingValue' => 'false'
                ],
                3 => [
                    'settingName' => 'recurringBilling',
                    'settingValue' => 'false'
                ],
                4 => [
                    'settingName' => 'testRequest',
                    'settingValue' => 'false'
                ]
            ]
        ],
        'userFields' => [
            'userField' => [
                0 => [
                    'name' => 'MerchantDefinedFieldName1',
                    'value' => 'MerchantDefinedFieldValue1',
                ],
                1 => [
                    'name' => 'favorite_color',
                    'value' => 'blue',
                ],
            ],
        ],
    ],
]);

if ($response->isSuccessful()) {
    echo $response->transactionResponse->authCode;
}

创建客户资料

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createCustomerProfileRequest([
        'profile' => [
        'merchantCustomerId' => '12345',
        'email' => 'user@example.com',
        'paymentProfiles' => [
            'billTo' => [
                'firstName' => 'John',
                'lastName' => 'Smith',
                'address' => '123 Main Street',
                'city' => 'Townsville',
                'state' => 'NJ',
                'zip' => '12345',
                'phoneNumber' => '800-555-1234'
            ],
            'payment' => [
                'creditCard' => [
                'cardNumber' => '4111111111111111',
                'expirationDate' => '2026-08',
                ],
            ],
        ],
        'shipToList' => [
            'firstName' => 'John',
            'lastName' => 'Smith',
            'address' => '123 Main Street',
            'city' => 'Townsville',
            'state' => 'NJ',
            'zip' => '12345',
            'phoneNumber' => '800-555-1234'
        ],
    ],
    'validationMode' => 'liveMode'
]);

if ($response->isSuccessful()) {
    echo $response->customerProfileId;
}

创建定期订阅

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->ARBCreateSubscriptionRequest([
    'refId' => 'Sample',
    'subscription' => [
        'name' => 'Sample subscription',
        'paymentSchedule' => [
            'interval' => [
                'length' => '1',
                'unit' => 'months'
            ],
            'startDate' => '2020-04-18',
            'totalOccurrences' => '12',
            'trialOccurrences' => '1'
        ],
        'amount' => '10.29',
        'trialAmount' => '0.00',
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4111111111111111',
                'expirationDate' => '2016-08'
            ]
        ],
        'billTo' => [
            'firstName' => 'John',
            'lastName' => 'Smith'
        ]
    ]
]);

if ($response->isSuccessful()) {
    echo $response->subscriptionId;
}

获取已结算批次列表

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getSettledBatchListRequest([
    'includeStatistics'   => 'true',
    'firstSettlementDate' => '2020-01-01T08:15:30',
    'lastSettlementDate'  => '2020-01-30T08:15:30',
]);

if ($response->isSuccessful()) {
    foreach ($response->batchList as $batch) {
        echo $batch->batchId;
    }
}

从 CIM API 调用获取交易详情

一些 CIM API 调用处理 AUTH_CAPTURE 交易并返回类似于 AIM AUTH_CAPTURE 交易的数据。要访问此信息,可以使用字段名或字段号调用 AuthnetJsonResponse::getTransactionResponseField()。例如,如果您正在查找交易 ID,则可以使用

$response->getTransactionResponseField('TransactionID');

$response->getTransactionResponseField(7);

字段名和编号可以在 Authorize.Net AIM 指南 中找到。请注意,字段名中的所有空格都被移除了,因此 TransactionID 变成了 TransactionID

创建 Webhook

$response = $request->createWebhooks([
    "net.authorize.customer.subscription.expiring",
    "net.authorize.customer.subscription.suspended",
    "net.authorize.payment.authcapture.created",
    "net.authorize.payment.authorization.created",
    "net.authorize.payment.capture.created",
    "net.authorize.payment.fraud.approved",
    "net.authorize.payment.fraud.declined",
    "net.authorize.payment.fraud.held",
    "net.authorize.payment.priorAuthCapture.created",
    "net.authorize.payment.refund.created",
    "net.authorize.payment.void.created"
], 'http://www.example.com:55950/api/webhooks', 'active');

验证和访问 Webhook

$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload);
if ($webhook->isValid()) {
    // Access notifcation values
    // echo $webhook->eventType;
}

如果 apache_request_headers()/getallheaders() 不可用,您需要获取 HTTP 请求头并将其作为第三个参数传递给 AuthnetWebhook()

$headers = yourGetHeadersFunction();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload, $headers);
if ($webhook->isValid()) {
    // Access notifcation values
    // echo $webhook->eventType;
}

Accept.js

要查看由 Accept.js 驱动的自托管支付表单Authorize.Net 托管支付表单托管客户资料页面的示例,请访问Accept.js 示例目录

调试

为了帮助调试,已覆盖 __toString() 方法以输出与该库使用相关的关键元素。简单地将 AuthnetJSON 对象进行 echo 输出即可查看。

  • 使用的 API 登录 ID
  • 使用的 API 交易密钥
  • 请求发送到的 API 端点
  • 请求 JSON
  • 响应 JSON

基本用法

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getUnsettledTransactionListRequest();
echo $request, $response;

支持

如果您需要使用此库的帮助,请先查看此包中包含的 HELP.md 文件。它包括常见问题和它们的解决方案。

如果您需要额外的帮助,我可以在 Stack Overflow 找到。在提问有关此类使用的问题时,请确保将问题标记为 PHPAuthorize.Net。确保您遵循他们的提问指南,因为问得不好的问题将被关闭,我将无法为您提供帮助。

不要使用 Stack Overflow 来报告错误。错误可以通过这里报告。