academe / authorizenet-objects
Authorize.Net API 消息值对象
Requires
- php: >=5.6.0
Requires (Dev)
- moneyphp/money: ^3.0
- phpunit/phpunit: ^5.7 || ^6.4 || ^7.0
- squizlabs/php_codesniffer: ^3
Suggests
- moneyphp/money: ^3.0
README
该软件包的目标是将以下定义的所有数据结构建模为值对象: http://developer.authorize.net/api/reference/
API消息的数据结构定义在 XSD DTD 中。虽然这是针对XSL API的,但JSON API与之非常相似。该DTD已使用 xs3p.xsl 解析到此文档页面,这可以更容易阅读,并且比官方HTML文档包含的错误更少。实际上,可以将两者结合使用。
通用项目结构
请求消息位于 Academe\AuthorizeNet\Request
下。所有这些请求都接受一个 Academe\AuthorizeNet\Auth\MerchantAuthentication
对象以提供身份验证,以及一个或多个额外的对象来携带主要消息细节。
Academe\AuthorizeNet\Request\CreateTransaction
消息接受来自 Academe\AuthorizeNet\Request\Transaction
下的交易对象。该 Transaction
对象主要从标量数据和 Academe\AuthorizeNet\Request\Model
下的其他对象构建。
在 Academe\AuthorizeNet\Collections
下有集合用于分组一些 Model
对象,例如 LineItems
或 UserFields
。这些 Collections
有可能在某个时候移动到 Academe\AuthorizeNet\Request
下,因此请为此做好准备。
注意:目前此文档比较稀疏,但代码涵盖了API的大部分内容。
示例
使用此软件包的一个包是 Authorize.Net API 驱动程序,用于 Omnipay 项目。
然后,可以进一步将这些对象包装到网关驱动程序中。任何构建后的请求对象都可以序列化为JSON(json_encode()
),以提供API期望的请求正文。
来自 Authorize.Net 的响应将是一个JSON结构,应用程序可以解析它。还打算在此软件包中创建一个工厂,将响应解析为值对象和集合。已经创建了许多响应值对象,但还没有工厂来填充它们。
<?php use Academe\AuthorizeNet; use Academe\AuthorizeNet\Auth; use Academe\AuthorizeNet\Amount; use Academe\AuthorizeNet\Payment; use Academe\AuthorizeNet\Request; use Academe\AuthorizeNet\Collections; // Composer: moneyphp/money use Money\Money; require 'vendor/autoload.php'; echo "<pre>"; // Set up authorisation details. $auth = new Auth\MerchantAuthentication('xxx', 'yyy'); // Create a credit card. $credit_card = new Payment\CreditCard('4000123412341234', '1220', '123'); // Or create a magnetic stripe track (only one can be used). $track1 = new Payment\Track1('TTTTTTTTTTTTTTTT11111'); $track2 = new Payment\Track2('TTTTTTTT2222222222222222'); // Create order details. $order = new Request\Model\Order('orderx', 'ordery'); // Create customer detals. $customer = new Request\Model\Customer('business', 'Customer ID', 'customer@example.com'); // Create retail flags $retail = new Request\Model\Retail(2, 3, 'HJSHDJSDJKSD'); // Create a billing name and address. $billTo = new Request\Model\NameAddress('BFirstname', 'BLastname', null, 'Address Line'); $billTo = $billTo->withCompany('My Billing Company Ltd')->withZip('ZippyZipBill'); // Create a shipping name and address. $shipTo = new Request\Model\NameAddress(); // A single with() can set multiple values at once. $shipTo = $shipTo->with([ 'firstName' => 'Firstname', 'lastName' => 'Lastname', 'city' => 'My City', 'country' => 'United Kingdom', ]); // Create a total amount of £24.99 $amount = new Amount\Amount('GBP', 2499); // Or maybe just £19.99 $amount = $amount->withMajorUnit(19.99); // Better still, use moneyphp/money to set the amount to £15.49 $money_php_object = Money::GBP(1549); $amount = new Amount\MoneyPhp($money_php_object); // Create tax amount. // All parameters have a with*() method available. $tax = new Request\Model\ExtendedAmount(); $tax = $tax->withName('Tax Name')->withDescription('Tax Description'); $tax = $tax->withAmount(new Amount\MoneyPhp(Money::GBP(99)); // Set up some line items. // Note these collections are not value objects. Should they be? $lineItems = new Collections\LineItems(); $lineItems->push(new Request\Model\LineItem(1, 'Item Name 1', 'Item Desc 1', 1.5, new Amount\MoneyPhp(Money::GBP(49)), false)); $lineItems->push(new Request\Model\LineItem(2, 'Item Name 2', 'Item Desc 2', 2, new Amount\MoneyPhp(Money::GBP(97)), true)); // Set up some transaction settings. $transactionSettings = new Collections\TransactionSettings(); $transactionSettings->push(new Request\Model\Setting('Foo', 'Bar')); // And add some user fields. $userFields = new Collections\UserFields(); $userFields->push(new Request\Model\UserField('UserFoo', 'UserBar')); // Now put most of these into an Auth Capture transaction. $auth_capture_transaction = new Request\Transaction\AuthCapture($amount); $auth_capture_transaction = $auth_capture_transaction->withPayment(/*$track1*/ $credit_card); $auth_capture_transaction = $auth_capture_transaction->withCreateProfile(false); $auth_capture_transaction = $auth_capture_transaction->withTaxExempt(true); $auth_capture_transaction = $auth_capture_transaction->withLineItems($lineItems); $auth_capture_transaction = $auth_capture_transaction->withTransactionSettings($transactionSettings); $auth_capture_transaction = $auth_capture_transaction->withUserFields($userFields); $auth_capture_transaction = $auth_capture_transaction->withSolutionId('SOLLLL'); $auth_capture_transaction = $auth_capture_transaction->withTax($tax)->withDuty($tax)->withShipping($tax); $auth_capture_transaction = $auth_capture_transaction->withPoNumber('myPoNumber'); $auth_capture_transaction = $auth_capture_transaction->withCustomer($customer); $auth_capture_transaction = $auth_capture_transaction->withRetail($retail); // You can set multiple items using an array: $auth_capture_transaction = $auth_capture_transaction->with([ 'order' => $order, 'shipTo' => $shipTo, 'billTo' => $billTo, 'employeeId' => '1234', 'customerIp' => '1.2.3.4', ]); // Add the auth capture transaction to the transaction request, along with the auth details. $transaction_request = new Request\CreateTransaction($auth, $auth_capture_transaction); // Display the resulting JSON request message. echo '<p>' . $transaction_request->getObjectName() . ': </p>'; echo "<textarea style='width:100%;height: 12em'>" . json_encode($transaction_request) . "</textarea>"; /* { "createTransactionRequest":{ "merchantAuthentication":{ "name":"xxx", "transactionKey":"yyy" }, "refId":"REFREFREF", "transactionRequest":{ "transactionType":"authCaptureTransaction", "amount":"9.99", "currencyCode":"GBP", "payment":{ "creditCard":{ "cardNumber":"4000123412341234", "expirationDate":"1220", "cardCode":"123" } }, "profile":{ "createProfile":false }, "solution":{ "id":"SOLLLL" }, "order":{ "invoiceNumber":"orderx", "description":"ordery" }, "lineItems":{ "lineItem":[ { "itemId":1, "name":"Item Name 1", "description":"Item Desc 1", "quantity":1.5, "unitPrice":"0.49", "taxable":false }, { "itemId":2, "name":"Item Name 2", "description":"Item Desc 2", "quantity":2, "unitPrice":"0.97", "taxable":true } ] }, "tax":{ "amount":"9.99", "name":"Tax Name", "description":"Tax Description" }, "duty":{ "amount":"9.99", "name":"Tax Name", "description":"Tax Description" }, "shipping":{ "amount":"9.99", "name":"Tax Name", "description":"Tax Description" }, "taxExempt":true, "poNumber":"myPoNumber", "customer":{ "type":"business", "id":"Customer ID", "email":"customer@example.com" }, "billTo":{ "firstName":"BFirstname", "lastName":"BLastname", "company":"My Billing Company Ltd", "address":"Address Line", "zip":"ZippyZipBill" }, "shipTo":{ "firstName":"Firstname", "lastName":"Lastname", "city":"My City", "country":"United Kingdom" }, "customerIP":"1.2.3.4", "cardholderAuthentication":{ "authenticationIndicator":"AAAA" }, "retail":{ "marketType":2, "deviceType":3, "customerSignature":"HJSHDJSDJKSD" }, "employeeId":"1234", "transactionSettings":{ "setting":[ { "settingName":"Foo", "settingValue":"Bar" } ] }, "userFields":{ "userField":[ { "name":"UserFoo", "value":"UserBar" } ] } } } } */
可以使用Guzzle简单地(并且天真地,仅用于演示)发送到网关
// "guzzlehttp/guzzle": "~6.0" use GuzzleHttp\Client; $client = new GuzzleHttp\Client(); // sandbox endpoint $endpoint = 'https://apitest.authorize.net/xml/v1/request.api'; $response = $client->request('POST', $endpoint, [ // Just pass the object and Guzzle will cast it to JSON. 'json' => $transaction_request, ]); var_dump((string)$response->getBody()); /* { "transactionResponse":{ "responseCode":"1", "authCode":"59WHY9", "avsResultCode":"Y", "cvvResultCode":"P", "transId":"60020301993", "refTransID":"", "transHash":"2EA6BB2D8D88C587EA16C77C44C7B8B2", "testRequest":"0", "accountNumber":"XXXX1234", "entryMode":"Keyed", "accountType":"Visa", "messages":[ { "code":"1", "description":"This transaction has been approved." } ], "userFields":[ { "name":"UserFoo", "value":"UserBar" } ], "transHashSha2":"" }, "refId":"5678917213", "messages":{ "resultCode":"Ok", "message":[ { "code":"I00001", "text":"Successful." } ] } } */
然后可以将此响应数据解析为嵌套对象
$responseObject = new \Academe\AuthorizeNet\Response\Response(json_decode((string)$response->getBody(), true));