DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Integrating With Paypal API for Mobile Payments

Integrating With Paypal API for Mobile Payments

OAuth 2 and REST APIs provide the keys to this straightforward example.

Max Katz user avatar by
Max Katz
·
May. 04, 16 · Mobile Zone · Tutorial
Like (1)
Save
Tweet
6.87K Views

Join the DZone community and get the full member experience.

Join For Free
PayPal is obviously one of the most popular services and APIs to use when you want to support payments in your 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

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 PMThe 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.

API mobile app

Published at DZone with permission of Max Katz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?
  • How to Utilize Python Machine Learning Models
  • 4 Different Ways to Work With Nebula Graph in Apache Spark
  • A Guide to Understanding Vue Lifecycle Hooks

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo