HipoPay PHP fourth party pay demo

1. This is a kind of synthesis of my own, PHP version is 5.6, groping for two days, finally worked out;

2. direct reference to the incoming main value offered by service providers on the line;

3. The encryption method including the last one, with no lines are;

4. Write the vegetables, hope bigwigs exhibitions;

<?php
/**
 * Payment collection Hipopay class
 * 
 */
class WechatCN extends Action{

    public function getBill($params) {
        $request = new Request('/download_bill', $params);
        $request->get();
    }

    public function getPayment($params) {
        $request = new Request('/payment', $params);
        $request->get();
    }

    public function refund($params) {
        $request = new Request('/payment_refund', $params);
        $request->post();
    }

    public function getRefund($params) {
        $request = new Request('/payment_refund', $params);
        $request->get();
    }

    public function appPay($params) {
//      $params = $this->__isCNY($params);
        $request = new Request('/wechatpay/app/payment', $params);
        $request->post();
    }
    public function mpPay($params) {
//        $params = $this->__isCNY($params);
        $request = new Request('/mp_pay', $params);
        $request->post();
    }   
    public function miniProgramPay($params) {
//        $params = $this->__isCNY($params);
        $request = new Request('/wechatpay/mini_program/payment', $params);
        $request->post();
    }   
    public function consumerScanWeb($params) {
//        $params = $this->__isCNY($params);
        $request = new Request('/wechatpay/web/payment', $params);
        // unset( $request->pay_config);
      
        $request->post();
    }   
    public function consumerScanDevice($params) {
//        $params = $this->__isCNY($params);
        $request = new Request('/wechatpay/qrcode/payment', $params);
        $request->post();
    }   
    public function merchantScanConsumer($params) {
//        $params = $this->__isCNY($params);
        $request = new Request('/wechatpay/barcode/payment', $params);
        $request->post();
    }   
    public function getRate ($ params) {
        $request = new Request('/wechatpay/forex_rate', $params);
        $request->get();
    }   
    public function declaration($params) {
        $request = new Request('/wechatpay/declaration', $params);
        $request->post();
    }   
    public function redeclaration($params) {
        $request = new Request('/wechatpay/redeclaration', $params);
        $request->post();
    }
    public function getDeclaration($params) {
        $request = new Request('/wechatpay/declaration', $params);
        $request->get();
    }
}
class Alipay{
//    protected function __isHK($params) {
//        if ($this->isHK) {
//            $params['hk_wallet'] = 'true';
//        }
//        return $params;
//    }
    public function getBill($params) {
        $request = new Request('/download_bill', $params);
        $request->get();
    }

    public function getPayment($params) {
        $request = new Request('/payment', $params);
        $request->get();
    }

    public function refund($params) {
        $request = new Request('/payment_refund', $params);
        $request->post();
    }

    public function getRefund($params) {
        $request = new Request('/payment_refund', $params);
        $request->get();
    }
    public function appPay($params) {
    //        $params = $this->__isCNY($params);
        $request = new Request('/alipay/app/payment', $params);
        $request->post();
    }

    public function wapPay($params) {
    //        $params = $this->__isHK($params);
    //        $params = $this->__isCNY($params);
        $request = new Request('/alipay/wap/payment', $params);
        $request->post();
    }

    public function consumerScanWeb($params) {
    //        $params = $this->__isHK($params);
        $request = new Request('/alipay/web/payment', $params);
        $request->post();
    }

    public function consumerScanMerchant($params) {
    //        $params = $this->__isHK($params);
        $request = new Request('/alipay/qrcode/payment', $params);
        $request->post();
    }

    public function merchantScanConsumer($params) {
    //        $params = $this->__isHK($params);
        $request = new Request('/alipay/barcode/payment', $params);
        $request->post();
    }


    public function getRate ($ params) {
        $request = new Request('/alipay/forex_rate', $params);
        $request->get();
    }

    public function declaration($params) {
        $request = new Request('/alipay/declaration', $params);
        $request->post();
    }

    public function redeclaration($params) {
        $request = new Request('/alipay/redeclaration', $params);
        $request->post();
    }

    public function getDeclaration($params) {
        $request = new Request('/alipay/declaration', $params);
        $request->get();
    }
    // two-dimensional code Alipay payment
    public function codePay($params){
        $request = new Request('/alipay/scanned_pay', $params);
        $request->post();
    }
}

class Request
{
    protected $_config = [];
    const REQUEST_TIME = 10;
    public function __construct($api_url, $params) {
        $this->pay_config = array(
            'HP_HOST'               =>  'https://testapi.wisecashier.com',
            'MERCHANT_NO' => 'Business Number'
            'VERSION'               =>  '1.0',
            'PRIVATE_KEY_PATH' => 'the path /private.key',
            'MERCHANT_APPID' => '', // micro-channel APPID
            // 'MERCHANT_MINI_APPID' => 'go micro-channel public platform, login applet account, get into the development setup'
        );
        $this->_config['url'] = sprintf("%s%s", $this->pay_config['HP_HOST'], $api_url);
        $this->_config['params'] = $params;
        $this->_config['timestamp'] = time();
        $this->_config['signature'] = $this->sign();
        // $this->_config['signature'] = sign($params, $this->_config['timestamp'], $this->pay_config['PRIVATE_KEY_PATH']);

        $this->_config['header'] = [
            "Version:" . $this->pay_config['VERSION'] ,
            "MerchantNo:" .  $this->pay_config['MERCHANT_NO'],
            "Signature:" . $this->_config['signature'],
            "Timestamp:" . $this->_config['timestamp']
        ];
        if($api_url == '/download_bill') {
            $this->_config['header'] = [
                "Version:3.0"  ,
                "MerchantNo:" .  $this->pay_config['MERCHANT_NO'],
                "Signature:" . $this->_config['signature'],
                "Timestamp:" . $this->_config['timestamp']
            ];
        }
        // echo json_encode($this->_config, JSON_PRETTY_PRINT);
        // exit;
    }
    /**
     * Internal calls get request
     * @return object | bool
     */
    public function get() {
        $data = $this->_config['params'];
        $url = $this->_config['url'];
        if (is_array($data) && count($data) > 0) {
            $param = http_build_query($data);
            $url = (!strpos($url, '?')) ? $url . "?" . $param : $url . "&" . $param;
        }
        $rs = $this->_httpGet($url);
        // echo $rs;
        return $rs;
    }
    /**
     * Internal call post request
     * @param $withFiles
     * @return object | bool
     */
    public function post($withFiles = false) {
        header("Content-type:text/html;charset=utf-8");
        $rs = $this->_httpPost('POST', $withFiles);
        // echo $rs;
        print_r(json_decode($rs,true));
        return $rs;
    }
    /**
     * Internal call put request
     * @param $withFiles
     * @return object | bool
     */
    public function put($withFiles = false) {
        $rs = $this->_httpPost('PUT', $withFiles);
        // echo $rs;
        return $rs;
    }
    /**
     * Internal call delete request
     * @return object | bool
     */
    public function del() {
        $rs = $this->_httpPost('DELETE');
        // echo $rs;
        return $rs;
    }
    /**
     * Curl-get request
     * @param $url
     * @return object | bool
     */
    protected function _httpGet($url = "") {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_config['header']);
        curl_setopt($curl, CURLOPT_TIMEOUT, self::REQUEST_TIME);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);

        $info = curl_getinfo($curl);
        curl_close($curl);
        return $this->returnData($res, $info);
    }
    /**
     * Curl-post requests
     * @param $request_type
     * @param $withFiles
     * @return object | bool
     */
    protected function _httpPost($request_type = 'POST', $withFiles = false) {
        // echo "\nAPI--->" . $this->_config['url'] . "\n";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->_config['url']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, self::REQUEST_TIME);
        if ($withFiles) {
            $boundary = '---------------'.time().rand(1000, 99999);
            $this->_config['header']['Content-Type'] = "multipart/form-data;boundary=" . $boundary;
        }
        // determine the type of submission
        switch ($request_type) {
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, true);
                break;
            case 'PUT':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                break;
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
            default:
                return Message::msg(904);
                break;
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->_config['params']);
        if ($this->_config['header']) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_config['header']);
        }
        $res = curl_exec($curl);
        $info = curl_getinfo($curl);
        curl_close($curl);
        return $this->returnData($res, $info);
    }
    /**
     * Return Data
     * @Param $ res
     * @Param $ info
     * @return object
     */
    protected function returnData($res, $info) {
       if ($info['http_code'] == 200) {
            return $res;
       } elseif ($info['http_code'] == 500 || $info['http_code'] == 0 ) {
           return Message::msg(500);
       } else {
           return Message::msg(903);
       }
    }
    public function sign() {
        $signature_str = '';
        $params = $this->_config['params'];
        $prikey = openssl_pkey_get_private(file_get_contents($this->pay_config['PRIVATE_KEY_PATH']));
        ksort($params);
        foreach ($params as $key => $item) {
            if (strlen($signature_str) == 0) {
                $signature_str .= $key . '=' . rawurlencode($item);
            } else {
                $signature_str .= '&' . $key . '=' . rawurlencode($item);
            }
        }
        $signature_str = utf8_encode($signature_str);
        $signature_str .= ',' . strval($this->_config['timestamp']);
        openssl_sign($signature_str, $sign, $prikey, OPENSSL_ALGO_SHA256);
        $sign = base64_encode($sign);
        return $sign;
    }
}
class Message
{
    const DEFINE_MSG_NUM = '000';
    public static function msg($msg_num = null) {
        if(is_null($msg_num) || !is_numeric($msg_num))
            $msg_num = self::DEFINE_MSG_NUM;
        return json_encode(self::_matchMsg((string)$msg_num));
    }
    protected static function _matchMsg($msg_num) {
        $msg_list = self::_msgList();
        $msg_num = array_key_exists($msg_num, $msg_list) ? $msg_num : self::DEFINE_MSG_NUM;
        return $msg_list[$msg_num];
    }
    protected static function _msgList() {
        return [
            '000' => [ 'meta' => [ 'status_code' => 0, 'message' => 'prompt code error, please re-set', 'success' => false]],
            '200' => ['meta' => ['status_code' => 200, 'message' => '成功', 'success' => true]],
            '404' => [ 'meta' => [ 'status_code' => 404, 'message' => 'This page is not found', 'success' => false]],
            '500' => [ 'meta' => [ 'status_code' => 500, 'message' => 'server fails to respond', 'success' => false]],
            '304' => [ 'meta' => [ 'status_code' => 304, 'message' => 'redirect page', 'success' => true]],
            '901' => [ 'meta' => [ 'status_code' => 901, 'message' => 'keys path errors', 'success' => false]],
            '902' => [ 'meta' => [ 'status_code' => 902, 'message' => 'request timeout', 'success' => false]],
            '903' => [ 'meta' => [ 'status_code' => 903, 'message' => 'request error, please try again', 'success' => false]],
            '904' => [ 'meta' => [ 'status_code' => 904, 'message' => 'Request type error', 'success' => false]],
        ];
    }
    public static function ifMsg($message, $contents) {}
}



function sign($data, $timestamp, $private_key) {
    $prikey = openssl_pkey_get_private(file_get_contents($private_key));
    ksort($data);
    $signature_str = '';
    foreach ($data as $key => $item) {
        if (strlen($signature_str) == 0) {
            $signature_str .= $key . '=' . rawurlencode($item);
        } else {
            $signature_str .= '&' . $key . '=' . rawurlencode($item);
        }
    }
    $signature_str = utf8_encode($signature_str);
    $signature_str .= ',' . strval($timestamp);
    $alg = OPENSSL_ALGO_SHA256;
    openssl_sign($signature_str, $sign, $prikey, $alg);
    $sign = base64_encode($sign);
    return $sign;
}
?>

Guess you like

Origin www.cnblogs.com/PLasir/p/11592626.html