Created
March 25, 2019 13:21
-
-
Save ziggybaba1/a6c35e5c2d069a6fcfb5dfe6366a7a9d to your computer and use it in GitHub Desktop.
Comprehensive PHP Script to verify paypal and Wepay payment using curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
class PaypalController extends Controller | |
{ | |
public function validatr($paymentID, $paymentToken, $payerID){ | |
$paypalEnv='sandbox'; // Or 'production' | |
if(getenv('client_status')=='production'){ | |
$paypalURL='https://api.paypal.com/v1/'; // URL of the call | |
$paypalClientID=getenv('production_client_id'); | |
$paypalSecret=getenv('production_client_secret'); | |
} | |
else{ | |
$paypalURL='https://api.sandbox.paypal.com/v1/'; | |
$paypalClientID=getenv('sandbox_client_id'); | |
$paypalSecret=getenv('sandbox_client_secret'); | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $paypalURL.'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, $paypalClientID.":".$paypalSecret); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
if(empty($response)){ | |
return false; | |
}else{ | |
$jsonData = json_decode($response); | |
$curl = curl_init($paypalURL.'payments/payment/'.$paymentID); | |
curl_setopt($curl, CURLOPT_POST, false); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: Bearer ' . $jsonData->access_token, | |
'Accept: application/json', | |
'Content-Type: application/xml' | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
// Transaction data | |
$result = json_decode($response); | |
return $result; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
public function wepayurl (Request $request){ | |
$account_id = getenv('WEPAY_ACCOUNT_ID'); | |
$client_id = getenv('WEPAY_CLIENT_ID'); | |
$client_secret = getenv('WEPAY_CLIENT_SECRET'); | |
$access_token = getenv('WEPAY_ACCESS_TOKEN'); | |
$amount=0; | |
$data = array( | |
"account_id" => $account_id, | |
"amount" => $amount, | |
"short_description"=>"Short Description of Payment", | |
"type"=>"service", | |
"currency"=>"USD", | |
'hosted_checkout' => ['mode' => 'iframe','redirect_uri' => url('')."/payment/redirect"] | |
$data = json_encode($data); | |
if(getenv('WEPAY_CLIENT_STATUS')=='production'){ | |
$ch = curl_init('https://wepayapi.com/v2/checkout/create'); // URL of the call | |
} | |
else{ | |
$ch = curl_init('https://stage.wepayapi.com/v2/checkout/create'); // URL of the call | |
} | |
CURL_SETOPT($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Authorization: Bearer ". $access_token)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8'); | |
// execute the api call | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment