如何在 Authorize.net ARB Create Subscription API 中添加电话和公司
支付小周
结论
phoneNumber 和 company 分别属于不同的 ARB 数据对象:
phoneNumber应设置在CustomerType中,再通过$subscription->setCustomer()添加到订阅。company应设置在账单地址对象NameAndAddressType中,再通过$subscription->setBillTo()添加到订阅。
ARB 的 $billTo 使用 NameAndAddressType,这个类型通常没有 setPhoneNumber() 方法,因此不能直接把 phoneNumber 设置到 $billTo 中。
PHP 示例
以下代码只包含客户信息和账单地址,可以合并到官方的 Create Subscription 示例中:
<?php
use net\authorize\api\contract\v1 as AnetAPI;
// 客户联系信息
$customer = new AnetAPI\CustomerType();
$customer->setId("12345");
$customer->setEmail("customer@example.com");
$customer->setPhoneNumber("1234567890");
// 账单地址
$billTo = new AnetAPI\NameAndAddressType();
$billTo->setFirstName("John");
$billTo->setLastName("Smith");
$billTo->setCompany("Example Company");
$billTo->setAddress("123 Main Street");
$billTo->setCity("Bellevue");
$billTo->setState("WA");
$billTo->setZip("98004");
$billTo->setCountry("USA");
// 创建订阅对象
$subscription = new AnetAPI\ARBSubscriptionType();
// 其他订阅设置,例如名称、付款计划、金额和支付信息
$subscription->setName("Example Subscription");
$subscription->setCustomer($customer);
$subscription->setBillTo($billTo);
如果已经创建了 $subscription、付款计划和支付信息,只需补充下面这段代码:
$customer = new AnetAPI\CustomerType();
$customer->setPhoneNumber("1234567890");
$subscription->setCustomer($customer);
$billTo = new AnetAPI\NameAndAddressType();
$billTo->setCompany("Example Company");
$subscription->setBillTo($billTo);
已有客户或账单对象时
如果代码中已经有 $customer 或 $billTo,不要再次创建对象,否则可能覆盖之前设置的姓名、邮箱或地址。直接添加所需字段即可:
$customer->setPhoneNumber("1234567890");
$billTo->setCompany("Example Company");
$subscription->setCustomer($customer);
$subscription->setBillTo($billTo);
对应的数据结构
生成的请求结构大致如下:
<subscription>
<customer>
<id>12345</id>
<email>customer@example.com</email>
<phoneNumber>1234567890</phoneNumber>
</customer>
<billTo>
<firstName>John</firstName>
<lastName>Smith</lastName>
<company>Example Company</company>
<address>123 Main Street</address>
<city>Bellevue</city>
<state>WA</state>
<zip>98004</zip>
<country>USA</country>
</billTo>
</subscription>
注意事项
- 不要对
NameAndAddressType调用$billTo->setPhoneNumber()。其他 Authorize.net API 使用的账单地址类型可能支持电话字段,但 ARB 使用的对象类型可能不同。 - 电话号码应符合 Authorize.net 当前接口允许的长度和格式,不要加入接口不支持的扩展符号。
- 如果调用
setPhoneNumber()或setCompany()时提示方法不存在,请检查已安装的authorizenet/authorizenetPHP SDK 版本及其生成的契约类。不同版本的 SDK 类定义可能有差异。 - 调试时可以记录序列化后的请求 XML,检查
phoneNumber是否位于<customer>下,以及company是否位于<billTo>下。生产日志中不要记录完整的银行卡信息。
备注:内容仅供参考。
