JezK
Edit File: paymentsCron.php
<?php include('../classes/class_users.php'); $users=new Users(); /* $imageCommission = $commisionRates['imageCommission']; $videoCommission = $commisionRates['videoCommission']; $productCommission = $commisionRates['productCommission'];*/ //echo "<pre>";print_r($orders);exit; $paypal_payment_fee_percent=2.9; // 2.9% $paypal_payment_fee_fixed=0.30; // $ 0.30 (0.30 Dollors) $paypal_masspay_fee_percent=2; // 2% $paypal_masspay_fee_max=1; // $ 1 (One Dollor) $currency = "usd"; $testing = true; if($testing) //sandbox { $clientID = "Aba6YOip9wUHZNTQ7EY1vyCrxBuDqmK4YH2RRAVYZTP6ZxF5KHnc_30MhHIxp96sh8whOUwBJOKfIM2e"; $secretKey = "ENbFvtI2x6NY6L629o6XRZ_eiaM9ebDCLnSa7Dw4-yEjfyHkvPbQb1eBbbqHmFXhF4kJn31rkfXT8wnU"; $paymentURL = "https://api.sandbox.paypal.com/v1/payments/payouts"; $accessTokenURL = "https://api.sandbox.paypal.com/v1/oauth2/token"; $paymentStatusURL = "https://api.sandbox.paypal.com/v1/payments/payouts/"; $paypalEnv = "sandbox"; } /*else //live { $clientID = "AXP5SPe7wR7HDNig8znBlaAOqQ6oZjEqQxGl_jRE3sCyaekUxl35IxDvVdoDvcZ6j4HE3-I0R-EYSxmj"; $secretKey = "ECn-7ZTy9ZoQylqchSAuu_vPSyvJROSWgQFeON2Dm1lvYwJq6McyFEBvS6UnPRkFkDvSmY3IxYUqNzJl"; $paymentURL = "https://api.paypal.com/v1/payments/payouts"; $accessTokenURL = "https://api.paypal.com/v1/oauth2/token"; $paymentStatusURL = "https://api.paypal.com/v1/payments/payouts/"; $paypalEnv = "production"; $configurationMode = "live"; }*/ //$ordersCount = count($orders); //for($i=0;$i<$ordersCount;$i++) //{ $orderProducts = $orders[$i]; $orderProductsCount = count($orderProducts); //for($j=0;$j<$orderProductsCount;$j++) //{ $items = array(); $AccessToken = getaccesstoken($db); //updating cron initial status $orderID = $orders[$i][$j]['orderID']; $orderProductID = $orderProducts[$j]['orderProductID']; $paypalEmailID = $orderProducts[$j]['paypalEmailID']; $merchantUserPaymentStatus = $orderProducts[$j]['merchantUserPaymentStatus']; //checking if user has paypalEmailID if($paypalEmailID) { //checking if payment already not done for order payment (0:not done,1-payment done) if($merchantUserPaymentStatus == 0) { echo "<li><span style='color:#800080'>Processing order product ".$orderProductID."</span></li>"; $amount = $orderProducts[$j]['amount']; $mediaProductName = $orderProducts[$j]['mediaProductName']; $netAmount = $amount; //calculating massPay which should not exceed $1 $masspay_fee = $netAmount*($paypal_masspay_fee_percent/(100+$paypal_masspay_fee_percent)); if($masspay_fee > $paypal_masspay_fee_max) //massPay exceeeds $1 { $masspay_fee = $paypal_masspay_fee_max; } $totalAmount = round($netAmount - $masspay_fee,2); $sender_batch_header = array('sender_batch_id' => time(), "email_subject"=>"You have a Payout Payment!"); $amount_obj= array("value"=>$totalAmount, "currency"=>"USD"); $items[] = array('recipient_type' => "EMAIL","amount"=>$amount_obj,"note" => "Payment for Media '.$mediaProductName.' of orderID ".$orderID,"sender_item_id"=>$orderProductID,"receiver"=>$paypalEmailID); $data = array("sender_batch_header"=>$sender_batch_header,"items"=>$items); $result = make_payment($data,$AccessToken,$orderID,$db); if(isset($result->name) && $result->name == 'INSUFFICIENT_FUNDS') { echo "<h3 style='color:red'>"; echo "</br>".$result->name; echo "</br>".$result->message; echo "</h3>"; exit; } $batch_header = $result->batch_header; $batch_status = $batch_header->batch_status; $payout_batch_id = $batch_header->payout_batch_id; $batch_daata = check_status($payout_batch_id,$AccessToken,$orderID,$db); if(($batch_status == 'PENDING' || $batch_status == 'SUCCESS') && $paypalEmailID != '') { echo "<li><span style='color:#0000FF'>Processed order product ".$orderProductID."</span></li>"; //updating payment status for orderProduct $db->updateOrderProductPaymentStatus($orderProductID,1); //updating cron status to success when all the order products are processed if($j == ($orderProductsCount-1)) { echo "<span style='color:green'>Processed order ".$orderID."</span></ul>"; $db->updateCronStatus($orderID,2); } } } } else { echo "</br><span style='color:red'>Paypal email not found for order ".$orderID."</span>"; break; } //} //} echo "</br>Order(s) processed successfully."; function make_payment($data,$AccessToken,$cart_id,$db){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $db->paymentURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: Bearer ".$AccessToken; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); $result_status = 'Error'; } //print_r($result); //print_r($result); $result = json_decode($result); curl_close ($ch); return $result; } function getaccesstoken($db){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $db->accessTokenURL); 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, $db->clientID. ":" .$db->secretKey); $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); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); return false; } $result = json_decode($result); curl_close ($ch); return $result->access_token; } function check_status($payout_batch_id,$AccessToken,$cart_id,$db) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $db->paymentStatusURL.$payout_batch_id."?fields=batch_header"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $headers = array(); $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: Bearer ".$AccessToken; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } $result = json_decode($result); curl_close ($ch); return $result; } /* * $AccessToken = getaccesstoken(); * $sender_batch_id = uniqid(); * $sender_batch_header = ['sender_batch_id' => $sender_batch_id, "email_subject"=>"You have a Payout Payment!"]; $amount_obj= ["value"=>$item_price_to_be_transferred_to_vendor, "currency"=>"USD"]; $items_obj = ['recipient_type' => "EMAIL","amount"=>$amount_obj,"note" => "Payment for ".$subtypeDetailsFull["hairtype_name"]."of cart Id ".$cart_id,"sender_item_id"=>$subtype_id,"receiver"=>$vendor_paypal_email]; $items[]=$items_obj; * $data = ["sender_batch_header"=>$sender_batch_header,"items"=>$items]; * $result = make_payment($data,$AccessToken,$cart_id); * $batch_header = $result->batch_header; * $batch_status = $batch_header->batch_status; * $payout_batch_id = $batch_header->payout_batch_id; * $batch_daata = check_status($payout_batch_id,$AccessToken,$cart_id); * if(($batch_status == 'PENDING' || $batch_status == 'SUCCESS') && $vendor_paypal_email != '') { * } * */ /*$db = new DbHandler(); $paypal_payment_cart_processing_type=1; $completedStatus=0; $cronTasks_cart_processing_paypal_payment=$db->getCronTasks($completedStatus,false,$paypal_payment_cart_processing_type); $AccessToken = getaccesstoken(); $result_status = ''; $sender_batch_id = uniqid(); if($cronTasks_cart_processing_paypal_payment != false) { foreach($cronTasks_cart_processing_paypal_payment as $cronTask) { $cronTaskId=$cronTask['cronTaskId']; $cronTaskDetails=json_decode($cronTask['json_data'],true); $cart_id=$cronTaskDetails['cart_id']; $cartDetails=$db->getCartDetails($cart_id); $cart_final_price=$cartDetails['final_price']; $cart_final_price_without_transaction_fee=$cart_final_price-$cartDetails['transaction_fee_value']; //$cart_paypal_transaction_fee=round($cart_final_price*$paypal_payment_fee_percent/100,2); $cart_paypal_transaction_fee=round($cart_final_price*$paypal_payment_fee_percent/100,2); //Creating payment records for orders $items=[]; foreach($cartDetails['cartItems'] as $cartItem) { $cart_item_id=$cartItem['cart_item_id']; $subtype_id=$cartItem['subtype_id']; $subtypeDetailsFull=$db->getSubtypeDetailsFull($subtype_id); $vendor_paypal_email=$subtypeDetailsFull['vendor_paypal_email']; $item_price=$cartItem['item_price']; //This is price of item_unit_price multiplied by quantity. $item_price=$item_price+$cartItem['shipping_price']; //Adding shipping price also //$vendor_paypal_email='jrdaman53-vendor-2@yahoo.com'; if($vendor_paypal_email != '') { //$paypal_transaction_fee_per_product=($item_price/$cart_final_price_without_transaction_fee)*$cart_paypal_transaction_fee; //$item_price_after_payment_fee=$item_price-$paypal_transaction_fee_per_product; // remove payment fee from item $item_price_after_payment_fee=$item_price; $masspay_fee=$item_price_after_payment_fee*($paypal_masspay_fee_percent/(100+$paypal_masspay_fee_percent)); //echo $item_price_after_payment_fee; if($masspay_fee > $paypal_masspay_fee_max) { $masspay_fee=$paypal_masspay_fee_max; } //echo 'reached'; $item_price_to_be_transferred_to_vendor=round($item_price_after_payment_fee-$masspay_fee,2); $sender_batch_header = ['sender_batch_id' => $sender_batch_id, "email_subject"=>"You have a Payout Payment!"]; $amount_obj= ["value"=>$item_price_to_be_transferred_to_vendor, "currency"=>"USD"]; $items_obj = ['recipient_type' => "EMAIL","amount"=>$amount_obj,"note" => "Payment for ".$subtypeDetailsFull["hairtype_name"]."of cart Id ".$cart_id,"sender_item_id"=>$subtype_id,"receiver"=>$vendor_paypal_email]; $items[]=$items_obj; /* $data = '{ "sender_batch_header":{ "sender_batch_id":"'.uniqid().'", "email_subject":"You have a Payout Payment!" }, "items":[ { "recipient_type":"EMAIL", "amount":{ "value":"'.$item_price_to_be_transferred_to_vendor.'", "currency":"USD" }, "note":"Payment for'.$subtypeDetailsFull["hairtype_name"].'of cart Id '.$cart_id.'", "sender_item_id":"'.$subtype_id.'", "receiver":"'.$vendor_paypal_email.'" } ] }';*/ //print_r($data); /* } } $data = ["sender_batch_header"=>$sender_batch_header,"items"=>$items]; //print_r(json_encode($data)); $db->payment_log(json_encode($data),$cart_id,'items'); $result = make_payment($data,$AccessToken,$cart_id); $result = json_encode($result); $db->payment_log($result,$cart_id,'after payment'); $result = json_decode($result); if($result) { $batch_header = $result->batch_header; $batch_status = $batch_header->batch_status; echo $payout_batch_id = $batch_header->payout_batch_id; echo "<br/>"; $batch_daata = check_status($payout_batch_id,$AccessToken,$cart_id); $batch_daata = json_encode($batch_daata); $db->payment_log($batch_daata,$cart_id,'status_checking'); $batch_daata = json_decode($batch_daata); $batch_status1 = $batch_daata->batch_header; $batch_status = $batch_status1->batch_status; echo $batch_status; foreach($cartDetails['cartItems'] as $cartItem) { $cart_item_id=$cartItem['cart_item_id']; $subtype_id=$cartItem['subtype_id']; $subtypeDetailsFull=$db->getSubtypeDetailsFull($subtype_id); $vendor_paypal_email=$subtypeDetailsFull['vendor_paypal_email']; if(($batch_status == 'PENDING' || $batch_status == 'SUCCESS') && $vendor_paypal_email != '') { $vendor_pay_type=1; // 1- paypal, 2-stripe $vendor_pay_status=1; $db->updateCartItemVendorPayStatus($cart_item_id,$vendor_pay_type,$vendor_pay_status,$payout_batch_id,$sender_batch_id); } } } } }*/ ?>