3

我最初将这个问题发布为对不同 SO 线程的评论(旧:PayPal REST API .net SDK - 400 Bad Requests)但由于我没有看到任何回复,我认为问题可能是最好的。

在不自己更改 RestAPISDK 的情况下,有没有办法让它在发生验证错误时将错误消息返回给我的代码?现在我唯一的做法是阅读日志文件以查看发生了什么,但是当我试图向用户报告为什么像卡号无效这样简单的事情收费失败时,这对我没有好处,它已经过期了,等等。现在我们得到了一个 400 错误请求,但是由于记录了失败,API 显然看到了发生的事情,只是没有报告它。

这是我正在使用的代码 - 当我执行 payment.Create(apiContext) 时出现错误。

var card = new PayPal.Api.Payments.CreditCard
        {
            billing_address = new PayPal.Api.Payments.Address
            {
                city = customer.BillingAddress.City,
                country_code = customer.BillingAddress.Country,
                line1 = customer.BillingAddress.Address1,                    
                postal_code = customer.BillingAddress.PostalCode,
                state = customer.BillingAddress.StateProvince
            },
            cvv2 = customer.CreditAccount.SecurityCode,
            expire_month = customer.CreditAccount.ExpirationMonth,
            expire_year = customer.CreditAccount.ExpirationYear,
            first_name = customer.FirstName,
            last_name = customer.LastName,
            number = customer.CreditAccount.CardNumber
        };

        //only send line 2 if it exists, otherwise it'll error out
        if (!string.IsNullOrEmpty(customer.BillingAddress.Address2))
            card.billing_address.line2 = customer.BillingAddress.Address2;

        switch (customer.CreditAccount.CardType)
        {
            case CardTypes.AmericanExpress:
                card.type = "amex";
                break;

            default:
                card.type = customer.CreditAccount.CardType.ToString().ToLower();
                break;
        }

        var amt = new PayPal.Api.Payments.Amount
        {
            currency = "USD",
            details = new PayPal.Api.Payments.Details
            {
                shipping = Math.Round(customer.CreditAccount.Shipping, 2).ToString(),
                subtotal = Math.Round(customer.CreditAccount.Amount, 2).ToString(),
                tax = Math.Round(customer.CreditAccount.Tax, 2).ToString()
            },
            total = Math.Round(customer.CreditAccount.GrandTotal, 2).ToString()
        };

        var payer = new PayPal.Api.Payments.Payer
        {
            funding_instruments = (new FundingInstrument[] { new FundingInstrument { credit_card = card } }).ToList(),
            payment_method = "credit_card"
        };

        var payment = new Payment
        {
            intent = "sale",
            payer = payer,
            transactions = (new Transaction[] {
                new Transaction { description = customer.CreditAccount.Description, amount = amt }
            }).ToList()
        };

        try
        {
            var accessToken = new PayPal.OAuthTokenCredential(this.Configuration.ClientID, this.Configuration.ClientSecret).GetAccessToken();
            var apiContext = new PayPal.APIContext(accessToken);

            var result = payment.Create(apiContext);

            if (result.state == "approved")
                creditResponse = new CreditResponse { AuthorizationCode = result.id, Status = CreditResponseTypes.Success, TransactionId = result.id };
            else
                creditResponse = new CreditResponse { Status = CreditResponseTypes.Declined };
        }
        catch (Exception ex)
        {
            creditResponse = new CreditResponse
            {
                Message = ex.ToString(),
                Status = CreditResponseTypes.Error
            };
        }

编辑:添加示例代码

4

0 回答 0