Node.js中为Authorize.net已有客户添加新卡片的实现求助
要给已创建的客户添加新支付卡片,需调用Authorize.net的CreateCustomerPaymentProfile API,以下是适配你现有代码结构的实现:
async addPaymentProfileToCustomer(customerProfileId, callback) { console.log('id =====', process.env.AUTHORIZE_LOGIN_ID) // 构建商户认证信息(和你创建客户时的逻辑一致) var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType(); merchantAuthenticationType.setName(process.env.AUTHORIZE_LOGIN_ID); merchantAuthenticationType.setTransactionKey(process.env.AUTHORIZE_TRANSACTION_KEY); // 构建新的信用卡信息 var creditCard = new ApiContracts.CreditCardType(); creditCard.setCardNumber('4242424242424242'); // 测试卡,替换为真实卡号 creditCard.setExpirationDate('1225'); // 格式为MMYY // 构建支付类型 var paymentType = new ApiContracts.PaymentType(); paymentType.setCreditCard(creditCard); // 构建账单地址(可选,根据需求调整) var customerAddress = new ApiContracts.CustomerAddressType(); customerAddress.setFirstName('test'); customerAddress.setLastName('new-card'); customerAddress.setAddress('456 Second Avenue'); customerAddress.setCity('Seattle'); customerAddress.setState('WA'); customerAddress.setZip('98101'); customerAddress.setCountry('USA'); customerAddress.setPhoneNumber('111-222-3333'); // 构建支付Profile var customerPaymentProfileType = new ApiContracts.CustomerPaymentProfileType(); customerPaymentProfileType.setCustomerType(ApiContracts.CustomerTypeEnum.INDIVIDUAL); customerPaymentProfileType.setPayment(paymentType); customerPaymentProfileType.setBillTo(customerAddress); // 创建添加支付Profile的请求 var createRequest = new ApiContracts.CreateCustomerPaymentProfileRequest(); createRequest.setCustomerProfileId(customerProfileId); // 传入已有客户的ID createRequest.setPaymentProfile(customerPaymentProfileType); createRequest.setValidationMode(ApiContracts.ValidationModeEnum.TESTMODE); // 测试模式,上线后修改 createRequest.setMerchantAuthentication(merchantAuthenticationType); // 执行请求并处理响应 var ctrl = new ApiControllers.CreateCustomerPaymentProfileController(createRequest.getJSON()); ctrl.execute(function () { var apiResponse = ctrl.getResponse(); var response = new ApiContracts.CreateCustomerPaymentProfileResponse(apiResponse); if (response != null) { if (response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK) { console.log('Successfully added payment profile with id: ' + response.getCustomerPaymentProfileId()); } else { console.log('Result Code: ' + response.getMessages().getResultCode()); console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode()); console.log('Error message: ' + response.getMessages().getMessage()[0].getText()); } } else { console.log('Null response received'); } callback(response); }); }
关键说明:
- 调用时需传入目标客户的
customerProfileId(即你创建客户时从响应中获取的response.getCustomerProfileId()值) - 测试用卡号可使用Authorize.net官方提供的测试卡段,上线环境需替换为真实用户的信用卡信息
- 验证模式
ValidationModeEnum可根据环境调整:测试环境用TESTMODE,生产环境可选择LIVE或NONE
内容的提问来源于stack exchange,提问作者Haris George
相关产品推荐
相关产品推荐

