| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Option;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use EasyWeChat\Kernel\Exceptions\RuntimeException;
- use yii\base\Model;
- class NewWechatPay extends Model
- {
- public $wechat_config;
- public $wechat;
- public $wechatPay;
- public function __construct($config = [])
- {
- parent::__construct($config);
- if ((int)get_store_id() > 0) {
- $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id()]);
- } else {
- $this->wechat_config = (object)[
- 'app_id' => Option::get('platform_appid', 0, 'saas')['value'],
- 'app_secret' => Option::get('platform_key', 0, 'saas')['value'],
- 'mch_id' => Option::get('platform_mch_id', 0, 'saas')['value'],
- ];
- }
- $config = [
- 'app_id' => $this->wechat_config->app_id,
- 'secret' => $this->wechat_config->app_secret,
- 'response_type' => 'array',
- ];
- $this->wechat = Factory::miniProgram($config);
- }
- public function newWxPay($args)
- {
- // 新版微信公众平台支付
- //商品名称处理
- $body = json_encode($args['body']);
- if (strlen($body) >= 126) {
- $arr = explode(';', $body);
- if (strlen($arr[0]) >= 123) {
- $str = substr($arr[0], 0, 123);
- $num = strrpos($str, '\\');
- $str = substr($arr[0], 0, $num);
- } else {
- $str = $arr[0];
- }
- $body = $str . "…";
- }
- $data = [
- 'openid' => $args['openid'],
- 'combine_trade_no' => time().''.rand(10000, 999999),
- 'expire_time' => time(),
- 'sub_orders' => [
- [
- 'mchid' => $this->wechat_config->mch_id,
- 'amount' => $args['total_fee'],
- 'trade_no' => $args['out_trade_no'],
- 'description' => $body
- ]
- ]
- ];
- try {
- $access_token = $this->wechat->access_token->getToken();
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $api = "https://api.weixin.qq.com/shop/pay/createorder?access_token=" . $access_token['access_token'];
- \Yii::error('api = ' . $api);
- //$data = json_encode($data, JSON_UNESCAPED_UNICODE);
- //$this->wechat->curl->post($api, $data);
- $res = json_decode(cloud_post($api, $data),true);
- \Yii::error('.' . json_encode($res));
- if ($res['errcode'] === 0) {
- $result = $res['payment_params'];
- $result['code'] = 0;
- $result['result_code'] = 'SUCCESS';
- $result['return_code'] = 'SUCCESS';
- $result['return_msg'] = strtoupper($res['errmsg']);
- $result['prepay_id'] = trim($result['package'], 'prepay_id=');
- $result['timeStamp'] = strval($result['timeStamp']);
- return $result;
- } elseif ($res['errcode'] == 400) {
- $result = $res['payment_params'];
- $result['code'] = 1;
- $result['result_code'] = 'error';
- $result['return_code'] = 'SUCCESS';
- $result['err_code'] = 'INVALID_REQUEST';
- $result['err_code_des'] = $res['errmsg'];
- $result['prepay_id'] = trim($result['package'], 'prepay_id=');
- return $result;
- } else {
- $result = $res['payment_params'];
- $result['code'] = 1;
- $result['result_code'] = 'error';
- $result['return_code'] = 'error';
- $result['return_msg'] = $res['errmsg'];
- $result['err_code_des'] = $res['errmsg'];
- return $result;
- }
- }
- }
|