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; } } }