CyberSource中使用存储支付方式的商户发起交易失败求助
问题描述
我正在使用CyberSource,需求如下:
- 使用PAN完成客户发起交易(CIT)支付;
- 存储支付方式以备后续使用;
- 使用存储的支付方式发起商户交易(MIT)。
我参考了官方文档中「Initial CIT Recurring Payment with a PAN」和「Subsequent MIT Recurring Payments」章节,严格按文档实现,但CIT交易成功且支付方式已存储后,后续MIT交易失败,报错:The credit card number is either missing or invalid。
以下是基于C#和CyberSource SDK的实现代码:
CIT(首次交易)代码
string clientReferenceInformationCode = Guid.NewGuid().ToString(); Ptsv2paymentsClientReferenceInformation clientReferenceInformation = new Ptsv2paymentsClientReferenceInformation( Code: clientReferenceInformationCode ); bool processingInformationCapture = true; string processingInformationPaymentSolution = "012"; Ptsv2paymentsProcessingInformation processingInformation = new Ptsv2paymentsProcessingInformation( Capture: processingInformationCapture, PaymentSolution: processingInformationPaymentSolution, AuthorizationOptions: new Ptsv2paymentsProcessingInformationAuthorizationOptions { Initiator = new Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiator { CredentialStoredOnFile = true, Type = "customer" }, }, CommerceIndicator: "internet" , RecurringOptions: new Ptsv2paymentsProcessingInformationRecurringOptions { FirstRecurringPayment = true } ); Ptsv2paymentsPaymentInformation paymentInformation = new Ptsv2paymentsPaymentInformation( FluidData: new Ptsv2paymentsPaymentInformationFluidData { Value = googlePayToken, } ); string orderInformationAmountDetailsTotalAmount = "1"; string orderInformationAmountDetailsCurrency = "USD"; Ptsv2paymentsOrderInformationAmountDetails orderInformationAmountDetails = new Ptsv2paymentsOrderInformationAmountDetails( TotalAmount: orderInformationAmountDetailsTotalAmount, Currency: orderInformationAmountDetailsCurrency ); string orderInformationBillToFirstName = "John"; string orderInformationBillToLastName = "Deo"; string orderInformationBillToAddress1 = "901 Metro Center Blvd"; string orderInformationBillToLocality = "Foster City"; string orderInformationBillToAdministrativeArea = "CA"; string orderInformationBillToPostalCode = "94404"; string orderInformationBillToCountry = "US"; string orderInformationBillToEmail = "test@googlepay.com"; string orderInformationBillToPhoneNumber = "6504327113"; Ptsv2paymentsOrderInformationBillTo orderInformationBillTo = new Ptsv2paymentsOrderInformationBillTo( FirstName: orderInformationBillToFirstName, LastName: orderInformationBillToLastName, Address1: orderInformationBillToAddress1, Locality: orderInformationBillToLocality, AdministrativeArea: orderInformationBillToAdministrativeArea, PostalCode: orderInformationBillToPostalCode, Country: orderInformationBillToCountry, Email: orderInformationBillToEmail, PhoneNumber: orderInformationBillToPhoneNumber ); Ptsv2paymentsOrderInformation orderInformation = new Ptsv2paymentsOrderInformation( AmountDetails: orderInformationAmountDetails, BillTo: orderInformationBillTo ); var requestObj = new CreatePaymentRequest( ClientReferenceInformation: clientReferenceInformation, ProcessingInformation: processingInformation, PaymentInformation: paymentInformation, OrderInformation: orderInformation ); try { var configDictionary = new Configuration().GetConfiguration(); var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary); var apiInstance = new PaymentsApi(clientConfig); PtsV2PaymentsPost201Response result = apiInstance.CreatePayment(requestObj); Console.WriteLine(result); return result; } catch (Exception e) { Console.WriteLine("Exception on calling the API : " + e.Message); return null; }
后续MIT交易代码
Ptsv2paymentsProcessingInformation processingInformation = new Ptsv2paymentsProcessingInformation( AuthorizationOptions: new Ptsv2paymentsProcessingInformationAuthorizationOptions { Initiator = new Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiator { StoredCredentialUsed = true, Type = "merchant", MerchantInitiatedTransaction = new Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction { Reason = "7", PreviousTransactionId = transactionId, } }, }, CommerceIndicator: "recurring", Capture: true ); Ptsv2paymentsPaymentInformation paymentInformation = new Ptsv2paymentsPaymentInformation( Card: new Ptsv2paymentsPaymentInformationCard { ExpirationYear = card.ExpirationYear, ExpirationMonth = card.ExpirationMonth, Number = card.Prefix + "xxxxxxxxxxxx" } ); string clientReferenceInformationCode = Guid.NewGuid().ToString(); Ptsv2paymentsClientReferenceInformation clientReferenceInformation = new Ptsv2paymentsClientReferenceInformation( Code: clientReferenceInformationCode ); var requestObj = new CreatePaymentRequest( ClientReferenceInformation: clientReferenceInformation, ProcessingInformation: processingInformation, OrderInformation: orderInformation, PaymentInformation: paymentInformation ); PtsV2PaymentsPost201Response result = apiInstance.CreatePayment(requestObj);
问题排查与解决方案
核心问题分析
- 错误使用掩码卡号:MIT交易中传入的掩码卡号(
card.Prefix + "xxxxxxxxxxxx")无法被CyberSource识别为有效支付凭证,系统判定卡号缺失或无效。 - 缺少关键参数:MIT交易未传入
PaymentSolution: "012",该参数是标识存储凭证交易的必要字段,需与CIT交易保持一致。 - 凭证复用方式错误:MIT交易应使用CIT返回的**支付令牌(Payment Token)**而非手动拼接的卡号,同时需确保
PreviousTransactionId是CIT交易的真实ID(而非客户端参考码)。
修正后的MIT交易代码
// 从CIT交易成功结果中获取存储的支付令牌和原始交易ID string storedPaymentToken = result.PaymentInformation.Token.Id; string originalTransactionId = result.Id; Ptsv2paymentsProcessingInformation processingInformation = new Ptsv2paymentsProcessingInformation( Capture: true, PaymentSolution: "012", // 与CIT交易保持一致,标识存储凭证交易 AuthorizationOptions: new Ptsv2paymentsProcessingInformationAuthorizationOptions { Initiator = new Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiator { StoredCredentialUsed = true, Type = "merchant", MerchantInitiatedTransaction = new Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction { Reason = "7", // 对应定期 recurring 交易类型 PreviousTransactionId = originalTransactionId, } }, }, CommerceIndicator: "recurring" ); // 使用支付令牌替代掩码卡号 Ptsv2paymentsPaymentInformation paymentInformation = new Ptsv2paymentsPaymentInformation( Token: new Ptsv2paymentsPaymentInformationToken { Id = storedPaymentToken } ); string clientReferenceInformationCode = Guid.NewGuid().ToString(); Ptsv2paymentsClientReferenceInformation clientReferenceInformation = new Ptsv2paymentsClientReferenceInformation( Code: clientReferenceInformationCode ); // 确保orderInformation包含正确的金额、货币及账单信息 var requestObj = new CreatePaymentRequest( ClientReferenceInformation: clientReferenceInformation, ProcessingInformation: processingInformation, OrderInformation: orderInformation, PaymentInformation: paymentInformation ); PtsV2PaymentsPost201Response result = apiInstance.CreatePayment(requestObj);
关键注意事项
- 支付令牌存储:CIT交易成功后,务必保存返回的
PaymentInformation.Token.Id,这是后续MIT交易唯一合法的存储凭证标识。 - 参数一致性:
PaymentSolution参数在CIT和MIT交易中必须相同,否则系统无法关联存储的凭证。 - 交易ID验证:
PreviousTransactionId必须是CIT交易返回的result.Id,而非自定义的客户端参考码。 - 避免存储卡号:永远不要存储完整或掩码卡号,使用支付令牌是合规且安全的方式。
内容的提问来源于stack exchange,提问作者ilito
相关产品推荐
相关产品推荐

