TrinityTuts

Create REST service to save user credit card info to paypal

Last updated on August 12th, 2015 at 11:49 pm

In this tutorial i will explain you to create a sample web service for application which require to save user credit card information on paypal. In my last tutorial i explain you that how we can create a web service if you don’t know or not yet read that post click here to read. Before we start creating this sample web service we need some paypal developer api key to make this service work. You need to create Paypal developer account to test this service .

Step to create Developer API Key in paypal

  1. Create a Paypal developer account at https://developer.paypal.com.
  2. Verify your email and login.
  3. Click on Applications link and create new app.
  4. After creating app you get your REST API CREDENTIALS to test your service.

After getting your REST api credentials you need to download paypal rest api sdk from github https://github.com/paypal/rest-api-sdk-php and also composer to run sample of this application from this link https://github.com/paypal/sdk-packages/raw/gh-pages/rest-api-sdk/php/rest-api-sdk-php-0.7.1.zip. Unzip {rest-api-sdk-php-0.7.1.zip} file and place the unzip folder inside the sdk -> sample . Now open your localhost or website where you place this folder -> sample. Most of the things are already done by this sdk. Now you need to add your paypal api key in this sdk open sample folder->bootsrap and add your client and secret key:

new OAuthTokenCredential(
			'<Client ID>',
			'<Secret>'
		)

Now open link: http://localhost/rest-api-sdk-php-master/sample/ and click  on execute in front of save credit card. By default there is sample information in backend which is send to paypal server and they return some Information like this:

Saved a new credit card with id: CARD-6FB49409VX484750UKNOUYIQ
object(PayPal\Api\CreditCard)#4 (1) {
  ["_propMap":"PayPal\Common\PPModel":private]=>
  array(13) {
    ["type"]=>
    string(4) "visa"
    ["number"]=>
    string(16) "xxxxxxxxxxxx0331"
    ["expire_month"]=>
    string(2) "11"
    ["expire_year"]=>
    string(4) "2019"
    ["cvv2"]=>
    string(3) "012"
    ["first_name"]=>
    string(3) "Joe"
    ["last_name"]=>
    string(7) "Shopper"
    ["id"]=>
    string(29) "CARD-6FB49409VX484750UKNOUYIQ"
    ["state"]=>
    string(2) "ok"
    ["valid_until"]=>
    string(20) "2017-04-26T00:00:00Z"
    ["create_time"]=>
    string(20) "2014-04-27T18:27:46Z"
    ["update_time"]=>
    string(20) "2014-04-27T18:27:46Z"
    ["links"]=>
    array(3) {
      [0]=>
      object(PayPal\Api\Links)#13 (1) {
        ["_propMap":"PayPal\Common\PPModel":private]=>
        array(3) {
          ["href"]=>
          string(81) "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-6FB49409VX484750UKNOUYIQ"
          ["rel"]=>
          string(4) "self"
          ["method"]=>
          string(3) "GET"
        }
      }
      [1]=>
      object(PayPal\Api\Links)#7 (1) {
        ["_propMap":"PayPal\Common\PPModel":private]=>
        array(3) {
          ["href"]=>
          string(81) "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-6FB49409VX484750UKNOUYIQ"
          ["rel"]=>
          string(6) "delete"
          ["method"]=>
          string(6) "DELETE"
        }
      }
      [2]=>
      object(PayPal\Api\Links)#8 (1) {
        ["_propMap":"PayPal\Common\PPModel":private]=>
        array(3) {
          ["href"]=>
          string(81) "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-6FB49409VX484750UKNOUYIQ"
          ["rel"]=>
          string(5) "patch"
          ["method"]=>
          string(5) "PATCH"
        }
      }
    }
  }
}
Back

Now our work start from here in above sample we get a card id eg.

CARD-6FB49409VX484750UKNOUYIQ 

Now with this id we can fetch user card information from paypal server and also delete user card information, so we need to save this information to our database and next time whenever user login to our website or purchase any thing then don’t need to add there card information again on any purchase.

Now we need a database where we can save this cardid.

You can download a sample code from here

Create database: paypal {you can name your database whatever you want.}

--
-- Database: `paypal`
--

-- --------------------------------------------------------

--
-- Table structure for table `cardinfo`
--

CREATE TABLE IF NOT EXISTS `cardinfo` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `userid` varchar(100) NOT NULL,
  `cardId` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Create connection to Database:

<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("paypal", $conn);

Now i am using Advance REST Client to request to my web service and create form in REST client as shown in image below

 

After request to webservice, webservice process your request and send user card information to paypal and receive a unique card id and we save that card id to our database with userid. If user information is saved you can get success response message from webservice.

Now if we want to fetch user card information from Paypal server we need to send CARD ID to paypal server and get response as shown in image below

 

and you get this response note that i send userid to service and with this user id i will fetch card id from database and send to paypal

To learn more about web service on paypal stay tune i am continuously try to update this post thank you.

Hapy coading.