溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

微信掃描支付訂單數(shù)據(jù)生成類(lèi)[模式二]

發(fā)布時(shí)間:2020-07-21 18:01:34 來(lái)源:網(wǎng)絡(luò) 閱讀:793 作者:penyanlu 欄目:開(kāi)發(fā)技術(shù)

微信掃描支付訂單數(shù)據(jù)生成類(lèi)[模式二]

*

*必要條件:

*1.微信公共號(hào)

*2.微信公共號(hào)APPID

*3.微信公共號(hào) 【微信支付】 綁定的 商戶(hù)號(hào)MCH_ID

*4. 微信公共號(hào) 商戶(hù)支付密鑰

*

* 支付流程:

* 1、調(diào)用統(tǒng)一下單,取得code_url,生成二維碼

* 2、用戶(hù)掃描二維碼,進(jìn)行支付

* 3、支付完成之后,微信服務(wù)器會(huì)通知支付成功

* 4、在支付成功通知中需要查單確認(rèn)是否真正支付成功

業(yè)務(wù)流程說(shuō)明:

(1)商戶(hù)后臺(tái)系統(tǒng)根據(jù)用戶(hù)選購(gòu)的商品生成訂單。

(2)用戶(hù)確認(rèn)支付后調(diào)用微信支付【統(tǒng)一下單API】生成預(yù)支付交易;

(3)微信支付系統(tǒng)收到請(qǐng)求后生成預(yù)支付交易單,并返回交易會(huì)話的二維碼鏈接code_url。

(4)商戶(hù)后臺(tái)系統(tǒng)根據(jù)返回的code_url生成二維碼。

(5)用戶(hù)打開(kāi)微信“掃一掃”掃描二維碼,微信客戶(hù)端將掃碼內(nèi)容發(fā)送到微信支付系統(tǒng)。

(6)微信支付系統(tǒng)收到客戶(hù)端請(qǐng)求,驗(yàn)證鏈接有效性后發(fā)起用戶(hù)支付,要求用戶(hù)授權(quán)。

(7)用戶(hù)在微信客戶(hù)端輸入密碼,確認(rèn)支付后,微信客戶(hù)端提交授權(quán)。

(8)微信支付系統(tǒng)根據(jù)用戶(hù)授權(quán)完成支付交易。

(9)微信支付系統(tǒng)完成支付交易后給微信客戶(hù)端返回交易結(jié)果,并將交易結(jié)果通過(guò)短信、微信消息提示用戶(hù)。微信客戶(hù)端展示支付交易結(jié)果頁(yè)面。

(10)微信支付系統(tǒng)通過(guò)發(fā)送異步消息通知商戶(hù)后臺(tái)系統(tǒng)支付結(jié)果。商戶(hù)后臺(tái)系統(tǒng)需回復(fù)接收情況,通知微信后臺(tái)系統(tǒng)不再發(fā)送該單的支付通知。

(11)未收到支付通知的情況,商戶(hù)后臺(tái)系統(tǒng)調(diào)用【查詢(xún)訂單API】。

(12)商戶(hù)確認(rèn)訂單已支付后給用戶(hù)發(fā)貨。

<?php

