config)) { $this->config = config('baofu'); } if (is_null($this->config)) { throw new ApiException('宝付未配置', 'ERROR_CONFIG'); } return $this->config; } private function getBaseUrl() { return $this->getConfig()['baseUrl'] ?: ''; } private function getMerchantNo() { return $this->getConfig()['merchantNo'] ?: ''; } public function getOrgNo() { return $this->getConfig()['orgNo'] ?: ''; } private function getTerminalNo() { return $this->getConfig()['terminalNo'] ?: ''; } private function getPrivateKeyPassword() { return $this->getConfig()['privateKeyPassword'] ?: ''; } private function getSimIDWx() { return $this->getConfig()['simIDWx'] ?: ''; } private function getSimIDAli() { return $this->getConfig()['simIDAli'] ?: ''; } private function getChannelId() { return $this->getConfig()['channelId'] ?: ''; } private function getChannelName() { return $this->getConfig()['channelName'] ?: ''; } private function getSimID($type){ switch($type){ case 'ALIPAY_ALSHH': case 'ALIPAY_ALXCX': case 'ALIPAY_NATIVE': $simId = $this->getSimIDAli(); break; case 'WECHAT_JSGZH': case 'WECHAT_JSXCX': $simId = $this->getSimIDWx(); break; default: throw new ApiException('未选择类型', 'ERROR_SIMID'); break; } return $simId; } private function getPfxFilePath() { return $this->getConfig()['pfxFilePath'] ?: ''; } private function getCerFilePath() { return $this->getConfig()['cerFilePath'] ?: ''; } private function pc($pcName, $data) { $pcItem = PcList::get($pcName); if (is_null($pcItem)) { throw new ApiException('未定义PC接口', 'ERROR_API'); } $response = $this->post($data, $pcItem, 'form'); return $this->parseResponse($response, 'pc'); } private function h5($h5Name, $data, $token) { $h5Item = H5List::get($h5Name); if (is_null($h5Item)) { throw new ApiException('未定义H5接口', 'ERROR_API'); } $response = $this->post($data, $h5Item); return $this->withReturnUrl($this->parseResponse($response, 'h5'), $token); } private function api($apiName, $data, $token = null) { $apiItem = ApiList::get($apiName); if (is_null($apiItem)) { throw new ApiException('未定义API接口', 'ERROR_API'); } if ($apiName == 'enterpriseUnbindCard') { $response = $this->post($data, $apiItem, 'json'); $res = $this->parseResponse($response, 'api'); return $this->withReturnUrl($res['checkPwdUrl'], $token); }else{ $response = $this->post($data, $apiItem, 'json'); return $this->parseResponse($response, 'api'); } } private function post($data, $apiOrH5Item, $dataType = 'form') { $url = $this->getBaseUrl() . $apiOrH5Item['url']; $data = $this->buildData($data, $apiOrH5Item); $response = ''; try { Log::info('Baofu Request Url: '. $url); Log::info('Baofu Request Data: ', $data); $response = HttpClient::post($url, $data, $dataType); Log::info('Baofu Response: '. $response); } catch (Exception $e) { Log::info('Baofu Request Error: ' . $e->getMessage()); throw new ApiException('网络错误', 'ERROR_NETWORK'); } return $response; } private function parseResponse($response, $mode) { $result = json_decode($response, true); if (is_null($result)) { throw new ApiException('接口返回数据异常', 'ERROR_RESPONSE'); } if ($mode == 'api') { if (!$result['success']) { throw new ApiException($result['errorMsg'], $result['errorCode']); } return $result['result']; } elseif ($mode == 'pc') { if (!$result['success']) { throw new ApiException($result['errorMsg'], $result['errorCode']); } return $result['result']; } elseif ($mode == 'h5') { if ($result['retCode'] != 'SUCCESS') { throw new ApiException($result['retMsg'], $result['retCode']); } return $result['redirectUrl']; } throw new ApiException('error parse response mode!!!', 'ERROR_MODE'); } private function buildData($data, $apiOrH5Item) { $needSignParams = $apiOrH5Item['signParams']; $commonData = []; $commonData['orgNo'] = $this->getOrgNo(); $commonData['merchantNo'] = $this->getMerchantNo(); $commonData['terminalNo'] = $this->getTerminalNo(); $commonData['requestDate'] = Tool::getTime(); if (isset($apiOrH5Item['callType'])) { $commonData['callType'] = $apiOrH5Item['callType']; } $data = array_merge($commonData, $data); return $this->withSignData($data, $needSignParams); } private function withSignData($data, $needSignParams) { $signData = $this->sign($data, $needSignParams); $data['signData'] = $signData; return $data; } private function sign($data, $needSignParams) { $paramList = explode('|', $needSignParams); $signString = Tool::getSignStr($needSignParams, $paramList, $data, 'request'); Log::info('Baofu SignString: ' . $signString); return SignatureUtil::sign($signString, $this->getPfxFilePath(), $this->getPrivateKeyPassword()); } public function notifyVerify($data, $notifyName) { $notifyItem = NotifyList::get($notifyName); if (is_null($notifyItem)) { throw new ApiException('回调未定义', 'ERROR_NOTIFY'); } $needSignParams = $notifyItem['signParams']; $paramList = explode('|', $needSignParams); $signString = Tool::getSignStr($needSignParams, $paramList, $data, 'response'); return SignatureUtil::verifySign($signString, $this->getCerFilePath(), $data['signature'] ?: ''); } public function notifySuccess() { return 'ok'; } public function withReturnUrl($url, $token) { return $url . '?redirect_url=' . urlencode(env('WEB_HOST') . '/return/' . $token); } }