PHP wechat payment function example

Time:2020-5-21

The example of this article describes the PHP wechat payment function. To share with you for your reference, as follows:

Wechat development SDK: PHP is used in this article_ sdk_ v3.0.9 : https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

import("wxpay.lib.AppPay#Config", EXTEND_PATH, ".php");
import("wxpay.lib.WxPay#Api", EXTEND_PATH, ".php");
//$this->weixinpay();
$config = new \AppPayConfig();
//Order No
$appId = $config->GetAppId();
$key = $config->GetKey();
$money = 0.01 * 100;
$orderSn = 'M201810241754107557';//Order No;
$curTime = time();
$input = new \WxPayUnifiedOrder();
$input - > setbody ("user red packet");
$input->SetOut_trade_no($orderSn);
$input->SetTotal_fee($money);
$input->SetTrade_type("APP");
$unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
if ($unifiedOrder['result_code'] == 'SUCCESS' && $unifiedOrder['return_code'] == 'SUCCESS') {
  /*$unifiedorder consists of:
   * ["appid"] => string(18) "wx2xxxx49"
  ["mch_id"] => string(10) "1xxxxxx01"
  ["nonce_str"] => string(16) "1GnEUXTuTcFtJVtb"
  ["prepay_id"] => string(36) "wx241933xxx68fa24ece70611692955"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(32) "7903xxxxx4710E460CB156"
  ["trade_type"] => string(3) "APP"*/
  $curTime = time();
  //Part I
  $wxpayResult = new \WxPayResults();
  $wxpayResult->SetData('appid', $unifiedOrder['appid']);
  $wxpayResult->SetData('partnerid', $unifiedOrder['mch_id']);
  $wxpayResult->SetData('prepayid', $unifiedOrder['prepay_id']);
  $wxpayResult->SetData('noncestr', $unifiedOrder['nonce_str']);
  $wxpayResult->SetData('timestamp',$curTime);
  $wxpayResult->SetData('package',"Sign=WXPay");
  $wxpayResult->SetData('sign',"");
  $wxpayResult->SetSign($config);
  $data = $wxpayResult->GetValues();
  //Part II
  $data['appid'] = $unifiedOrder['appid'];
  $data['partnerid'] = $unifiedOrder['mch_id'];
  $data['prepayid'] = $unifiedOrder['prepay_id'];
  $data['noncestr'] = $unifiedOrder['nonce_str'];;
  $data['timestamp'] = $curTime;
  $data['package'] = "Sign=WXPay";
  $data['sign'] = $this->wxappPaySign ($key, $data); //$unifiedOrder['sign']; // signature; the specific signature scheme is referred to WeChat official account for payment assistance documents;
  $this->returnSuccess($data);
} else {
  Log::write(var_export($unifiedOrder, true));
  $this - > returnerror (300, 'wechat payment failed');
}
private function wxappPaySign($key,$parameters)
{
  $sign = md5(sprintf("appid=%s&noncestr=%s&package=%s&partnerid=%s&prepayid=%s&timestamp=%s&key=%s",
    $parameters['appid'],
    $parameters['noncestr'],
    $parameters['package'],
    $parameters['partnerid'],
    $parameters['prepayid'],
    $parameters['timestamp'],
    $key));
  return strtoupper($sign);
}

The functions of the first part and the second part are the same.

Official development resources

For more information about PHP, readers who are interested in PHP, please refer to the following topics: summary of PHP wechat development skills, summary of PHP curl usage, summary of PHP network programming skills, summary of PHP string usage, summary of JSON format data operation skills in PHP and summary of PHP operation skills for XML files

I hope that this article will be helpful for PHP programming.