class UnifiedOrder
	{
		protected $signKey='';//商戶(hù)支付密鑰,參考開(kāi)戶(hù)郵件設(shè)置(必須配置,登錄商戶(hù)平臺(tái)自行設(shè)置)
		protected $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//掃描支付模式二提交接口
		//訂單配置數(shù)據(jù)	
		protected $orderInfo=array(
				'body'=>'',//商品支付描述
				'attach'=>'',//附加數(shù)據(jù)
				'out_trade_no'=>'',//交易號(hào)
				'total_fee'=>'',//總金額
				'time_start'=>'',//開(kāi)始時(shí)間
				'time_expire'=>'',//有效時(shí)間
				'goods_tag'=>'',//商品
				'notify_url'=>'',//異步通知URL
				'trande_type'=>'',//支付類(lèi)型  NATIVE
				'product_id'=>'',//商品ID
				'Appid'=>'111111',//公共號(hào)APPID
				'mch_id'=>'111111',//商戶(hù)號(hào)
				'bill_create_ip'=>'0.0.0.0',//客戶(hù)端IP
				'nonce_str'=>'1111111',//隨機(jī)字符串
			);
		
		//必填參數(shù)
		protected $needfileds=array(
				'out_trade_no',
				'body',
				'total_fee',
				'trade_type',
				'product_id',
				'notify_url',
				'Appid',
				'mch_id',
				'bill_create_ip',
				'nonce_str',
			);
			
		//獲取訂單配置數(shù)據(jù)	
		public function getOrderinfo()
			{
				return $this->orderInfo;
			}
			
		//設(shè)置訂單數(shù)據(jù)	
		public function setOrderinfo($info=array())
			{
				if(!empty($info) && is_array($info))
					{
						foreach($info as $key=>$val)
							{
								if(isset($this->orderInfo[$key]))
									{
										$this->orderInfo[$key]=$val;
									}
							}
					}
				return true;
			}
			
		//檢查必填參數(shù)
		protected function checkOrderinfo()
			{
				foreach($this->orderInfo as $key=>$val)
					{
						if(in_array($key,$this->needfileds))
							{
								if(!$val)
									{
										throw new Exception('缺少統(tǒng)一支付接口必填參數(shù)'.$key.'!');
									}
							}
					}
			}
		
		//生成簽名
		protected function setSign()
			{
				ksort($this->orderInfo);
				$signstr=$this->ToKeyVal();
				$signstr=$signstr.'&key='.$this->signKey;
				$signstr=strtoupper(md5($signstr));
				$this->orderInfo['sign']=$signstr;
				return $signstr;
			}
			
		protected function checkSign()
			{
				if(!$this->orderInfo['sign'])
					{
						throw new Exception('簽名錯(cuò)誤!');
					}
					
				$sign=$this->setSign();
				
				if($this->orderInfo['sign']==$sign)
					{
						return true;
					}
				throw new Exception('簽名錯(cuò)誤!');
			}
			
		//生成KEY=VAL參數(shù)個(gè)數(shù)
		protected function ToKeyVal()
			{
				$keyval='';
				foreach($this->orderInfo as $key=>$val)
					{
						if(strtolower($key)!='sign' && $val !='' && !is_array($val))
							{
								$keyval .=$key.'='.$val.'&';
							}
					}
				$keyval=trim($keyval,'&');
				return $keyval;
			}
			
		//數(shù)組轉(zhuǎn)換成XML格式數(shù)據(jù)	
		protected function arrayToXml()
			{
				if(!is_array($this->orderInfo) && count($this->orderInfo)<=0)
					{
						throw new Exception('數(shù)組數(shù)據(jù)異常!');
					}
					else
						{
							$xml='<xml>';
							foreach($this->orderInfo as $k=>$v)
								{
									if(is_numeric($v))
										{
											$xml.='<'.$k.'>'.$v.'</'.$k.'>';
										}
										else
											{
												$xml.='<'.$k.'>![CDATA['.$v.']]</'.$k.'>';
											}
								}
							$xml.='</xml>';
							return $xml;
						}
			}	
			
		//XML格式數(shù)據(jù)轉(zhuǎn)換成數(shù)組數(shù)據(jù)
		protected function xmlToArray($xml='')
			{
				if(!$xml)
					{
							throw new Exception('xml數(shù)據(jù)異常!');
					}
					else
						{
							libxml_disable_entity_loader(true);
							$this->returnData=json_decode(json_encode(@simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
							return $this->returnData;
						}
			}
			
		//以curl方式提交數(shù)據(jù)到微信服務(wù)器
		protected function curlXml($xml, $url, $ca = false, $second = 30)
			{
				$ch = curl_init();
				//設(shè)置超時(shí)
				curl_setopt($ch, CURLOPT_TIMEOUT, $second);

				curl_setopt($ch,CURLOPT_URL, $url);
				curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
				curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//嚴(yán)格校驗(yàn)
				//設(shè)置header
				curl_setopt($ch, CURLOPT_HEADER, FALSE);
				//要求結(jié)果為字符串且輸出到屏幕上
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
			
				if($ca == true)
					{
						//設(shè)置證書(shū)
						//使用證書(shū):cert 與 key 分別屬于兩個(gè).pem文件
						curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
						curl_setopt($ch,CURLOPT_SSLCERT, './apiclient_cert.pem');
						curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
						curl_setopt($ch,CURLOPT_SSLKEY, './apiclient_key.pem');
					}
				//post提交方式
				curl_setopt($ch, CURLOPT_POST, TRUE);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
				//運(yùn)行curl
				$data = curl_exec($ch);
				//返回結(jié)果
				if($data)
					{
						curl_close($ch);
						return $data;
					} 
					else 
						{ 
							$error = curl_errno($ch);
							curl_close($ch);
							throw new Exception("curl出錯(cuò),錯(cuò)誤碼:$error");
						}
			}
			
		protected function wxReply($reply='')
			{
				$this->orderInfo=$this->xmlToArray($reply);
				if($this->orderInfo['return_code'] != 'SUCCESS')
					{
						 return $this->orderInfo;
					}
					
				$this->CheckSign();
				return $this->orderInfo;
			}
			
		//發(fā)送請(qǐng)求數(shù)據(jù)到微信服務(wù)器
		public function requestXml($info=array())
			{
				$this->setOrderinfo($info);//設(shè)置數(shù)據(jù)
				$this->checkOrderinfo();//檢查必填參數(shù)
				$this->setSign();//生成簽名
				
				$xml=$this->arrayToXml();//數(shù)據(jù)格式轉(zhuǎn)換
				$result=$this->curlXml($xml,$this->url,false);//curl發(fā)送數(shù)據(jù)
				$result=$this->wxReply($result);//返回結(jié)果處理 
				
				return $result;
			}
			
		
	}

	$class = new UnifiedOrder();
	$info=array(
				'body'=>'test',
				'attach'=>'test',
				'out_trade_no'=>date("YmdHis"),
				'total_fee'=>'1',
				'time_start'=>date("YmdHis"),
				'time_expire'=>date("YmdHis", time() + 600),
				'goods_tag'=>'test',
				'notify_url'=>'http://localhost/WXqrpay/notify.php',
				'trande_type'=>'NATIVE',
				'product_id'=>'10000000111',
			);
	$class->requestXml($info);
	$result=$class->getOrderinfo();
	var_dump($result);
?>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI