1
我正在做一個類與貝寶付款。獲取令牌的作品,但付款不
通過功能獲得令牌getToken()
的作品,它吐出了一個很好的迴應(根據文檔)。
但是,當我使用該令牌建立與令牌使用的相同的付款時;它只返回一個空字符串(根據var_dump())。
如果我從api-information中拷貝確切的curl命令並粘貼beare-token;有用。所以我的API調用有些問題....
顯然我錯過了一些東西。任何人都可以指出我的錯誤?
它得到了承載令牌功能:
public function getToken(){
$headers = array(
"Accept-Language" => 'en_US',
"Accept" => "application/json"
);
$t = json_decode($this->sendAPICall('grant_type=client_credentials', '/oauth2/token', $headers, true));
if($t->error){
$this->error = $t->error_description;
$this->token = NULL;
return false;
}else{
$this->token = $t;
return true;
}
}
檢查有可用的令牌後應付款的功能。
public function makePayment(){
$this->getToken();
if($this->error){
return false;
}else{
$d = '{"intent":"sale",
"redirect_urls":{
"return_url":"'.$this->config['returnURL'].'",
"cancel_url":"'.$this->config['cancelURL'].'"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"'.$this->amount.'",
"currency":"'.$this->config['currency'].'"
},
"description":"'.$this->description.'"
}
]
}';
$headers = array( "Authorization" => $this->token->token_type . ' ' . $this->token->access_token,
"Content-type" => "application/json"
);
return $this->sendAPICall(urlencode($d), '/payments/payment', $headers, false);
}
}
和關閉過程與PayPal的API,在這裏我使用了$ AUTH布爾值,使發送userpwd或使用令牌之間的差異連接:
private function sendAPICall($data, $url, $headers, $auth=true){
$ch = curl_init();
$options = array( CURLOPT_URL => $this->config['endpoint'].$url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
);
if($auth){
$options[CURLOPT_USERPWD] = $this->config['client_id'].':'.$this->config['client_secret'];
};
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec($ch);
}