In this technology world paypal is one of great way to send or receive money online, It provides a lot of feature to ease and make reliable your business scope.You just need to login and there you can send payments over the globe,Now Paypal is going to accept credit cards just like other merchant account providers
Now I am going to show you step by step learning how to process the credit cards by using Paypal Pro, Its simple by using Paypal web service.
Now I am going to show you step by step learning how to process the credit cards by using Paypal Pro, Its simple by using Paypal web service.
Step 1: How Directory Structure should be
Step 2: Setup the Config File
Set Up your configuration file....
1
2
3
4
5
6
7
8
9
| // Set sandbox (test mode) to true/false. $sandbox = TRUE; // Set PayPal API version and credentials. $api_version = '85.0' ; $api_endpoint = $sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp' ; $api_username = $sandbox ? 'SANDBOX_USERNAME_GOES_HERE' : 'LIVE_USERNAME_GOES_HERE' ; $api_password = $sandbox ? 'SANDBOX_PASSWORD_GOES_HERE' : 'LIVE_PASSWORD_GOES_HERE' ; $api_signature = $sandbox ? 'SANDBOX_SIGNATURE_GOES_HERE' : 'LIVE_SIGNATURE_GOES_HERE' ; |
In above mentioned code you will see $sandbox is TRUE , Its mean we are integrating it within testing mode , so when you put your code live turn it into FALSE.
On the base of $sandbox you will have to fill other placeholders so after that we have a ready piece of code to connect with api ..
Step 3: Configure API Request
NOw include your configuration file in this way :
1
2
| // Include config file require_once ( 'includes/config.php' ); |
Now you need to set name-value-pair that contain all data which we need to send paypal to process our payment, there are URL parameters so it make sure all parameter in Caps
1
| PARAM1=value1&PARAM2=value2&PARAM3=value3…etc. |
Now maybe you are confused how we can set this complicated string so don't be worry we will set required parameters into array then we can generate NVP string.
METHOD
– The name of the API call you’re making.USER
– The API usernamePWD
– The API passwordSIGNATURE
– The API signatureVERSION
– The API version
Now below is the array in accurate form which we will send to paypal for processing
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| // Store request params in an array $request_params = array ( 'METHOD' => 'DoDirectPayment' , 'USER' => $api_username , 'PWD' => $api_password , 'SIGNATURE' => $api_signature , 'VERSION' => $api_version , 'PAYMENTACTION' => 'Sale' , 'IPADDRESS' => $_SERVER [ 'REMOTE_ADDR' ], 'CREDITCARDTYPE' => 'MasterCard' , 'ACCT' => '5522340006063638' , 'EXPDATE' => '022013' , 'CVV2' => '456' , 'FIRSTNAME' => 'Tester' , 'LASTNAME' => 'Testerson' , 'STREET' => '707 W. Bay Drive' , 'CITY' => 'Largo' , 'STATE' => 'FL' , 'COUNTRYCODE' => 'US' , 'ZIP' => '33770' , 'AMT' => '100.00' , 'CURRENCYCODE' => 'USD' , 'DESC' => 'Testing Payments Pro' ); |
Now we can simply loop through this array to generate our NVP string.
1
2
3
4
5
6
| // Loop through $request_params array to generate the NVP string. $nvp_string = '' ; foreach ( $request_params as $var => $val ) { $nvp_string .= '&' . $var . '=' .urlencode( $val ); } |
The value of
$nvp_string
is now:
1
| METHOD=DoDirectPayment&USER=sandbo*****e.com&PWD=12***74&SIGNATURE=AiKZ******6W18v&VERSION=85.0&PAYMENTACTION=Sale&IPADDRESS=72.135.111.9&CREDITCARDTYPE=MasterCard&ACCT=5522340006063638&EXPDATE=022013&CVV2=456&FIRSTNAME=Tester&LASTNAME=Testerson&STREET=707+W.+Bay+Drive&CITY=Largo&STATE=FL&COUNTRYCODE=US&ZIP=33770&AMT=100.00&CURRENCYCODE=USD&DESC=Testing+Payments+Pro |
This string is what we'll send to PayPal for our request.
Step 4: Send the HTTP Request to PayPal
Now we will use CURL method to send string to the Paypal server....
01
02
03
04
05
06
07
08
09
10
11
| // Send NVP string to PayPal and store response $curl = curl_init(); curl_setopt( $curl , CURLOPT_VERBOSE, 1); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $curl , CURLOPT_TIMEOUT, 30); curl_setopt( $curl , CURLOPT_URL, $api_endpoint ); curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); curl_setopt( $curl , CURLOPT_POSTFIELDS, $nvp_string ); $result = curl_exec( $curl ); curl_close( $curl ); |
If the call will be succeeded you will get result in $result variable, whether or not the call succeeded or not.
Step 5: Parse the API Response
The value that we get back in
$result
from the previous step will be an NVP string just like the one we generated and sent to PayPal. When we run our current script we get a successful response back that looks like this:
1
| TIMESTAMP=2012%2d04%2d16T07%3a59%3a36Z&CORRELATIONID=9eb40cd84a7d3&ACK=Success&VERSION=85%2e0&BUILD=2764190&AMT=100%2e00&CURRENCYCODE=USD&AVSCODE=X&CVV2MATCH=M&TRANSACTIONID=160896645A8111040 |
One very simple way to parse this result is to use PHP’s
parse_str()
function. This will load all of the response data into PHP variables matching the names and values returned in the response. For example, if we do the following:
1
2
| // Parse the API response $nvp_response_array = parse_str ( $result ); |
We would end up with access to the following PHP variables:
$TIMESTAMP
$CORRELATIONID
$ACK
$VERSION
$BUILD
$AMT
$CURRENCYCODE
$AVSCODE
$CVV2MATCH
$TRANSACTIONID
We can then proceed to use these variables to present information back to our customer, populate values in email receipts we’d like to generate, update database information, or anything else we need to do once an order is completed.
The
$ACK
value is what will tell us whether or not the API call was successful or not. Values for $ACK
can be:Success
SuccessWithWarning
Failure
FailureWithWarning
$TIMESTAMP
$CORRELATIONID
$ACK
$VERSION
$BUILD
$L_ERRORCODE0
$L_SHORTMESSAGE0
$L_LONGMESSAGE0
$L_SEVERITYCODE0
$AMT
$CURRENCYCODE
You can simply redirect your user where they need to go and show them information based on this value.
A failing API call will result in additional parameters that provide information about why the transaction failed. If I run this test again with an invalid credit card number, for example, I get the following response back from PayPal:
1
| TIMESTAMP=2012%2d04%2d16T08%3a08%3a52Z&CORRELATIONID=590d41dbb31e0&ACK=Failure&VERSION=85%2e0&BUILD=2764190&L_ERRORCODE0=10527&L_SHORTMESSAGE0=Invalid%20Data&L_LONGMESSAGE0=This%20transaction%20cannot%20be%20processed%2e%20Please%20enter%20a%20valid%20credit%20card%20number%20and%20type%2e&L_SEVERITYCODE0=Error&AMT=100%2e00&CURRENCYCODE=USD |
Now, when we use
parse_str()
we end up with the following PHP variables available to us:
In this case,
$ACK
shows a Failure so we know the call did not succeed and we can check the error parameters for more details about what went wrong.Additional Data Parsing Option
While the previous method of parsing the response works just fine, I personally prefer to work with data arrays. As such, I use the following function to convert the PayPal response into an array.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
| // Function to convert NTP string to an array function NVPToArray( $NVPString ) { $proArray = array (); while ( strlen ( $NVPString )) { // name $keypos = strpos ( $NVPString , '=' ); $keyval = substr ( $NVPString ,0, $keypos ); // value $valuepos = strpos ( $NVPString , '&' ) ? strpos ( $NVPString , '&' ): strlen ( $NVPString ); $valval = substr ( $NVPString , $keypos +1, $valuepos - $keypos -1); // decoding the respose $proArray [ $keyval ] = urldecode( $valval ); $NVPString = substr ( $NVPString , $valuepos +1, strlen ( $NVPString )); } return $proArray ; } |
This allows me to see all of the response data available by simply looking at the contents of the array:
If I run my script again now I get the following result on screen:
01
02
03
04
05
06
07
08
09
10
11
12
13
| Array ( [TIMESTAMP] => 2012-04-16T08:15:41Z [CORRELATIONID] => 9a652cbabfdd9 [ACK] => Success [VERSION] => 85.0 [BUILD] => 2764190 [AMT] => 100.00 [CURRENCYCODE] => USD [AVSCODE] => X [CVV2MATCH] => M [TRANSACTIONID] => 6VR832690S591564M ) |
And If I were to cause an error again I get the following:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
| Array ( [TIMESTAMP] => 2012-04-16T08:18:46Z [CORRELATIONID] => 2db182b912a9 [ACK] => Failure [VERSION] => 85.0 [BUILD] => 2764190 [L_ERRORCODE0] => 10527 [L_SHORTMESSAGE0] => Invalid Data [L_LONGMESSAGE0] => This transaction cannot be processed. Please enter a valid credit card number and type. [L_SEVERITYCODE0] => Error [AMT] => 100.00 [CURRENCYCODE] => USD ) |
You can see this is a nice, easy to navigate result array that contains everything we might need to move the user through our application and update data sources as necessary.
Conclusion
As you can see, processing credit cards using PayPal Payments Pro is actually a very simple procedure. It just involves a few standard steps for working with API web services, and a basic knowledge of working with array data can help as well.
Good luck, and happy coding!
No comments:
Post a Comment