Home  >  Article  >  Backend Development  >  How to develop WeChat payment in PHP?

How to develop WeChat payment in PHP?

王林
王林Original
2023-05-12 08:39:211252browse

With the popularity of smartphones, mobile payment has become an essential tool in people's lives. As one of the representatives, WeChat Pay has a large user base and market share in the domestic market. Therefore, WeChat payment development has become increasingly important for some developers who want to develop e-commerce or mobile applications.

This article will mainly introduce how to develop WeChat payment in PHP.

  1. Preparation

Before using WeChat Pay, we need to register an account on the WeChat Pay official website and obtain the developer ID, merchant number, API key and other necessary Information. At the same time, it is necessary to ensure that the merchant account has successfully opened the WeChat payment service, submitted relevant review materials, and passed the WeChat payment certification.

  1. Integrated SDK

WeChat Pay provides many SDKs for development in different languages, which we can download directly from the official website and integrate into our projects. Here we take PHP language as an example to introduce how to integrate WeChat Payment SDK.

(1) Download WeChat Payment SDK

We can download the PHP version of SDK on the official website, and after downloading, unzip it to get the "lib" directory. The "lib/WxPay.Api.php" file is the main SDK file.

(2) Create a payment file

In order to facilitate the use of the SDK, we can create a new "pay.php" file ourselves to process the logic related to WeChat payment. In this file, we need to reference and initialize the relevant settings of the above SDK. Before doing this, we need to pay attention to the following points:

——The public account appid can be obtained through the official website of WeChat Pay;
——The merchant number can be obtained through the official website of WeChat Pay;
—— Set the API key, which can be obtained through the official website of WeChat Pay;
—— Set the order transaction number, order transaction amount and other necessary parameters;

Then, we can initialize the SDK through the following code:

<?php
include_once "WxPay.Api.php";
include_once "WxPay.Data.php";

// 初始化SDK信息
WxPayConfig::$APPID = '您的公众账号APPID';
WxPayConfig::$MCHID = '您的商户号';
WxPayConfig::$KEY = '您的API密钥';

// 设置订单交易号、订单交易金额等必要的参数
$out_trade_no = '您的订单交易号';
$total_fee = '您的订单交易金额';

// ...
?>

(3) Generate signature

Before conducting WeChat payment transactions, we need to generate a signature. The code to generate the signature is as follows:

<?php
// 创建订单支付对象
$input = new WxPayUnifiedOrder();
$input->SetBody("test");                                       // 商品描述
$input->SetAttach("test");                                     // 附加数据
$input->SetOut_trade_no($out_trade_no);                        // 商户订单号
$input->SetTotal_fee($total_fee);                              // 标价金额
$input->SetTime_start(date("YmdHis"));                         // 交易起始时间
$input->SetTime_expire(date("YmdHis", time() + 60 * 5));        // 交易结束时间
$input->SetGoods_tag("test");                                  // 订单优惠标记
$input->SetNotify_url("http://localhost/pay/callback.php");    // 通知地址
$input->SetTrade_type("JSAPI");                                 // 交易类型
$input->SetOpenid("用户的openid");                              // 用户标识

// 生成签名
$order = WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
?>

(4) JSAPI payment

Finally, we can call the WeChat payment page through JSAPI to implement the WeChat payment task. The code is as follows:

<input type="button" value="微信支付" onclick="callpay()" />
<script type="text/javascript">

// JSAPI调起微信支付
function callpay()
{
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        <?php echo $jsApiParameters; ?>,
        function(res){
            WeixinJSBridge.log(res.err_msg);
            if(res.err_msg == "get_brand_wcpay_request:ok" ){
                alert('支付成功');
            }else{
                alert('支付失败:' + res.err_msg);
            }
        }
    );
}
</script>
  1. Summary

The above is a detailed introduction on how to develop WeChat payment in PHP. Through the above steps, we can quickly implement the WeChat payment function and improve the user experience of the website or mobile application. Of course, depending on the specific situation, related issues such as security during the payment process also need to be considered.

The above is the detailed content of How to develop WeChat payment in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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