1. 程式人生 > >PHP微信支付之掃碼支付

PHP微信支付之掃碼支付

在手機微信端進行微信支付,直接調起JSAPI支付,這可以實現在微信裡邊的開的頁面進行支付,比如微商城,微信端JSAPI支付詳見:;但有時候商城還有PC端,需要在PC端使用微信支付,則需要PC端生成支付二維碼,然後微信掃碼完成支付。例如:


這裡主要講一下PC端掃碼支付以及退款的具體實現:

/**
 * 微信支付請求介面(POST)
 * @param string $goods_id 	商品ID
 * @param string $body 		商品簡單描述
 * @param string $order_sn  訂單編號
 * @param string $total_fee 金額
 * @return  json的資料
 */
public function wxpay($goods_id,$total_fee,$body,$order_sn){
	$config = $this->config;
	
	//統一下單引數構造
	$unifiedorder = array(
		'appid'			=> $config['appid'],
		'mch_id'		=> $config['mch_id'],
		'device_info'	=> 'WEB',
		'nonce_str'		=> self::getNonceStr(),
		'body'			=> $body,
		'out_trade_no'	=> $order_sn,
		'total_fee'		=> $total_fee * 100,
		'spbill_create_ip'	=> self::getip(),
		'notify_url'	=> 'http://'.$_SERVER['HTTP_HOST'].'/notify.php',
		'trade_type'	=> 'NATIVE',
		'product_id'	=> $goods_id
	);
	$unifiedorder['sign'] = self::makeSign($unifiedorder);
	
	//return $unifiedorder;
	
	//請求資料,統一下單
	$xmldata = self::array2xml($unifiedorder);
	$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
	$res = self::curl_post_ssl($url, $xmldata);
	if(!$res){
		return array('status'=>0, 'msg'=>"Can't connect the server" );
	}
	// 這句file_put_contents是用來檢視伺服器返回的結果 測試完可以刪除了
	file_put_contents('./log.txt',$res,FILE_APPEND);
	
	$content = self::xml2array($res);
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
	if(strval($content['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	
	return $content;
}

/**
 * 微信退款(POST)
 * @param string(28) $transaction_id 	在微信支付的時候,微信伺服器生成的訂單流水號,在支付通知中有返回
 * @param string $out_refund_no 		商品簡單描述
 * @param string $total_fee 			微信支付的時候支付的總金額(單位:分)
 * @param string $refund_fee 			此次要退款金額(單位:分)
 * @return string						xml格式的資料
 */
public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){
	$config = $this->config;
	
	//退款引數
	$refundorder = array(
		'appid'			=> $config['appid'],
		'mch_id'		=> $config['mch_id'],
		'nonce_str'		=> self::getNonceStr(),
		'transaction_id'=> $transaction_id,
		'out_refund_no'	=> $out_refund_no,
		'total_fee'		=> $total_fee * 100,
		'refund_fee'	=> $refund_fee * 100
	);
	$refundorder['sign'] = self::makeSign($refundorder);
	
	//請求資料,進行退款
	$xmldata = self::array2xml($refundorder);
	$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
	$res = self::curl_post_ssl($url, $xmldata);
	if(!$res){
		return array('status'=>0, 'msg'=>"Can't connect the server" );
	}
	// 這句file_put_contents是用來檢視伺服器返回的結果 測試完可以刪除了
	//file_put_contents('./log3.txt',$res,FILE_APPEND);
	
	$content = self::xml2array($res);
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
	if(strval($content['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	
	return $content;
}
支付和退款就是這麼簡單,而且支付的時候無需獲取使用者openid,無需證書檔案,無需配置支付授權目錄,這是封裝過的支付類檔案的實現,呼叫方法更簡單:
require_once "webwxpay.class.php";

$config = array(
	'appid'			=> 'wx123456789876',
	'mch_id'	 	=> '123456789',
	'pay_apikey' 	=> '123456789876123456789876123456789876'
);

$wxpay = new WxPay($config);
$result = $wxpay->paytest();
//print_r($result);
scerweima($result['code_url']);		//生成的支付二維碼,使用者可以掃碼付款
這時候就會生成支付二維碼,然後微信掃一掃就可以完成支付:


至於支付回撥驗證,這裡就不過多講了,不明白的可以看,這裡詳細講了如何處理回撥。

類檔案以及使用例項程式碼原始碼下載:

如果真的感覺好用,請點個贊留個好評,謝謝!有問題也可以評論區告訴我!