支付接口对接技术通用方法 第三方四方支付接口对接开发教程API集成服务

支付接口对接技术做多了以后你会发现,基本上所有的第三方第四方支付接口的基本方法流程就是这样,至于有些签名方式不一样,配制参数不一样等等按照其提供的技术文档要求修改一下即可,有不懂的可以联系。

//配制类,填写在支付平台获取的相关信息
class Constant {
	const USER= "QQ4770851";
    const MERCHNO = "ff8080816586cbd434358a66b0bc1525";
    const CREATE_URL = "https://xxx.com/order/create";
    const QUERY_URL = "https://xxx.com/order/query";
    const KEY = "C66604323DFA42B4864299246E04FD1B";

}
//签名类,按照支付平台的规则对参数进行签名
class SignUtils {
    /** = */
    const QSTRING_EQUAL = "=";
    /** & */
    const QSTRING_SPLIT = "&";
    public static function signMD5($param, $merKey = '') {
    	ksort($param);

    	$arr = [];
    	foreach ($param as $key => $value) {
    		$arr[] = $key . self::QSTRING_EQUAL . $value;
    	}
    	$str = implode(self::QSTRING_SPLIT, $arr);
    	return strtoupper(md5($str . self::QSTRING_SPLIT . $merKey));
    }
}

//请求类,发起支付请求
class HttpUtils {
	/**
	 * [post description]
	 * @param  [type] $url  [description]
	 * @param  array  $post [description]
	 * @return [type]       [description]
	 */
	public static function post($url, $post = []) {
	    $headers = [ "Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache" ];
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$data = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
		curl_close($ch);

	    return $data;
	}
}

// 支付正文开始,配制参数,发起请求
$param = [];
$param['merchno'] = Constant::MERCHNO;
$param['out_trade_no'] = $_POST['rechargeId'];
$param['subject'] = $_POST['username'];
$param['total_amount'] = $_POST['amount'];
$param['redirect_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/return.php';
$param['notify_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/notify.php';
$param['sign'] = SignUtils::signMD5($param, Constant::KEY);
$response = HttpUtils::post(Constant::CREATE_URL, $param);
$arr = json_decode($response,TRUE);
header("location:".$arr['pay_url']);

你可能感兴趣的:(支付接口对接)