Maison développement back-end tutoriel php PHP 微信支付类 demo_php实例

PHP 微信支付类 demo_php实例

Jun 07, 2016 pm 05:10 PM

一切尽在代码中,代码附有注释,欢迎大家参考。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

<&#63;php

class WxpayService

{

  protected $mchid;

  protected $appid;

  protected $key;

  public function __construct($mchid, $appid, $key)

  {

    $this->mchid = $mchid; // 微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送

    $this->appid = $appid; //公众号APPID 通过微信支付商户资料审核后邮件发送

    $this->key = $key;   //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥

  }

  /**

   * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid

   * @param float $totalFee 收款总费用 单位元

   * @param string $outTradeNo 唯一的订单号

   * @param string $orderName 订单名称

   * @param string $notifyUrl 支付结果通知url 不要有问号

   *   https://mp.weixin.qq.com/ 微信支付-开发配置-测试目录

   *   测试目录 http://mp.izhanlue.com/paytest/  最后需要斜线,(需要精确到二级或三级目录)

   * @return string

   */

  public function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp)

  {

    $config = array(

      'mch_id' => $this->mchid,

      'appid' => $this->appid,

      'key' => $this->key,

    );

    $unified = array(

      'appid' => $config['appid'],

      'attach' => '支付',             //商家数据包,原样返回

      'body' => $orderName,

      'mch_id' => $config['mch_id'],

      'nonce_str' => self::createNonceStr(),

      'notify_url' => $notifyUrl,

      'openid' => $openid,            //rade_type=JSAPI,此参数必传

      'out_trade_no' => $outTradeNo,

      'spbill_create_ip' => '127.0.0.1',

      'total_fee' => intval($totalFee * 100),       //单位 转为分

      'trade_type' => 'JSAPI',

    );

    $unified['sign'] = self::getSign($unified, $config['key']);

    $responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));

    /*

    <xml>

    <return_code><![CDATA[SUCCESS]]></return_code>

    <return_msg><![CDATA[OK]]></return_msg>

    <appid><![CDATA[wx00e5904efec77699]]></appid>

    <mch_id><![CDATA[1220647301]]></mch_id>

    <nonce_str><![CDATA[1LHBROsdmqfXoWQR]]></nonce_str>

    <sign><![CDATA[ACA7BC8A9164D1FBED06C7DFC13EC839]]></sign>

    <result_code><![CDATA[SUCCESS]]></result_code>

    <prepay_id><![CDATA[wx2015032016590503f1bcd9c30421762652]]></prepay_id>

    <trade_type><![CDATA[JSAPI]]></trade_type>

    </xml>

    */

    $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);

    if ($unifiedOrder === false) {

      die('parse xml error');

    }

    if ($unifiedOrder->return_code != 'SUCCESS') {

      die($unifiedOrder->return_msg);

    }

    if ($unifiedOrder->result_code != 'SUCCESS') {

      die($unifiedOrder->err_code);

      /*

      NOAUTH 商户无此接口权限

      NOTENOUGH 余额不足

      ORDERPAID 商户订单已支付

      ORDERCLOSED 订单已关闭

      SYSTEMERROR 系统错误

      APPID_NOT_EXIST   APPID不存在

      MCHID_NOT_EXIST MCHID不存在

      APPID_MCHID_NOT_MATCH appid和mch_id不匹配

      LACK_PARAMS 缺少参数

      OUT_TRADE_NO_USED 商户订单号重复

      SIGNERROR 签名错误

      XML_FORMAT_ERROR XML格式错误

      REQUIRE_POST_METHOD 请使用post方法

      POST_DATA_EMPTY post数据为空

      NOT_UTF8 编码格式错误

      */

    }

    //$unifiedOrder->trade_type 交易类型 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP

    //$unifiedOrder->prepay_id 预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时

    //$unifiedOrder->code_url 二维码链接 trade_type为NATIVE是有返回,可将该参数值生成二维码展示出来进行扫码支付

    $arr = array(

      "appId" => $config['appid'],

      "timeStamp" => $timestamp,

      "nonceStr" => self::createNonceStr(),

      "package" => "prepay_id=" . $unifiedOrder->prepay_id,

      "signType" => 'MD5',

    );

    $arr['paySign'] = self::getSign($arr, $config['key']);

    return $arr;

  }

  public function notify()

  {

    $config = array(

      'mch_id' => $this->mchid,

      'appid' => $this->appid,

      'key' => $this->key,

    );

    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    //error_log($postStr, 3, './str.txt');

    /*

    $postStr = '<xml>

    <appid><![CDATA[wx00e5904efec77699]]></appid>

    <attach><![CDATA[支付测试]]></attach>

    <bank_type><![CDATA[CMB_CREDIT]]></bank_type>

    <cash_fee><![CDATA[1]]></cash_fee>

    <fee_type><![CDATA[CNY]]></fee_type>

    <is_subscribe><![CDATA[Y]]></is_subscribe>

    <mch_id><![CDATA[1220647301]]></mch_id>

    <nonce_str><![CDATA[a0tZ41phiHm8zfmO]]></nonce_str>

    <openid><![CDATA[oU3OCt5O46PumN7IE87WcoYZY9r0]]></openid>

    <out_trade_no><![CDATA[550bf2990c51f]]></out_trade_no>

    <result_code><![CDATA[SUCCESS]]></result_code>

    <return_code><![CDATA[SUCCESS]]></return_code>

    <sign><![CDATA[F6F519B4DD8DB978040F8C866C1E6250]]></sign>

    <time_end><![CDATA[20150320181606]]></time_end>

    <total_fee>1</total_fee>

    <trade_type><![CDATA[JSAPI]]></trade_type>

    <transaction_id><![CDATA[1008840847201503200034663980]]></transaction_id>

    </xml>';

    */

    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

    if ($postObj === false) {

      die('parse xml error');

    }

    if ($postObj->return_code != 'SUCCESS') {

      die($postObj->return_msg);

    }

    if ($postObj->result_code != 'SUCCESS') {

      die($postObj->err_code);

    }

    $arr = (array)$postObj;

    unset($arr['sign']);

    if (self::getSign($arr, $config['key']) == $postObj->sign) {

      // $mch_id = $postObj->mch_id; //微信支付分配的商户号

      // $appid = $postObj->appid; //微信分配的公众账号ID

      // $openid = $postObj->openid; //用户在商户appid下的唯一标识

      // $transaction_id = $postObj->transaction_id;//微信支付订单号

      // $out_trade_no = $postObj->out_trade_no;//商户订单号

      // $total_fee = $postObj->total_fee; //订单总金额,单位为分

      // $is_subscribe = $postObj->is_subscribe; //用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效

      // $attach = $postObj->attach;//商家数据包,原样返回

      // $time_end = $postObj->time_end;//支付完成时间

      echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';

      return $postObj;

    }

  }

  /**

   * curl get

   *

   * @param string $url

   * @param array $options

   * @return mixed

   */

  public static function curlGet($url = '', $options = array())

  {

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    if (!empty($options)) {

      curl_setopt_array($ch, $options);

    }

    //https请求 不验证证书和host

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

  }

  public static function curlPost($url = '', $postData = '', $options = array())

  {

    if (is_array($postData)) {

      $postData = http_build_query($postData);

    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数

    if (!empty($options)) {

      curl_setopt_array($ch, $options);

    }

    //https请求 不验证证书和host

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

  }

  public static function createNonceStr($length = 16)

  {

    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    $str = '';

    for ($i = 0; $i < $length; $i++) {

      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

    }

    return $str;

  }

  public static function arrayToXml($arr)

  {

    $xml = "<xml>";

    foreach ($arr as $key => $val) {

      if (is_numeric($val)) {

        $xml .= "<" . $key . ">" . $val . "</" . $key . ">";

      } else

        $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";

    }

    $xml .= "</xml>";

    return $xml;

  }

  /**

   * 例如:

   * appid:  wxd930ea5d5a258f4f

   * mch_id:  10000100

   * device_info: 1000

   * Body:  test

   * nonce_str: ibuaiVcKdpRxkhJA

   * 第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:

   * stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i

   * d=10000100&nonce_str=ibuaiVcKdpRxkhJA";

   * 第二步:拼接支付密钥:

   * stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"

   * sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7"

   */

  public static function getSign($params, $key)

  {

    ksort($params, SORT_STRING);

    $unSignParaString = self::formatQueryParaMap($params, false);

    $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));

    return $signStr;

  }

  protected static function formatQueryParaMap($paraMap, $urlEncode = false)

  {

    $buff = "";

    ksort($paraMap);

    foreach ($paraMap as $k => $v) {

      if (null != $v && "null" != $v) {

        if ($urlEncode) {

          $v = urlencode($v);

        }

        $buff .= $k . "=" . $v . "&";

      }

    }

    $reqPar = '';

    if (strlen($buff) > 0) {

      $reqPar = substr($buff, 0, strlen($buff) - 1);

    }

    return $reqPar;

  }

}

