-1

I am new to Javascript and rest API. I am building a simple ecommerce site where sellers can list some items for the public to buy, so I am using paypal connected account as payment processing medium.

Paypal website say a restAPI should be call in this link

https://developer.paypal.com/docs/api/partner-referrals/#partner-referrals_create I have a paypal logo on the signup page, once the user clicks the logo I want it to call the API

they gave two steps on the page

Create partner referral

Show referral data

  Sample Request
 curl -v -X POST https://api.sandbox.paypal.com/v1/customer/partner-      referrals \
 -H "Content-Type: application/json" \
  -H "Authorization: Bearer Access-Token" \
 -d 

this is my paypal logo click handler

$('#paypal_img').click(function() {

      console.log("lets go to payment")


//API Call



})

sample customer data format from paypal site

    "customer_data": {
        "customer_type": "MERCHANT",
        "person_details": {
        "email_address": "customer@example.com",
        "name": {
        "prefix": "Mr.",
        "given_name": "Shashank",
        "surname": "Wankhede",
        "middle_name": "Govind"
    },

I tried to look through the tutorial but could not get much out of it How do I Call the paypal REST API for connected account in Javascript?

jone2
  • 191
  • 1
  • 4
  • 18

1 Answers1

0

You should be able to do a AJAX call in your click handler, something like this:

$('#paypal_img').click(function() {
    $.ajax({
        type: "POST",
        url: <PP API URL HERE>,
        data: {
            // Data goes here
        },
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Authorization", "<TOKEN HERE>")
        },
        success: function(data) {
            // Handle response data here
        },
        error: function() {
            // Handle errors here
        },
        dataType: 'json'
    });
})

Another (safer) way could be to do the call to PayPal on your back-end, to not expose to much to the client. Then you just do a normal AJAX call to initiate it on the front-end. All the credential handling and everything will then be handled by the back-end.

KungWaz
  • 1,918
  • 3
  • 36
  • 61
  • I added customer data format from paypal site, will the customer data go under data in the same exact format as posted above – jone2 Feb 15 '18 at 13:48
  • I think you need to look at the documentation about which data to send in the POST. If the data you added is the correct data, then yes, just send it as the data object in the AJAX call. – KungWaz Feb 16 '18 at 08:44