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

227 lines
6.9 KiB
PHP

<?php
namespace App\Helper\Baofu;
use App\Exception\BusinessException;
use think\Config;
use think\Log;
trait Common
{
private $config;
private function getConfig()
{
if (is_null($this->config)) {
$this->config = Config::get('baofu');
}
if (is_null($this->config)) {
throw new BusinessException('宝付未配置');
}
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 BusinessException('未选择类型');
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 BusinessException('未定义PC接口');
}
$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 BusinessException('未定义H5接口');
}
$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 BusinessException('未定义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::write('BaofuPost{time: ' . date('Y-m-d H:i:s') . ', url: '. $url . ', data: '. json_encode(new \ArrayObject($data)) . ', dataType: '. $dataType . '}');
$response = HttpClient::post($url, $data, $dataType);
Log::write('BaofuPost{response: '. $response . '}');
} catch (BusinessException $e) {
throw new BusinessException('网络错误');
}
return $response;
}
private function parseResponse($response, $mode)
{
$result = json_decode($response, true);
if (is_null($result)) {
throw new BusinessException('接口返回数据异常');
}
if ($mode == 'api') {
if (!$result['success']) {
throw new BusinessException($result['errorMsg']);
}
return $result['result'];
} elseif ($mode == 'pc') {
if (!$result['success']) {
throw new BusinessException($result['errorMsg']);
}
return $result['result'];
} elseif ($mode == 'h5') {
if ($result['retCode'] != 'SUCCESS') {
throw new BusinessException($result['retMsg']);
}
return $result['redirectUrl'];
}
throw new BusinessException('error parse response 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::write('BaofuPost{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 BusinessException('回调未定义');
}
$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);
}
}