In our last post, we have seen how to get Paypal App Id and App Secret, which is necessary to access PayPal PHP Merchant SDK.
Follow that post to learn how to get the credentials and put it in Configuration.php file of this project to setup the required settings.
Then you will be ready to setup the project.
PHP Code:
Here is the Configuration
define('PAYPAL_CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PAYPAL_SECRATE_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
Here is the Integration code
// Get access token from PayPal client Id and secrate key
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ttps://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, PAYPAL_CLIENT_ID . ":" . PAYPAL_SECRATE_KEY);
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Accept-Language: en_US";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$results = curl_exec($ch);
$getresult = json_decode($results);
// PayPal Payout API for Send Payment from PayPal to PayPal account
curl_setopt($ch, CURLOPT_URL, "ttps://api.sandbox.paypal.com/v1/payments/payouts");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$array = array('sender_batch_header' => array(
"sender_batch_id" => time(),
"email_subject" => "You have a payout!",
"email_message" => "You have received a payout."
),
'items' => array(array(
"recipient_type" => "EMAIL",
"amount" => array(
"value" => '100',
"currency" => "USD"
),
"note" => "Thanks for the payout!",
"sender_item_id" => time(),
"receiver" => 'paypalemail@xxx.com'
))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer $getresult->access_token";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$payoutResult = curl_exec($ch);
//print_r($result);
$getPayoutResult = json_decode($payoutResult);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($getPayoutResult);