airwallex(空中云汇)支付

该代码实现了Airwallex支付平台的接口调用,包括创建付款链接、退款功能以及检索付款链接。通过POST请求与API交互,使用accesstoken进行身份验证。
摘要由CSDN通过智能技术生成
<?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;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值