Copier après la connexion

以上代码大家都能看得懂吧,有哪里不明白的地方欢迎给我留言,我会在第一时间和大家取得联系的。谢谢大家对脚本之家网站的支持。

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) 11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) Mar 03, 2025 am 10:49 AM

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium)

Travailler avec les données de session Flash dans Laravel Travailler avec les données de session Flash dans Laravel Mar 12, 2025 pm 05:08 PM

Travailler avec les données de session Flash dans Laravel

Misque de réponse HTTP simplifié dans les tests Laravel Misque de réponse HTTP simplifié dans les tests Laravel Mar 12, 2025 pm 05:09 PM

Misque de réponse HTTP simplifié dans les tests Laravel

Introduction à l'API Instagram Introduction à l'API Instagram Mar 02, 2025 am 09:32 AM

Introduction à l'API Instagram

Construisez une application React avec un Laravel Back End: Partie 2, React Construisez une application React avec un Laravel Back End: Partie 2, React Mar 04, 2025 am 09:33 AM

Construisez une application React avec un Laravel Back End: Partie 2, React

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Mar 14, 2025 am 11:42 AM

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST

12 meilleurs scripts de chat PHP sur Codecanyon 12 meilleurs scripts de chat PHP sur Codecanyon Mar 13, 2025 pm 12:08 PM

12 meilleurs scripts de chat PHP sur Codecanyon

Annonce de l'enquête sur la situation en 2025 PHP Annonce de l'enquête sur la situation en 2025 PHP Mar 03, 2025 pm 04:20 PM

Annonce de l'enquête sur la situation en 2025 PHP

See all articles