PayPal is hottest payment gateway now a days, 60% of site owner using PayPal for exchange money. we can send money to PayPal using two method one is using PayPal form and another is using PayPal API CURL.
Developer can’t get PayPal payment API using CURL code easily in google or the other program so i just express my idea the way to make payment using API in PHP.
Before we start you will need a PayPal merchant account credential to proceed. So let’s create it first, here are the steps for this.
PayPal Developer Account
1. You must login with developer.paypal.com using your live PayPal account credential, define merchant credential.
1 2 |
$client = "YOUR CLIENT ID"; $secret= "YOUR CLIENT SECRET"; |
2. Define client test payment detail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//Define Payment Related Test Detail $email= "Yourpaypalemailid@gmail.com"; $fname= "Thecodehelpers"; $lname= "programming blog"; $address= "Street no 3 usa"; $city= "Wahington"; $country= "US"; $zip="99501"; $state="Alaska"; $phone="011554454"; $ccnum= "4012888888881881"; $credit_card_type= "visa"; $ccmo= "02"; $ccyr= "2022"; $cvv2_number= "123"; $first_name= $fname; $last_name= $lname; $cost= "2"; |
3. Make your first API call for access token.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ch = curl_init(); $client= $CLIENTID; $secret= $CLIENTSECRET; curl_setopt($ch, CURLOPT_URL, $PAYPAL_API_URL."v1/oauth2/token"); /*curl_setopt($ch, CURLOPT_URL, “https://api.paypal.com/v1/oauth2/token”);*/ curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $client.":".$secret); curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); $result = curl_exec($ch); if(empty($result))die("Error: No response."); else { $json = json_decode($result); /*print_r($json->access_token);*/ $accessToken=$json->access_token; } |
4. When you call above API, it returns an access token for payoff API call. So here we are storing access token into a variable for further usage or in exchange for those credentials, PayPal authorization server returns your access token within the access_token field.
5. Final API call for payment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
$ch = curl_init(); $data = '{ "intent": "sale", "payer": { "payment_method": "credit_card", "payer_info": { "email": "'.$email.'", "shipping_address": { "recipient_name": "'.$fname.' '.$lname.'", "line1": "'.$address.'", "city": "'.$city.'", "country_code": "'.$country.'", "postal_code": "'.$zip.'", "state": "'.$state.'", "phone": "'.$phone.'" }, "billing_address": { "line1": "'.$address.'", "city": "'.$city.'", "state": "'.$state.'", "postal_code": "'.$zip.'", "country_code": "'.$country.'", "phone": "'.$phone.'" } }, "funding_instruments": [{ "credit_card": { "number": "'. $ccnum.'", "type": "'.$credit_card_type.'", "expire_month": "'.$ccmo.'", "expire_year": "'.$ccyr.'", "cvv2": "'.$cvv2_number.'", "first_name": "'.$first_name.'", "last_name": "'.$last_name.'", "billing_address": { "line1": "'.$address.'", "city": "'.$city.'", "country_code": "'.$country.'", "postal_code": "'.$zip.'", "state": "'.$state.'", "phone": "'.$phone.'" } } }] }, "transactions": [{ "amount": { "total": "'.$cost.'", "currency": "GBP" }, "description": "This is member subscription payment at Thecodehelpers.", "item_list": { "shipping_address": { "recipient_name": "'.$fname.' '.$lname.'", "line1": "'.$address.'", "city": "'.$city.'", "country_code": "'.$country.'", "postal_code": "'.$zip.'", "state": "'.$state.'", "phone": "'.$phone.'" } } }] }'; curl_setopt($ch, CURLOPT_URL, $PAYPAL_API_URL."v1/payments/payment"); /*curl_setopt($ch, CURLOPT_URL, “https://api.paypal.com/v1/payments/payment”);*/ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: Bearer ".$json->access_token)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); //curl_close($ch); $json = json_decode($result); $state=$json->state; echo "<pre>" print_r($json); |
See above is our final API call base on access token. You can check your payment is gone success or not base on $state variable ($state=$json->state;).
Is return “Success” string once payment is completed other wise “Fail”. If you your payment is success you can insert data in database. Hope you learn some thing new from this post.
For more updates, subscribe to our newsletter service.
Excelent!..Thanks
Its’ my pleasure…! comment me if you want solution of any critical task..
Hi, i need this implement in my project. Can you support me . Paid task. Thanks
Yes why not can you please send me your skype id in my email thecodehelpers@gmail.com
so I can connect you
I need paypal paymnet gateway integration. Paid task.
yes i will help you please whatsapp(919033917643) or email me
stdClass Object ( [name] => UNAUTHORIZED_PAYMENT [message] => Unauthorized payment. [information_link] => https://developer.paypal.com/docs/api/payments/#errors [debug_id] => bdd78ae4d470d )
Need support. sandbox credential working, live not working.
I need your code or website so i can determine issue
Or you can directly contact to paypal support team also because sandbox is working so I thing there is not any bug in your code
Thanks for sharing your thoughts on legit cialis online.
Regards