.NET Core项目PayPal REST API信用卡支付报错:收款账户无效
问题:PayPal REST API信用卡支付提示"Payee account is invalid"错误
在.NET Core项目中使用PayPal REST API 1.9.1,已创建沙箱账户,调用/v1/payments/payment端点进行信用卡支付时出现"Payee account is invalid"错误。切换为PayPal支付方式仅需少量修改即可正常运行,如何让代码支持信用卡支付?
原代码片段
string accessToken = new PayPal.Api.OAuthTokenCredential(config).GetAccessToken(); var apiContext = new PayPal.Api.APIContext(accessToken); // A transaction defines the contract of a payment. var transaction = new PayPal.Api.Transaction() { amount = new PayPal.Api.Amount() { currency = "USD", total = totalAmount, details = new PayPal.Api.Details() { shipping = shipping, subtotal = subTotal, tax = tax } }, description = description, item_list = new PayPal.Api.ItemList() { items = new List<PayPal.Api.Item>() { new PayPal.Api.Item() { name = itemName, currency = "USD", price = "1", quantity = "5", sku = "sku" } }, shipping_address = new PayPal.Api.ShippingAddress { city = "Johnstown", country_code = "US", line1 = "52 N Main ST", postal_code = "43210", state = "OH", recipient_name = "Basher Buyer" } }, invoice_number = invoice_number }; // A resource representing a Payer that funds a payment. var payer = new PayPal.Api.Payer() { payment_method = "credit_card", funding_instruments = new List<PayPal.Api.FundingInstrument>() { new PayPal.Api.FundingInstrument() { credit_card = new PayPal.Api.CreditCard() { billing_address = new PayPal.Api.Address() { city = "Johnstown", country_code = "US", line1 = "52 N Main ST", postal_code = "43210", state = "OH" }, cvv2 = card.CVV, expire_month = Convert.ToInt32(card.ExpirationMonth), expire_year = Convert.ToInt32(card.ExpirationYear), first_name = firstName, last_name = lastName, number = card.CardNumber, type = card.CardType } } }, payer_info = new PayPal.Api.PayerInfo { email = "test@gmail.com" }, }; // A Payment resource; create one using the above types and intent as `sale`, 'order', 'none' or `authorize` var payment = new PayPal.Api.Payment() { intent = "sale", payer = payer, transactions = new List<PayPal.Api.Transaction>() { transaction } }; // Create a payment using a valid APIContext var createdPayment = payment.Create(apiContext);
解决方案
- 启用沙箱商家账户的Payments Pro权限:信用卡直连支付需要商家账户开通PayPal Payments Pro功能。登录PayPal开发者平台,进入沙箱账户管理页面,找到你的商家账户,检查并启用该权限。
- 校验API配置的账户身份:确保
config中的客户端ID和密钥属于已开启Payments Pro的沙箱商家账户,而非个人买家账户。 - 移除冗余的payer_info.email字段:使用
credit_card支付方式时,该字段非必填,且可能触发账户校验错误,建议删除该部分代码。 - 使用PayPal官方测试信用卡:沙箱环境必须使用官方提供的测试卡号,例如Visa测试卡:
4111111111111111,CVV填任意3位,有效期选择未来年份和月份。 - 确保金额计算准确:
total必须等于subtotal + shipping + tax的总和,PayPal对金额校验严格,数值不匹配会导致请求失败。
修改后的关键代码片段(移除payer_info)
var payer = new PayPal.Api.Payer() { payment_method = "credit_card", funding_instruments = new List<PayPal.Api.FundingInstrument>() { new PayPal.Api.FundingInstrument() { credit_card = new PayPal.Api.CreditCard() { billing_address = new PayPal.Api.Address() { city = "Johnstown", country_code = "US", line1 = "52 N Main ST", postal_code = "43210", state = "OH" }, cvv2 = card.CVV, expire_month = Convert.ToInt32(card.ExpirationMonth), expire_year = Convert.ToInt32(card.ExpirationYear), first_name = firstName, last_name = lastName, number = card.CardNumber, type = card.CardType } } } };
内容的提问来源于stack exchange,提问作者Tarlok
相关产品推荐
相关产品推荐

