有乎-Tommy.Li的个人博客

价值、共享、信任

【支付宝】支付宝即时到帐接口简单版实现

| 阅读:203 发表时间:2020-07-14 10:54:18 技术专栏

支付宝即时到帐接口简单版     官方DEMO

$appid = '2088XXXXXXXXXXX';
$key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';
$data['service'] = 'alipay.wap.create.direct.pay.by.user';      //PC端:create_direct_pay_by_user    WAP端:alipay.wap.create.direct.pay.by.user
$data['partner'] = $appid;
$data['_input_charset'] = 'UTF-8';
$data['out_trade_no'] = time().rand(1000,9999);
$data['subject'] = "余额充值";
$data['payment_type'] = 1;
$data['total_fee'] = rand(99, 256);
$data['notify_url']='xxxxxxxxxxxxxxx';
$data['return_url']='xxxxxxxxxxxxxxx';
$data['seller_id'] = $appid;

function buildRequestParaToString($para_temp) {
	//待请求参数数组
	$para = buildRequestPara($para_temp);
	//把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对字符串做urlencode编码
	$request_data = createLinkstringUrlencode($para);
	return $request_data;
}

function buildRequestPara($para_temp) {
		//除去待签名参数数组中的空值和签名参数
		$para_filter = paraFilter($para_temp);
		//对待签名参数数组排序
		$para_sort = argSort($para_filter);
		//生成签名结果
		$mysign = buildRequestMysign($para_sort);
		//签名结果与签名方式加入请求提交参数组中
		$para_sort['sign'] = $mysign;
		$para_sort['sign_type'] = "MD5";
		return $para_sort;
	}
	
function paraFilter($para) {
	$para_filter = array();
	while (list ($key, $val) = each ($para)) {
		if($key == "sign" || $key == "sign_type" || $val == "")continue;
		else	$para_filter[$key] = $para[$key];
	}
	return $para_filter;
}
/**
 * 对数组排序
 * @param $para 排序前的数组
 * return 排序后的数组
 */
function argSort($para) {
	ksort($para);
	reset($para);
	return $para;
}

function md5Sign($prestr, $key) {
	$prestr = $prestr . $key;
	return md5($prestr);
}


function buildRequestMysign($para_sort) {
       global $key;
		//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
		$prestr = createLinkstring($para_sort);
		$mysign = md5Sign($prestr, $key);
		return $mysign;
	}

function createLinkstring($para) {
	$arg  = "";
	while (list ($key, $val) = each ($para)) {
		$arg.=$key."=".$val."&";
	}
	//去掉最后一个&字符
	$arg = substr($arg,0,count($arg)-2);
	//如果存在转义字符,那么去掉转义
	if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
	return $arg;
}

function createLinkstringUrlencode($para) {
	$arg  = "";
	while (list ($key, $val) = each ($para)) {
		$arg.=$key."=".urlencode($val)."&";
	}
	//去掉最后一个&字符
	$arg = substr($arg,0,count($arg)-2);
	//如果存在转义字符,那么去掉转义
	if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
	return $arg;
}
$para = buildRequestPara($data);
$payUrl= "https://mapi.alipay.com/gateway.do?".http_build_query($para);


生成payUrl之后,直接跳转链接文字

*文章为作者独立观点,不代表【uuuho有乎】的立场
本文由【uuuho有乎】发表并编辑,转载此文章须经作者同意,并请附上出处及本页链接。如有侵权,请联系本站删除。