Guide to Online Payment and Refund Processing with PHP and Mini Programs

王林
Release: 2023-07-04 13:20:01
Original
1164 people have browsed it

Guide to Online Payment and Refund Processing with PHP and Mini Programs

With the continuous development of e-commerce, more and more companies are beginning to use online payment and refund functions to provide consumers with convenience and speed payment method. As a commonly used server-side programming language, PHP can be combined with small programs to realize online payment and refund functions. This article will introduce how to use PHP and mini programs to develop online payment and refund functions, and provide code samples for readers' reference.

1. Online payment function

  1. Preparation

First, we need to prepare a merchant account and key, as well as the API interface for WeChat payment. In the mini program, we can use the mini program payment interface of WeChat Pay to complete the payment function. For details, please refer to the development documentation of WeChat Payment.

  1. Client Development

In the front-end page of the mini program, we can use the wx.requestPayment() method of WeChat Pay to activate the payment function. The specific code is as follows:

wx.requestPayment({
  'timeStamp': '',
  'nonceStr': '',
  'package': '',
  'signType': 'MD5',
  'paySign': '',
  'success': function(res){
     // 支付成功的回调函数
  },
  'fail': function(res){
     // 支付失败的回调函数
  }
})
Copy after login

In this method, we need to pass in some payment parameters, such as timestamp, random string, order information, etc. These parameters can be obtained from the server side. For details, please refer to the development documentation of WeChat Payment.

  1. Server-side development

On the server side, we need to use PHP to process payment requests. The specific code is as follows:

<?php
  $appid = 'your_appid'; // 小程序的appid
  $mch_id = 'your_mch_id'; // 商户号
  $key = 'your_key'; // 商户密钥
  $body = '商品描述';
  $out_trade_no = '订单号';
  $total_fee = '总金额';

  $params = array(
    'appid' => $appid,
    'mch_id' => $mch_id,
    'nonce_str' => md5(rand()),
    'body' => $body,
    'out_trade_no' => $out_trade_no,
    'total_fee' => $total_fee,
    'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
    'trade_type' => 'JSAPI',
    'openid' => '用户openid'
  );

  ksort($params);
  $string = http_build_query($params);
  $string = urldecode($string).'&key='.$key;
  $sign = strtoupper(md5($string));

  $params['sign'] = $sign;

  $xml = '<xml>';
  foreach ($params as $key => $value) {
    $xml .= '<'.$key.'>'.$value.'</'.$key.'>';
  }
  $xml .= '</xml>';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.mch.weixin.qq.com/pay/unifiedorder');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

  $result = simplexml_load_string($result);
  $prepay_id = $result->prepay_id; // 预支付id

  $params = array(
    'appId' => $appid,
    'timeStamp' => time(),
    'nonceStr' => md5(rand()),
    'package' => 'prepay_id='.$prepay_id,
    'signType' => 'MD5'
  );

  ksort($params);
  $string = http_build_query($params);
  $string = urldecode($string).'&key='.$key;
  $sign = strtoupper(md5($string));

  $params['paySign'] = $sign;

  echo json_encode($params);
?>
Copy after login

In this code , we first need to generate a signature based on the merchant account and key, splice the relevant parameters into an XML format string, and send the payment request to the WeChat payment interface through cURL. After receiving the return result, we splice the prepayment ID in the result into another signature, and then return the relevant parameters to the applet.

2. Refund function

  1. Client development

In the front-end page of the mini program, we can use the wx.request() method to Initiate a refund request. The specific code is as follows:

wx.request({
  url: 'https://your_domain.com/refund.php',
  method: 'POST',
  data: {
    'out_trade_no': '订单号',
    'refund_fee': '退款金额'
  },
  success: function(res){
     // 退款成功的回调函数
  },
  fail: function(res){
     // 退款失败的回调函数
  }
})
Copy after login
  1. Server-side development

On the server side, we need to use PHP to handle the refund request. The specific code is as follows:

<?php
  $appid = 'your_appid'; // 小程序的appid
  $mch_id = 'your_mch_id'; // 商户号
  $key = 'your_key'; // 商户密钥
  $out_trade_no = '订单号';
  $refund_fee = '退款金额';

  $params = array(
    'appid' => $appid,
    'mch_id' => $mch_id,
    'nonce_str' => md5(rand()),
    'out_trade_no' => $out_trade_no,
    'out_refund_no' => $out_trade_no,
    'total_fee' => $refund_fee,
    'refund_fee' => $refund_fee
  );

  ksort($params);
  $string = http_build_query($params);
  $string = urldecode($string).'&key='.$key;
  $sign = strtoupper(md5($string));

  $params['sign'] = $sign;

  $xml = '<xml>';
  foreach ($params as $key => $value) {
    $xml .= '<'.$key.'>'.$value.'</'.$key.'>';
  }
  $xml .= '</xml>';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.mch.weixin.qq.com/secapi/pay/refund');
  curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
  curl_setopt($ch, CURLOPT_SSLCERT, 'cert.pem');
  curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
  curl_setopt($ch, CURLOPT_SSLKEY, 'key.pem');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

  $result = simplexml_load_string($result);

  if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
    // 退款成功的处理逻辑
  }
?>
Copy after login

In this code, we first need to generate a signature based on the merchant account and key, splice the relevant parameters into an XML format string, and send the refund request to the WeChat payment interface through cURL. After receiving the return result, we can determine whether the refund is successful based on the return_code and result_code in the result.

3. Summary

Through the above steps, we can use PHP and small programs to develop online payment and refund functions. In practical applications, the functions can also be expanded according to specific needs, such as adding order inquiry, refund inquiry and other functions. I hope this article will be helpful to you when using PHP and mini programs to develop online payment and refund functions.

The above is the detailed content of Guide to Online Payment and Refund Processing with PHP and Mini Programs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template