WxpayServiceNotify.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. /**
  13. * 微信扫码支付回调
  14. * Class WxpayServiceNotify
  15. * @package service
  16. *
  17. * 调用实例
  18. *
  19. * $mchid = SystemConfig::getValue('pay_weixin_mchid'); //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
  20. $appid = SystemConfig::getValue('pay_weixin_appid'); //公众号APPID 通过微信支付商户资料审核后邮件发送
  21. $apiKey = SystemConfig::getValue('pay_weixin_key'); //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
  22. $wxPay = new WxpayServiceNotify($mchid,$appid,$apiKey);
  23. $result = $wxPay->notify();
  24. if($result){
  25. $data['is_pay'] = 1;
  26. $data['pay_time'] = time();
  27. $res = ThemeEnlistModel::edit($data,$result['out_trade_no'],'order_id');
  28. if($res)
  29. echo 'success';
  30. //完成你的逻辑
  31. //例如连接数据库,获取付款金额$result['cash_fee'],获取订单号$result['out_trade_no'],修改数据库中的订单状态等;
  32. }else{
  33. echo 'pay error';
  34. }
  35. */
  36. class WxpayServiceNotify
  37. {
  38. protected $mchid;
  39. protected $appid;
  40. protected $apiKey;
  41. public function __construct($mchid, $appid, $key)
  42. {
  43. $this->mchid = $mchid;
  44. $this->appid = $appid;
  45. $this->apiKey = $key;
  46. }
  47. public function notify()
  48. {
  49. $config = array(
  50. 'mch_id' => $this->mchid,
  51. 'appid' => $this->appid,
  52. 'key' => $this->apiKey,
  53. );
  54. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  55. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  56. if ($postObj === false) {
  57. die('parse xml error');
  58. }
  59. if ($postObj->return_code != 'SUCCESS') {
  60. die($postObj->return_msg);
  61. }
  62. if ($postObj->result_code != 'SUCCESS') {
  63. die($postObj->err_code);
  64. }
  65. $arr = (array)$postObj;
  66. unset($arr['sign']);
  67. if (self::getSign($arr, $config['key']) == $postObj->sign) {
  68. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  69. return $arr;
  70. }
  71. }
  72. /**
  73. * 获取签名
  74. */
  75. public static function getSign($params, $key)
  76. {
  77. ksort($params, SORT_STRING);
  78. $unSignParaString = self::formatQueryParaMap($params, false);
  79. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  80. return $signStr;
  81. }
  82. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  83. {
  84. $buff = "";
  85. ksort($paraMap);
  86. foreach ($paraMap as $k => $v) {
  87. if (null != $v && "null" != $v) {
  88. if ($urlEncode) {
  89. $v = urlencode($v);
  90. }
  91. $buff .= $k . "=" . $v . "&";
  92. }
  93. }
  94. $reqPar = '';
  95. if (strlen($buff) > 0) {
  96. $reqPar = substr($buff, 0, strlen($buff) - 1);
  97. }
  98. return $reqPar;
  99. }
  100. }