<?php
namespace app\common\service;
/**
* Airwallex 空中云汇支付
* @package app\common\service
*/
class Airwallex
{
var $config = [
'clientId' => '',
'apiKey' => '',
'urlDomain' => 'https://api.airwallex.com/'
];
/**
* 创建付款链接
* @param $order
* @return array
*/
function generateTrade($order = array())
{
$arr = [
'amount' => $order['amount'],
'currency' => '',
'reusable' => false,
'title' => '',
"reference" => '',
];
$result = $this->post(
$this->config['urlDomain'].'api/v1/pa/payment_links/create',
['Content-Type: application/json','Authorization: Bearer '.$this->getAirwallexToken()],
json_encode($arr)
);
if(isset($result['url'])){
return array('code' => 1, 'data' => $result);
}else{
return array('code' => 0, 'msg' => $result['message']);
}
}
/**
* 创建退款
* @param $order
* @return array
*/
function refund($order = array())
{
$arr = [
'amount' => $order['amount'],
'currency' => '',
'payment_intent_id' => $order['pay_id'],
'request_id' => $order['refund_no']
];
$result = $this->post(
$this->config['urlDomain'].'api/v1/pa/refunds/create',
['Content-Type: application/json','Authorization: Bearer '.$this->getAirwallexToken()],
json_encode($arr)
);
if(isset($result['id'])){
return array('code' => 1, 'data' => $result);
}else{
return array('code' => 0, 'msg' => $result['message']);
}
}
/**
* 检索付款链接
* @param $order
* @return array
*/
function notify($payment_links_id)
{
$result = $this->post(
$this->config['urlDomain'].'api/v1/pa/payment_links/'.$payment_links_id,
['Content-Type: application/json','Authorization: Bearer '.$this->getAirwallexToken()],
'',
'GET'
);
return $result;
}
/**
* 获取airwallex AccessToken
*/
function getAirwallexToken(){
$airwallex_token_config = cache("airwallex-token");
$end_time = time();
if($airwallex_token_config)
{
$result = $airwallex_token_config;
$end_time = strtotime($result['expires_at']);
}
if($end_time - time() < 60){ //更新access token
$result = $this->post(
$this->config['urlDomain'].'api/v1/authentication/login',
[
'Content-Type: application/json',
'x-api-key: '.$this->config['apiKey'],
'x-client-id: '.$this->config['clientId'],
]
);
if(isset($result['token'])){
cache("airwallex-token",json_encode($result),3600);
}
}
return isset($result['token']) ? $result['token'] : false;
}
/**
* 发起POST请求
*/
public function post($url, $headers = array(), $request = '',$methed = 'POST')
{
$header_res = [];
$curl = curl_init($url);
if($methed == 'POST'){
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, 1);
}else{
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $methed);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
if($request){
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$header_res) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$header_res[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$response_data = curl_exec($curl);
curl_close($curl);
$result = json_decode($response_data, true);
return $result;
}
}
airwallex(空中云汇)支付
于 2023-06-29 15:54:37 首次发布
该代码实现了Airwallex支付平台的接口调用,包括创建付款链接、退款功能以及检索付款链接。通过POST请求与API交互,使用accesstoken进行身份验证。
摘要由CSDN通过智能技术生成