'pay_data'], [['pay_id'], 'required', 'on' => 'balance_pay'], ]; } public function payData() { if(!$this->validate()){ return $this->getModelError(); } try{ if(empty($this->code)){ $openid = ''; }else{ $res = $this->code2session($this->code); if($res['code'] != 0){ $openid = ''; }else{ $openid = $res['data']['openid']; } $user_id = \Yii::$app->user->identity->id; if(!empty($user_id) && !empty($openid)){ $user_oauth = UserOauth::findOne(['user_id' => $user_id,'is_delete' => 0,'type' => SysConst::$cxOauthProviderWxmp]); if($user_oauth != null){ $user_oauth->openid = $openid; $user_oauth->created_at = time(); $user_oauth->save(); } } } $payment = new Payment(); $data = $payment->getPayData($this->pay_id, $this->pay_type, $this->cx_mch_id,$openid); } catch (\Exception $ex){ $data = $this->apiReturnError($ex->getMessage()); } catch (\yii\base\ErrorException $ex){ $data = $this->apiReturnError($ex->getMessage()); } return $data; } public function balancePay() { if(!$this->validate()){ return $this->getModelError(); } try{ $payment = new Payment(); $data = $payment->payBuyBalance($this->pay_id); } catch (\Exception $ex){ $data = $this->apiReturnError($ex->getMessage()); } return $data; } public function integralPay() { if(!$this->validate()){ return $this->getModelError(); } try{ $payment = new Payment(); $data = $payment->payBuyIntegral($this->pay_id); } catch (\Exception $ex){ $data = $this->apiReturnError($ex->getMessage()); } return $data; } private function code2session($code) { $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code"; $this->wechat_mp->curl->get($api); if($this->wechat_mp->curl->error_code != 0){ return [ 'code' => 1, 'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}" ]; } $resp = $this->wechat_mp->curl->response; $res = json_decode($resp, true); if(!isset($res['openid'])){ return [ 'code' => 1, 'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error' ]; } return [ 'code' => 0, 'msg' => 'ok', 'data' => $res ]; } /*** * 用户数据解密 */ private function decrypted_data($session_key){ $pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key); $errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data ); if ($errCode == 0) { return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } else { return [ 'code' => 1, 'msg' => $errCode, ]; } } }