我使用 curl 将资金从企业转移到几个个人用户。
通过循环,我找到必须接收付款的用户,并通过 curl POST 请求和 php 我进行付款。
付款返回一个带有 payout_batch_id 的响应,并用这个我做一个 curl GET 请求来获取事务 id,但是如果我不让 php 睡眠将近 5 秒,我有响应挂起而不是成功。我怎样才能确定付款是否真的做到了?
这是我的代码:
###########################################################################################
# PAYOUT FROM BUSINESS TO PERSONAL #
###########################################################################################
private function curl_request($url, $method, $headers = [], $data = [], $curl_options = []){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//--- If any headers set add them to curl request
if(!empty($headers)){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
//--- Set the request type , GET, POST, PUT or DELETE
switch($method){
case "POST":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
}
//--- If any data is supposed to be send along with request add it to curl request
if($data){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
//--- Any extra curl options to add in curl object
if($curl_options){
foreach($curl_options as $option_key => $option_value){
curl_setopt($curl, $option_key, $option_value);
}
}
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
//--- If curl request returned any error return the error
if ($error) {
return "CURL Error: $error";
}
//--- Return response received from call
return $response;
}
public function get_access_token()
{
//--- Headers for our token request
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
//--- Data field for our token request
$data = "grant_type=client_credentials";
//--- Pass client id & client secrent for authorization
$curl_options[CURLOPT_USERPWD] = $this->CI->config->item('client_id') . ":" . $this->CI->config->item('client_secret');
$token_request = $this->curl_request($this->CI->config->item('PAYPAL_TOKEN_URL'), "POST", $headers, $data, $curl_options);
$token_request = json_decode($token_request);
if(isset($token_request->error)){
$result['no_error'] = FALSE;
$result['message_error'] = $token_request->error_description;
}else{
$result['no_error'] = TRUE;
$result['token_request'] = $token_request ;
}
return $result;
}
function transfer_money_from_business_to_personal($email_receiver,$cifra)
{
$this->log_ipn_test('########################## NUOVO TENTATIVO DI PAGAMENTO ############################## ');
if ($email_receiver=='') {
$this->log_ipn_test('Mail vuota impossibile pagare');
die();
}
//restituisce un array per poter inserire nel caso gli errori
$result= $this->get_access_token();
if($result['no_error']){
$token_request = $result['token_request'];
$this->log_ipn_test('Token Ok');
$headers = $data = [];
//--- Headers for payout request
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer $token_request->access_token";
$time = time();
//--- Prepare sender batch header
$sender_batch_header["sender_batch_id"] = $time;
$sender_batch_header["email_subject"] = "Payout Received";
$sender_batch_header["email_message"] = "You have received a payout, Thank you for using our services";
//--- First receiver
$receiver["recipient_type"] = "EMAIL";
$receiver["note"] = "Thank you for your services";
$receiver["sender_item_id"] = $time++;
$receiver["receiver"] = $email_receiver;
$receiver["amount"]["value"] = $cifra;
$receiver["amount"]["currency"] = "EUR";
$items[] = $receiver;
/*
//--- Second receiver
$receiver["recipient_type"] = "EMAIL";
$receiver["note"] = "You received a payout for your services";
$receiver["sender_item_id"] = $time++;
$receiver["receiver"] = "buyer2@example.com";
$receiver["amount"]["value"] = 15.00;
$receiver["amount"]["currency"] = "USD";
$items[] = $receiver;
*/
$data["sender_batch_header"] = $sender_batch_header;
$data["items"] = $items;
//--- Send payout request
$payout = $this->curl_request($this->CI->config->item('PAYPAL_PAYOUTS_URL'), "POST", $headers, json_encode($data));
$payout = json_decode($payout);
//$this->log_ipn_test("|2|$payout[0]['batch_header']");
//$this->log_ipn_test("|2|$payout[0]['batch_header']['payout_batch_id']");
$headers2 = $data2 = [];
//--- Headers for payment description
$headers2[] = "Content-Type: application/json";
$headers2[] = "Authorization: Bearer $token_request->access_token";
$link = $payout->links[0]->href;
$this->log_ipn_test($link);
sleep(3); //sleep to prevent PENDING
$dati_pagamento = $this->curl_request($link, "GET", $headers2);
$this->log_ipn_test(json_encode($dati_pagamento));
var_dump($dati_pagamento);
if($payout){
return TRUE;
}else{
return FALSE;
}
}else{
$this->log_ipn_test($result['message_error']);
return FALSE;
}
} //fine transfer_money_from_business_to_personal
###########################################################################################
# END TRANSFER FROM BUSINESS TO PERSONAL #
###########################################################################################