Home  >  Article  >  Backend Development  >  Alipay mobile payment interface_PHP tutorial

Alipay mobile payment interface_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:16:241064browse

Explanation:

One: Obtain token

To obtain the token, there are two parameters that are required

req_data and sign

The format of req_data is as follows (this is required. If you want more parameters, download the manual yourself):     

<direct_trade_create_req>
	<notify_url>{通知地址}</notify_url>
	<call_back_url>{返回地址}</call_back_url>
	<seller_account_name>{商家支付宝账号}</seller_account_name>
	<out_trade_no>{外部订单号}</out_trade_no>
	<subject>{商品介绍}</subject>
	<total_fee>{商品价格}</total_fee>
</direct_trade_create_req>

The signature format is as follows

   
 $params = array(
            /* 基本信息 */
            'partner'           => {支付宝pid},
            'req_id'            =>  md5({网站订单号}),
            'service'           =>  'alipay.wap.trade.create.direct',
            'format'            =>  'xml',
            'v'                 =>  '2.0',   
            'sec_id'            =>  'MD5',
            "_input_charset"    => CHARSET
            "req_data"      =>  {以面的req_data}
        );
   
    if($sort){
            /* 排序 */
            ksort($params);
            reset($params);
        }
        $sign  = '';
        foreach ($params AS $key => $value)
        {
            $sign  .= "{$key}={$value}&";
        }

        md5(substr($sign, 0, -1) . {支付宝key});

The thing to note about sign is that before sorting md5, you must follow the sorting in the manual, otherwise errors will occur

Then submit it to

via post

http://wappaygw.alipay.com/service/rest.htm?

Note that the returned data is urlencoded, so we need to use urldecode to decode and then parse the returned data

/**
	 * 解析远程模拟提交后返回的信息
		 * @param $str_text 要解析的字符串
	 * @return 解析结果
	 */
	function parseResponse($str_text) {
		//以&ldquo;&&rdquo;字符切割字符串
		$para_split = explode('&',$str_text);
		//把切割后的字符串数组变成变量与数值组合的数组
		foreach ($para_split as $item) {
			//获得第一个=字符的位置
			$nPos = strpos($item,'=');
			//获得字符串长度
			$nLen = strlen($item);
			//获得变量名
			$key = substr($item,0,$nPos);
			//获得数值
			$value = substr($item,$nPos+1,$nLen-$nPos-1);
			//放入数组中
			$para_text[$key] = $value;
		}
		
		if( ! empty ($para_text['res_data'])) {			
			//token从res_data中解析出来(也就是说res_data中已经包含token的内容)
			$doc = new DOMDocument();
			$doc->loadXML($para_text['res_data']);
			$para_text['request_token'] = $doc->getElementsByTagName( "request_token" )->item(0)->nodeValue;
		}
		
		return $para_text;
	}

The final standardized payment form data is (get is used here)

$params = array(
            'partner'           => $this->_config['wap_alipay_partner'],
            'req_id'            =>  md5({网站订单号}),
            'service'           =>  'alipay.wap.trade.create.direct',
            'format'            =>  'xml',
            'v'                 =>  '2.0',   
            'sec_id'            =>  'MD5',
            "_input_charset"    => CHARSET
			'req_data'=>'<auth_and_execute_req><request_token>' . {token} . '</request_token></auth_and_execute_req>',
			'service'	=>	"alipay.wap.auth.authAndExecute",			
		
	);
	//这个地方也要签名的。方式和上面一样
	$params['sign']	= _get_sign($params);
	
	//get数据
	$return = array(
		'online'    =>  {联线},
		'desc'      =>  {支付说明},
		'method'    =>  'GET',
		'gateway'   =>  'http://wappaygw.alipay.com/service/rest.htm?',
		'params'    =>  $params,
	)

Finally jump to Alipay

 <form action="<?php echo $return['gateway'];?>" id="payform" method="<?php echo $return['method'];?>" style="display:none">
<?php foreach ( $return['params'] $_k=>$value){?>
	<input type="hidden" name="<?php echo $_k;>" value="<?php echo $value;>" />
<?php }?>

</form>
<script type="text/javascript">
  document.getElementById('payform').submit();
</script>

The one that will return tomorrow

   

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440113.htmlTechArticleExplanation: 1: Get the token. To get the token, there are two parameters, req_data and sign. The format of req_data is as follows (this These are required. If you want more parameters, download them yourself...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn