首页 > 解决方案 > 通过 ASP.NET Core 中的 PayPal API 进行付款

问题描述

我在 ASP.NET Core 中做一个项目。我的要求是,我想将 PayPal 集成为支付网关。

用户可以通过“立即购买”按钮查看一组项目,当单击一个按钮时,我想将该项目的名称和费用发送到贝宝,这样它将向用户收取费用。

付款成功后,我希望贝宝重定向到控制器方法之一并保存该交易。

现在我的问题是,

  1. 我不确定我是否使用了正确的 API。
  2. 即使我使用该 API,我也会收到 400 代码错误

我使用下面的代码来生成令牌。

$.ajax({
            url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
            datatype: 'json',
            type: 'POST',
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Basic ' + btoa('client ID and secret'));
            },
            data: {
                'grant_type': 'client_credentials',
            });

上面的函数给了我一个密钥并使用它,我执行下面的 API 调用。现在我不确定下面的 API 是否适用于此目的。

$.ajax({
            url: 'https://api.sandbox.paypal.com/v1/payments/payment',
            type: 'POST',
            headers: {
                'Content-Type':'application/json',
                'Authorization': 'Bearer ' + accessToken
            },
            data: {
                "intent": "sale",
                "payer": {
                    "payment_method": "paypal"
                },
                "transactions": [
                    {
                        "amount": {
                            "total": "30.11",
                            "currency": "USD",
                            "details": {
                                "subtotal": "30.00",
                                "tax": "0.07",
                                "shipping": "0.03",
                                "handling_fee": "1.00",
                                "shipping_discount": "-1.00",
                                "insurance": "0.01"
                            }
                        },
                        "description": "The payment transaction description.",
                        "custom": "EBAY_EMS_90048630024435",
                        "invoice_number": "48787589673",
                        "payment_options": {
                            "allowed_payment_method": "INSTANT_FUNDING_SOURCE"
                        },
                        "soft_descriptor": "ECHI5786786",
                        "item_list": {
                            "items": [
                                {
                                    "name": "hat",
                                    "description": "Brown hat.",
                                    "quantity": "5",
                                    "price": "3",
                                    "tax": "0.01",
                                    "sku": "1",
                                    "currency": "USD"
                                }
                            ],
                            "shipping_address": {
                                "recipient_name": "Brian Robinson",
                                "line1": "4th Floor",
                                "line2": "Unit #34",
                                "city": "San Jose",
                                "country_code": "US",
                                "postal_code": "95131",
                                "phone": "011862212345678",
                                "state": "CA"
                            }
                        }
                    }
                ],
                "note_to_payer": "Contact us for any questions on your order.",
                "redirect_urls": {
                    "return_url": "https://example.com/return",
                    "cancel_url": "https://example.com/cancel"
                }

上面的 API 调用是 PayPal 开发人员网站本身的确切示例,但是它给了我以下错误:

{"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]}

这是我应该使用的正确 API 还是有其他 API?请帮助我。

谢谢你。

标签: ajaxauthenticationasp.net-corepaypalpayment-gateway

解决方案


推荐阅读