title: Make a payment through PayPal API in ASP.NET Core
body: <p>I am doing a project in ASP.NET Core. My requirement is, I want to integrate PayPal  as the payment gateway. </p>
  
  <p>User can see set of items, with <strong>BUY NOW</strong> button, and when a button is clicked, i want to send that item's name and cost to paypal so it will charge the amount from the user.</p>
  
  <p>When the payment is successful, I want paypal to redirect to one of Controller method and save that transaction.</p>
  
  <p>Now my problem is,</p>
  
  <ol>
  <li>I am not sure if I am using the correct API.</li>
  <li>Even though I use that API, I get an error with 400 code</li>
  </ol>
  
  <p>I use below code to generate the token.</p>
  
  <pre><code>$.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',
              });
  </code></pre>
  
  <p>The above function is giving me a key and using that, i do the below API call. Now I am not sure if the below API is correct for this purpose.</p>
  
  <pre><code>$.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"
                  }
  </code></pre>
  
  <p><strong>Above API call is the exact example in the PayPal developer's website</strong> itself but however it gives me the following error:</p>
  
  <pre><code>{"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"}]}
  </code></pre>
  
  <p>Is this the correct API that I should use or is there any other? Please help me on this.</p>
  
  <p>Thank you.</p>