| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\controllers;
- use app\utils\QrCode;
- use app\utils\Wechat\WechatProfit;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use GuzzleHttp\Exception\GuzzleException;
- use Think\Exception;
- use app\models\WechatConfig;
- use app\models\Option;
- class WechatNativeController extends WechatProfit
- {
- public $mch_id;
- public $private_key;
- public $certificate;
- public $certificate_serial_no;
- public $http;
- public $secret_key;
- public $config;
- // TODO: 微信走统一的订单回调接口
- public static $notify_url = 'wechat/notify';
- /**
- * @var wechat对象配置
- */
- public static $wechat_config;
- public function inits($order)
- {
- try {
- //parent::init(0,0,1);
- //parent::initSaas(WechatProfit::WECHAT_KIND_PAY);
- self::$wechat_config = new WechatConfig();
- $keys = [
- 'platform_appid',
- 'platform_mch_id',
- 'platform_key',
- 'platform_pay_key',
- 'platform_apiclient_cert',
- 'platform_apiclient_key',
- 'platform_api_key',
- ];
- $data = Option::get($keys, 0, 'saas');
- $data = array_column($data, 'value','name');
- self::$wechat_config->app_id = $data['platform_appid'];
- self::$wechat_config->app_secret = $data['platform_key'];
- self::$wechat_config->mch_id = $data['platform_mch_id'];
- self::$wechat_config->pay_key = $data['platform_pay_key'];
- self::$wechat_config->cert_pem = $data['platform_apiclient_cert'];
- self::$wechat_config->key_pem = $data['platform_apiclient_key'];
- $config = [
- 'app_id' => self::$wechat_config->app_id,
- 'secret' => self::$wechat_config->app_secret,
- 'response_type' => 'array'
- ];
- // 证书
- if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
- mkdir(\Yii::$app->runtimePath . '/pem');
- file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
- }
- $cert_pem_file = null;
- if (self::$wechat_config->cert_pem) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->cert_pem);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, self::$wechat_config->cert_pem);
- }
- }
- $key_pem_file = null;
- if (self::$wechat_config->key_pem) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->key_pem);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, self::$wechat_config->key_pem);
- }
- }
- $config['key'] = self::$wechat_config->pay_key;
- $config['mch_id'] = self::$wechat_config->mch_id;
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- self::$wechat_pay = Factory::payment($config);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- ];
- }
- $data = [
- 'appid' => self::$wechat_pay->config->app_id,
- 'mch_id' => self::$wechat_pay->config->mch_id,
- 'sub_mch_id' => self::$wechat_pay->config->sub_mch_id,
- 'out_trade_no' => $order->order_no,
- 'body' => $order->desc,
- //'notify_url' => pay_notify_url(self::$notify_url),
- 'notify_url' => $this->new_pay_url(self::$notify_url),
- 'trade_type' => 'NATIVE',
- 'product_id' => 1,
- 'sign_type'=>"MD5",
- 'total_fee'=>$order->total_price*1*100,
- 'time_start'=>date("YmdHis",time()),
- 'time_expire'=>date("YmdHis",time()+60),
- ];
- $res = self::$wechat_pay->order->unify($data);
- if($res['return_code'] === "SUCCESS"){
- $filename = md5('wxNative_' . $order->store_id.time());
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $text = $res['code_url'];
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- return [
- 'code'=>0,
- 'name'=>$order->desc,
- 'order_id'=>$order->id,
- 'url'=>\Yii::$app->request->hostInfo .'/runtime/image/'. $filename . '.jpg',
- 'desc'=>"付款后,系统使用期限将延长一年",
- 'total_price'=>$order->total_price*1,
- 'data'=>$data,
- 'end_time'=>date("Y-m-d",strtotime('+1 year'))
- ];
- }else{
- return [
- 'code' => 0,
- 'msg' => $res['return_msg'],
- 'data' => $data
- ];
- }
- }
- public function index(){
- $app = Factory::payment($this->config);
- }
- private function new_pay_url($suffix){
- $hostInfo = \Yii::$app->request->hostInfo;
- if (\Yii::$app->request->getIsSecureConnection()) {
- $hostInfo = str_replace('http:', 'https:', $hostInfo);
- } else {
- $hostInfo = str_replace('https:', 'http:', $hostInfo);
- }
- $hostInfo .= '/index.php/' . $suffix . '/-1';
- return $hostInfo;
- }
- }
|