NewWechatPay.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\Option;
  9. use app\models\WechatConfig;
  10. use EasyWeChat\Factory;
  11. use EasyWeChat\Kernel\Exceptions\HttpException;
  12. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  13. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  14. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  15. use yii\base\Model;
  16. class NewWechatPay extends Model
  17. {
  18. public $wechat_config;
  19. public $wechat;
  20. public $wechatPay;
  21. public function __construct($config = [])
  22. {
  23. parent::__construct($config);
  24. if ((int)get_store_id() > 0) {
  25. $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id()]);
  26. } else {
  27. $this->wechat_config = (object)[
  28. 'app_id' => Option::get('platform_appid', 0, 'saas')['value'],
  29. 'app_secret' => Option::get('platform_key', 0, 'saas')['value'],
  30. 'mch_id' => Option::get('platform_mch_id', 0, 'saas')['value'],
  31. ];
  32. }
  33. $config = [
  34. 'app_id' => $this->wechat_config->app_id,
  35. 'secret' => $this->wechat_config->app_secret,
  36. 'response_type' => 'array',
  37. ];
  38. $this->wechat = Factory::miniProgram($config);
  39. }
  40. public function newWxPay($args)
  41. {
  42. // 新版微信公众平台支付
  43. //商品名称处理
  44. $body = json_encode($args['body']);
  45. if (strlen($body) >= 126) {
  46. $arr = explode(';', $body);
  47. if (strlen($arr[0]) >= 123) {
  48. $str = substr($arr[0], 0, 123);
  49. $num = strrpos($str, '\\');
  50. $str = substr($arr[0], 0, $num);
  51. } else {
  52. $str = $arr[0];
  53. }
  54. $body = $str . "…";
  55. }
  56. $data = [
  57. 'openid' => $args['openid'],
  58. 'combine_trade_no' => time().''.rand(10000, 999999),
  59. 'expire_time' => time(),
  60. 'sub_orders' => [
  61. [
  62. 'mchid' => $this->wechat_config->mch_id,
  63. 'amount' => $args['total_fee'],
  64. 'trade_no' => $args['out_trade_no'],
  65. 'description' => $body
  66. ]
  67. ]
  68. ];
  69. try {
  70. $access_token = $this->wechat->access_token->getToken();
  71. } catch (\Exception $e) {
  72. return [
  73. 'code' => 1,
  74. 'msg' => $e->getMessage()
  75. ];
  76. }
  77. $api = "https://api.weixin.qq.com/shop/pay/createorder?access_token=" . $access_token['access_token'];
  78. \Yii::error('api = ' . $api);
  79. //$data = json_encode($data, JSON_UNESCAPED_UNICODE);
  80. //$this->wechat->curl->post($api, $data);
  81. $res = json_decode(cloud_post($api, $data),true);
  82. \Yii::error('.' . json_encode($res));
  83. if ($res['errcode'] === 0) {
  84. $result = $res['payment_params'];
  85. $result['code'] = 0;
  86. $result['result_code'] = 'SUCCESS';
  87. $result['return_code'] = 'SUCCESS';
  88. $result['return_msg'] = strtoupper($res['errmsg']);
  89. $result['prepay_id'] = trim($result['package'], 'prepay_id=');
  90. $result['timeStamp'] = strval($result['timeStamp']);
  91. return $result;
  92. } elseif ($res['errcode'] == 400) {
  93. $result = $res['payment_params'];
  94. $result['code'] = 1;
  95. $result['result_code'] = 'error';
  96. $result['return_code'] = 'SUCCESS';
  97. $result['err_code'] = 'INVALID_REQUEST';
  98. $result['err_code_des'] = $res['errmsg'];
  99. $result['prepay_id'] = trim($result['package'], 'prepay_id=');
  100. return $result;
  101. } else {
  102. $result = $res['payment_params'];
  103. $result['code'] = 1;
  104. $result['result_code'] = 'error';
  105. $result['return_code'] = 'error';
  106. $result['return_msg'] = $res['errmsg'];
  107. $result['err_code_des'] = $res['errmsg'];
  108. return $result;
  109. }
  110. }
  111. }