You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Helper/Baofu/Common.php

230 lines
7.0 KiB
PHTML

1 year ago
<?php
namespace App\Helper\Baofu;
1 year ago
use App\Exception\ApiException;
use App\Helper\Log;
use Exception;
1 year ago
trait Common
{
private $config;
private function getConfig()
{
if (is_null($this->config)) {
1 year ago
$this->config = config('baofu');
1 year ago
}
if (is_null($this->config)) {
1 year ago
throw new ApiException('宝付未配置', 'ERROR_CONFIG');
1 year ago
}
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:
1 year ago
throw new ApiException('未选择类型', 'ERROR_SIMID');
1 year ago
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)) {
1 year ago
throw new ApiException('未定义PC接口', 'ERROR_API');
1 year ago
}
$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)) {
1 year ago
throw new ApiException('未定义H5接口', 'ERROR_API');
1 year ago
}
$response = $this->post($data, $h5Item);
return $this->withReturnUrl($this->parseResponse($response, 'h5'), $token);
}
1 year ago
private function api($apiName, $data, $token = null)
1 year ago
{
$apiItem = ApiList::get($apiName);
if (is_null($apiItem)) {
1 year ago
throw new ApiException('未定义API接口', 'ERROR_API');
1 year ago
}
1 year ago
if ($apiName == 'enterpriseUnbindCard') {
1 year ago
$response = $this->post($data, $apiItem, 'json');
$res = $this->parseResponse($response, 'api');
1 year ago
return $this->withReturnUrl($res['checkPwdUrl'], $token);
1 year ago
}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 {
1 year ago
Log::info('Baofu Request Url: '. $url);
Log::info('Baofu Request Data: ', $data);
1 year ago
$response = HttpClient::post($url, $data, $dataType);
1 year ago
Log::info('Baofu Response: '. $response);
} catch (Exception $e) {
Log::info('Baofu Request Error: ' . $e->getMessage());
throw new ApiException('网络错误', 'ERROR_NETWORK');
1 year ago
}
return $response;
}
private function parseResponse($response, $mode)
{
$result = json_decode($response, true);
if (is_null($result)) {
1 year ago
throw new ApiException('接口返回数据异常', 'ERROR_RESPONSE');
1 year ago
}
if ($mode == 'api') {
if (!$result['success']) {
1 year ago
throw new ApiException($result['errorMsg'], $result['errorCode']);
1 year ago
}
return $result['result'];
} elseif ($mode == 'pc') {
if (!$result['success']) {
1 year ago
throw new ApiException($result['errorMsg'], $result['errorCode']);
1 year ago
}
return $result['result'];
} elseif ($mode == 'h5') {
if ($result['retCode'] != 'SUCCESS') {
1 year ago
throw new ApiException($result['retMsg'], $result['retCode']);
1 year ago
}
return $result['redirectUrl'];
}
1 year ago
throw new ApiException('error parse response mode!!!', 'ERROR_MODE');
1 year ago
}
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');
1 year ago
Log::info('Baofu SignString: ' . $signString);
1 year ago
return SignatureUtil::sign($signString, $this->getPfxFilePath(), $this->getPrivateKeyPassword());
}
public function notifyVerify($data, $notifyName)
{
$notifyItem = NotifyList::get($notifyName);
if (is_null($notifyItem)) {
1 year ago
throw new ApiException('回调未定义', 'ERROR_NOTIFY');
1 year ago
}
$needSignParams = $notifyItem['signParams'];
$paramList = explode('|', $needSignParams);
$signString = Tool::getSignStr($needSignParams, $paramList, $data, 'response');
1 year ago
Log::info('Baofu SignString: ' . $signString);
1 year ago
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);
}
}