Integrating With PayPal API for Mobile Payments

PayPal is obviously one of the most popular services and APIs to use when you want to support payments in you mobile app. This tutorial will show you how to create a single payment using Appery.io Server Code. You can the use it as a starting point for all other APIs made available by PayPal.

If you read the PayPal documentation, the first step is to get a token so you can invoke PayPal APIs. Before you can get the token, you need to create an app in PayPal:

Screen Shot 2016-04-27 at 4.25.01 PM
Apps in PayPal

Every app has a Client ID and Secret values that you need to use to get a token.

Screen Shot 2016-04-27 at 4.31.49 PM
Client ID and Secret

The Server Code script below sends a request to PayPal to get a token:

var url = "https://api.sandbox.paypal.com/v1/oauth2/token";

var XHRResponse = XHR2.send("POST", url, {
  "parameters": {
    "grant_type": "client_credentials"
  },
  "headers": {
    "Accept": "application/json",
    "Accept-Language": "en_US",
    "Authorization": "Basic clientid:secret_as_base64"
});

Apperyio.response.success(XHRResponse.body, "application/json");

The response looks like this:

{
 "app_id": "APP-80W2.....",
 "access_token": "A101.VyA8xBdNyOY.....",
 "token_type": "Bearer",
 "scope": "https://uri.paypal.com/....",
 "nonce": "2016-04-27T23:15:21Zahq3.....",
 "expires_in": 31284
}

The access_token is what you need to invoke any other PayPal API.

Now that you have the token, you can invoke any PayPal API. In the next script I create a payment (charge):

var url = "https://api.sandbox.paypal.com/v1/payments/payment";

var XHRResponse = XHR2.send("POST", url, {
 "headers": {
    "Content-type": "application/json",
    "Authorization": "Bearer A101.VyA8xBdNyOY....."
 },
 "body": {
    "intent": "sale",
    "payer": {
       "payment_method": "credit_card",
       "funding_instruments": [{
          "credit_card": {
             "number": "4060xxxxxxxxxxxx",
             "type": "visa",
             "expire_month": 06,
             "expire_year": 2019,
             "cvv2": "874",
             "first_name": "Betsy",
             "last_name": "Buyer",
             "billing_address": {
                "line1": "111 First Street",
                "city": "Saratoga",
                "state": "CA",
                "postal_code": "95070",
                "country_code": "US"
             }
          }
       }]
    },
    "transactions": [{
       "amount": {
          "total": "7.47",
          "currency": "USD",
          "details": {
             "subtotal": "7.41",
             "tax": "0.03",
             "shipping": "0.03"
          }
       },
       "description": "This is the payment transaction description."
       }]
    }
});

Apperyio.response.success(XHRResponse.body, "application/json");

The payment example was adopted from PayPal API documentation

One important thing you need to do is use a valid credit card number from your test account. Without a valid credit card number, the request will not work. To find your credit card number go to Sandbox > Accounts, expand your account and click Profile. Select the Funding tab and use the credit card and expiration data from there for the test payment.

Once you have a working Server Code script that creates a payment in PayPal, you can use the script in your mobile app. A Server Code script is automatically exposed via a REST API which you can call from your app. We have many videos on our YouTube channel that show how to use Server Code and other Appery.io backend services.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s