Laravel PHP 创建 Authorize.Net subscription 报 E00121
结论
E00121 No default payment/shipping profile found 表示 Authorize.Net 在客户档案中没有找到本次订阅可用的支付档案或配送档案。该错误来自 Authorize.Net,与 Laravel 无关;服务类业务也不一定需要提供配送地址。
创建 subscription 时,请求只要包含有效的 customerProfileId 和 customerPaymentProfileId,通常就不需要 customerAddressId。如果仍收到 E00121,应检查实际发送的 customerPaymentProfileId 是否为空、是否属于对应客户,以及 Sandbox 环境与档案 ID 是否一致。请求未明确指定支付档案时,网关才会尝试使用默认档案。
为什么会触发 E00121
Authorize.Net 的客户档案包含三类 ID:
customerProfileId:客户档案 ID。customerPaymentProfileId:支付档案 ID,包含银行卡或银行账户以及billTo信息。customerAddressId:配送地址档案 ID。
Authorize.Net 将 E00121 解释为客户档案中没有默认 payment/shipping profile。Authorize.Net 错误码说明
错误信息同时提到 payment 和 shipping,因为这是一条通用提示,不能由此认定缺少配送地址。不配送商品的服务可以省略 customerAddressId。
代码片段无法反映运行时参数,所以暂时无法确定具体是哪个值有问题。常见原因包括:
- 调用
createSubscription()时,$customerPaymentProfileId实际是null、空字符串,或传入了错误字段。 - 该支付档案不属于传入的
customerProfileId。 - 请求使用 Sandbox 凭据,却传入了 Production 环境生成的档案 ID,或者反过来。
- 客户档案中只有支付信息,并未创建真正的 Customer Payment Profile。
- 请求未成功序列化
customerPaymentProfileId,网关转而查找默认支付档案,但该客户没有默认档案。 - 支付档案缺少订阅所需的姓名信息。Authorize.Net 当前文档要求,用于 subscription 的 payment profile 必须填写
firstName和lastName。Create a Subscription from Customer Profile
先验证传入的 ID
构造 API 请求前,直接验证运行时值:
$customerProfileId = trim((string) $customerProfileId);
$customerPaymentProfileId = trim((string) $customerPaymentProfileId);
if ($customerProfileId === '') {
throw new \InvalidArgumentException('customerProfileId is required.');
}
if ($customerPaymentProfileId === '') {
throw new \InvalidArgumentException('customerPaymentProfileId is required.');
}
然后使用同一套 Sandbox 凭据调用 GetCustomerProfile,确认该支付档案确实属于这个客户:
$getProfileRequest = new AnetAPI\GetCustomerProfileRequest();
$getProfileRequest->setMerchantAuthentication($merchantAuthentication);
$getProfileRequest->setCustomerProfileId($customerProfileId);
$controller = new AnetController\GetCustomerProfileController($getProfileRequest);
$response = $controller->executeWithApiResponse(
\net\authorize\api\constants\ANetEnvironment::SANDBOX
);
if (
$response === null ||
$response->getMessages()->getResultCode() !== 'Ok'
) {
throw new \RuntimeException('Unable to retrieve Authorize.Net customer profile.');
}
$paymentProfileFound = false;
foreach ($response->getProfile()->getPaymentProfiles() as $paymentProfile) {
if (
(string) $paymentProfile->getCustomerPaymentProfileId() ===
$customerPaymentProfileId
) {
$paymentProfileFound = true;
break;
}
}
if (!$paymentProfileFound) {
throw new \RuntimeException(
'The payment profile does not belong to the specified customer profile.'
);
}
正确构造 subscription profile
业务不涉及配送时,只需发送客户档案和支付档案:
$profile = new AnetAPI\CustomerProfileIdType();
$profile->setCustomerProfileId($customerProfileId);
$profile->setCustomerPaymentProfileId($customerPaymentProfileId);
if ($customerAddressId !== null && trim((string) $customerAddressId) !== '') {
$profile->setCustomerAddressId(trim((string) $customerAddressId));
}
$subscription->setProfile($profile);
构造完整请求时,建议使用 $apiRequest,以免覆盖原来的 Laravel $request 变量:
$apiRequest = new AnetAPI\ARBCreateSubscriptionRequest();
$apiRequest->setMerchantAuthentication($merchantAuthentication);
$apiRequest->setRefId($refId);
$apiRequest->setSubscription($subscription);
$controller = new AnetController\ARBCreateSubscriptionController($apiRequest);
$response = $controller->executeWithApiResponse(
\net\authorize\api\constants\ANetEnvironment::SANDBOX
);
if ($response === null) {
return [
'success' => false,
'message' => 'Authorize.Net returned no response.',
];
}
if ($response->getMessages()->getResultCode() === 'Ok') {
return [
'success' => true,
'message' => 'SUCCESS: Subscription ID: ' .
$response->getSubscriptionId(),
];
}
$messages = $response->getMessages()->getMessage();
return [
'success' => false,
'message' => isset($messages[0])
? 'Response: ' .
$messages[0]->getCode() . ' ' .
$messages[0]->getText()
: 'Authorize.Net returned an unknown error.',
];
PHP 方法名不区分大小写,所以原代码中的 setmerchantAuthentication() 通常也能正常调用。按照 SDK 中的方法名写成 setMerchantAuthentication() 会更清楚。
需要默认支付档案时
如果请求只发送 customerProfileId,没有指定 customerPaymentProfileId,Authorize.Net 会尝试使用默认支付档案。创建 payment profile 时,可以将其设为默认:
$paymentProfile = new AnetAPI\CustomerPaymentProfileType();
$paymentProfile->setBillTo($billTo);
$paymentProfile->setPayment($payment);
$paymentProfile->setDefaultPaymentProfile(true);
已有支付档案可以通过 Merchant Interface 或对应的 Customer Profile 更新 API 设为默认。具体支持哪些更新类型和方法,取决于项目安装的 authorizenet/authorizenet SDK 版本。
只要请求始终携带有效的 customerPaymentProfileId,通常无需设置默认档案。
配送地址与账单信息不是一回事
customerAddressId 指向 shipping profile,服务类业务可以省略。payment profile 中的 billTo 则属于账单信息,其中仍需填写 firstName 和 lastName。省略 shipping profile 后,payment profile 依然要包含订阅接口要求的账单姓名。
排查时请确认:
- 两个 ID 均来自同一个 Sandbox 商户账户。
customerPaymentProfileId属于对应的customerProfileId。- API 请求中确实包含
customerPaymentProfileId。 - payment profile 中存在
billTo.firstName和billTo.lastName。 - 不配送商品时,不要发送空的
customerAddressId。
备注:内容仅供参考。
