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