With the popularity of WeChat payment, more and more websites need to integrate WeChat payment function. In this article, we will discuss how to implement WeChat payment initiation and query in PHP.
To initiate WeChat payment in PHP, the first step is to call the unified ordering API of WeChat payment to generate a prepayment order. The API needs to pass some necessary parameters, including merchant ID, payment amount, order number, callback URL, etc. The following is a sample code:
<?php require_once('WxPayApi.php'); require_once('WxPayUnifiedOrder.php'); $order = new WxPayUnifiedOrder(); $order->SetBody('商品描述'); $order->SetOut_trade_no('订单号'); $order->SetTotal_fee('支付金额'); $order->SetNotify_url('回调URL'); $order->SetTrade_type('JSAPI'); $order->SetOpenid('用户OpenID'); $result = WxPayApi::unifiedOrder($order);
After calling the unified order API, we need to get the prepayment order information, including prepayment transactions Session ID (prepay_id) and other necessary parameters. The following is a sample code:
<?php require_once('WxPayApi.php'); require_once('WxPayData.php'); $input = new WxPayData(); $input->SetBody('商品描述'); $input->SetOut_trade_no('订单号'); $input->SetTotal_fee('支付金额'); $input->SetNotify_url('回调URL'); $input->SetTrade_type('JSAPI'); $input->SetOpenid('用户OpenID'); $result = WxPayApi::unifiedOrder($input); $prepay_id = $result['prepay_id']; $params = [ 'appId' => $result['appid'], 'timeStamp' => time(), 'nonceStr' => $result['nonce_str'], 'package' => "prepay_id={$prepay_id}", 'signType' => 'MD5' ]; $params['paySign'] = WxPayApi::getSign($params);
After obtaining the prepayment order information, we can pass the obtained parameters to the front end to let the user Initiate payment in WeChat client. The following is a sample code:
<?php echo '<script language="javascript">'; echo 'WeixinJSBridge.invoke("getBrandWCPayRequest", { "appId":"'.$params['appId'].'", "timeStamp":"'.$params['timeStamp'].'", "nonceStr":"'.$params['nonceStr'].'", "package":"'.$params['package'].'", "signType":"'.$params['signType'].'", "paySign":"'.$params['paySign'].'" }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ){ alert("支付成功"); }else{ alert("支付失败"); } });'; echo '</script>';
After the payment is completed, we need to query the payment results to ensure that the transaction has been completed successfully. The following is a sample code:
<?php require_once('WxPayApi.php'); $input = new WxPayOrderQuery(); $input->SetOut_trade_no('订单号'); $result = WxpayApi::orderQuery($input); if ($result['trade_state'] == 'SUCCESS') { echo '支付成功'; } else { echo '支付失败'; }
Summary:
The above is the entire process of initiating and querying WeChat payment in PHP. When we need to integrate WeChat payment on the website, we can follow the above steps. It is recommended to use the officially provided SDK for development, which can save a lot of time and energy.
The above is the detailed content of Implementing WeChat payment initiation and query in PHP. For more information, please follow other related articles on the PHP Chinese website!