WechatNativeController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers;
  8. use app\utils\QrCode;
  9. use app\utils\Wechat\WechatProfit;
  10. use EasyWeChat\Factory;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  13. use GuzzleHttp\Exception\GuzzleException;
  14. use Think\Exception;
  15. use app\models\WechatConfig;
  16. use app\models\Option;
  17. class WechatNativeController extends WechatProfit
  18. {
  19. public $mch_id;
  20. public $private_key;
  21. public $certificate;
  22. public $certificate_serial_no;
  23. public $http;
  24. public $secret_key;
  25. public $config;
  26. // TODO: 微信走统一的订单回调接口
  27. public static $notify_url = 'wechat/notify';
  28. /**
  29. * @var wechat对象配置
  30. */
  31. public static $wechat_config;
  32. public function inits($order)
  33. {
  34. try {
  35. //parent::init(0,0,1);
  36. //parent::initSaas(WechatProfit::WECHAT_KIND_PAY);
  37. self::$wechat_config = new WechatConfig();
  38. $keys = [
  39. 'platform_appid',
  40. 'platform_mch_id',
  41. 'platform_key',
  42. 'platform_pay_key',
  43. 'platform_apiclient_cert',
  44. 'platform_apiclient_key',
  45. 'platform_api_key',
  46. ];
  47. $data = Option::get($keys, 0, 'saas');
  48. $data = array_column($data, 'value','name');
  49. self::$wechat_config->app_id = $data['platform_appid'];
  50. self::$wechat_config->app_secret = $data['platform_key'];
  51. self::$wechat_config->mch_id = $data['platform_mch_id'];
  52. self::$wechat_config->pay_key = $data['platform_pay_key'];
  53. self::$wechat_config->cert_pem = $data['platform_apiclient_cert'];
  54. self::$wechat_config->key_pem = $data['platform_apiclient_key'];
  55. $config = [
  56. 'app_id' => self::$wechat_config->app_id,
  57. 'secret' => self::$wechat_config->app_secret,
  58. 'response_type' => 'array'
  59. ];
  60. // 证书
  61. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  62. mkdir(\Yii::$app->runtimePath . '/pem');
  63. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  64. }
  65. $cert_pem_file = null;
  66. if (self::$wechat_config->cert_pem) {
  67. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->cert_pem);
  68. if (!file_exists($cert_pem_file)) {
  69. file_put_contents($cert_pem_file, self::$wechat_config->cert_pem);
  70. }
  71. }
  72. $key_pem_file = null;
  73. if (self::$wechat_config->key_pem) {
  74. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->key_pem);
  75. if (!file_exists($key_pem_file)) {
  76. file_put_contents($key_pem_file, self::$wechat_config->key_pem);
  77. }
  78. }
  79. $config['key'] = self::$wechat_config->pay_key;
  80. $config['mch_id'] = self::$wechat_config->mch_id;
  81. $config['cert_path'] = $cert_pem_file;
  82. $config['key_path'] = $key_pem_file;
  83. self::$wechat_pay = Factory::payment($config);
  84. } catch (\Exception $e) {
  85. return [
  86. 'code' => 1,
  87. 'msg' => $e->getMessage(),
  88. 'file' => $e->getFile(),
  89. 'line' => $e->getLine(),
  90. ];
  91. }
  92. $data = [
  93. 'appid' => self::$wechat_pay->config->app_id,
  94. 'mch_id' => self::$wechat_pay->config->mch_id,
  95. 'sub_mch_id' => self::$wechat_pay->config->sub_mch_id,
  96. 'out_trade_no' => $order->order_no,
  97. 'body' => $order->desc,
  98. //'notify_url' => pay_notify_url(self::$notify_url),
  99. 'notify_url' => $this->new_pay_url(self::$notify_url),
  100. 'trade_type' => 'NATIVE',
  101. 'product_id' => 1,
  102. 'sign_type'=>"MD5",
  103. 'total_fee'=>$order->total_price*1*100,
  104. 'time_start'=>date("YmdHis",time()),
  105. 'time_expire'=>date("YmdHis",time()+60),
  106. ];
  107. $res = self::$wechat_pay->order->unify($data);
  108. if($res['return_code'] === "SUCCESS"){
  109. $filename = md5('wxNative_' . $order->store_id.time());
  110. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  111. $text = $res['code_url'];
  112. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  113. return [
  114. 'code'=>0,
  115. 'name'=>$order->desc,
  116. 'order_id'=>$order->id,
  117. 'url'=>\Yii::$app->request->hostInfo .'/runtime/image/'. $filename . '.jpg',
  118. 'desc'=>"付款后,系统使用期限将延长一年",
  119. 'total_price'=>$order->total_price*1,
  120. 'data'=>$data,
  121. 'end_time'=>date("Y-m-d",strtotime('+1 year'))
  122. ];
  123. }else{
  124. return [
  125. 'code' => 0,
  126. 'msg' => $res['return_msg'],
  127. 'data' => $data
  128. ];
  129. }
  130. }
  131. public function index(){
  132. $app = Factory::payment($this->config);
  133. }
  134. private function new_pay_url($suffix){
  135. $hostInfo = \Yii::$app->request->hostInfo;
  136. if (\Yii::$app->request->getIsSecureConnection()) {
  137. $hostInfo = str_replace('http:', 'https:', $hostInfo);
  138. } else {
  139. $hostInfo = str_replace('https:', 'http:', $hostInfo);
  140. }
  141. $hostInfo .= '/index.php/' . $suffix . '/-1';
  142. return $hostInfo;
  143. }
  144. }