How to use PHP to develop WeChat payment function
As a convenient online payment method, WeChat payment is deeply loved by merchants and consumers. For developers, developing WeChat payment functions using PHP language is a very important task. This article will introduce how to use PHP to develop WeChat payment functions and provide corresponding code examples.
Before starting development, you first need to apply for the payment interface from WeChat and obtain the relevant API key and merchant number. Log in to the WeChat Payment Developer Platform, apply for the payment interface according to the guidelines, and obtain the API key and merchant number.
Before starting development, make sure you have installed the PHP environment and enabled the corresponding extensions: cURL extension and openssl extension. These two extensions are used in PHP to send HTTPS requests and encrypt data, and are necessary extensions to implement WeChat payment functions.
Before making a payment request, you need to configure the relevant payment parameters and generate a signature according to the WeChat payment interface document. The following is a sample code:
// 配置支付参数 $params = [ 'appid' => 'YOUR_APPID', 'mch_id' => 'YOUR_MCH_ID', 'nonce_str' => md5(time() . mt_rand(0, 1000)), 'body' => '商品描述', 'out_trade_no' => '订单号', 'total_fee' => '支付金额', 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'notify_url' => '支付回调地址', 'trade_type' => 'JSAPI', 'openid' => '用户openid', ]; // 生成签名 $sign = ''; foreach ($params as $key => $value) { if ($key != 'sign' && $value != '') { $sign .= $key . '=' . $value . '&'; } } $sign .= 'key=' . 'YOUR_API_KEY'; // API密钥 $params['sign'] = strtoupper(md5($sign));
Initiate a payment request through the cURL library function and obtain the prepayment order information. The following is a sample code:
$xml_data = "<xml>"; foreach ($params as $key => $value) { $xml_data .= "<" . $key . ">" . $value . "</" . $key . ">"; } $xml_data .= "</xml>"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.mch.weixin.qq.com/pay/unifiedorder'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); $response = curl_exec($ch); curl_close($ch); $result = simplexml_load_string($response); if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') { $prepay_id = $result->prepay_id; // 保存prepay_id,用于后续支付 }
Generate payment parameters through prepay_id, which is used to evoke the WeChat payment page. The following is the sample code:
$nonce_str = md5(time() . mt_rand(0, 1000)); $prepay_params = [ 'appId' => 'YOUR_APPID', 'timeStamp' => time(), 'nonceStr' => $nonce_str, 'package' => 'prepay_id=' . $prepay_id, 'signType' => 'MD5', ]; $sign = ''; ksort($prepay_params); foreach ($prepay_params as $key => $value) { $sign .= $key . '=' . $value . '&'; } $sign .= 'key=' . 'YOUR_API_KEY'; // API密钥 $prepay_params['paySign'] = strtoupper(md5($sign));
Pass the generated payment parameters to the front-end and call the WeChat payment interface through JS.
function onBridgeReady() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', { 'appId': '<?php echo $prepay_params["appId"]; ?>', 'timeStamp': '<?php echo $prepay_params["timeStamp"]; ?>', 'nonceStr': '<?php echo $prepay_params["nonceStr"]; ?>', 'package': '<?php echo $prepay_params["package"]; ?>', 'signType': '<?php echo $prepay_params["signType"]; ?>', 'paySign': '<?php echo $prepay_params["paySign"]; ?>' }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { // 支付成功后的处理逻辑 } } ); } if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } } else { onBridgeReady(); }
The above is the whole process of using PHP to develop WeChat payment function. With these code examples, you can easily implement WeChat Pay functionality and apply it to your website or application. I wish you successful development!
The above is the detailed content of How to use PHP to develop WeChat payment function. For more information, please follow other related articles on the PHP Chinese website!