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/Baofu.php

588 lines
23 KiB
PHP

<?php
namespace App\Helper\Baofu;
use App\Exception\BusinessException;
use App\Helper\StringHelper;
class Baofu
{
use Common;
const OK = 'OK';
const FAIL = 'FAIL';
const PEYMENT_PAID_TYPE = [
'WECHAT_JSGZH' => 2,//'微信公众号支付',
// 'WECHAT_JSXCX' => '微信小程序支付',
// 'ALIPAY_ALSHH' => '支付宝生活号支付',
// 'ALIPAY_ALXCX' => '支付宝小程序支付',
'ALIPAY_NATIVE' => 1 //'支付宝主扫'
];
public function apply($loginNo, $email, $token) {
$data = [];
$data['loginNo'] = $loginNo;
$data['email'] = $email;
$data['notifyUrl'] = NotifyList::getNotifyUrl('apply', $token);
return $this->pc('apply', $data);
}
public function register($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['dataContent'] = NotifyList::getNotifyUrl('register', $token);
return $this->h5('register', $data, $token);
}
public function cardBind($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['dataContent'] = NotifyList::getNotifyUrl('card-bind', $token);
return $this->h5('card-bind', $data, $token);
}
public function cardUnbind($loginNo, $agreementNo, $requestNo = '')
{
if (empty($requestNo)) {
$requestNo = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_UNBIND_CARD);
}
$data = [];
$data['loginNo'] = $loginNo;
$data['requestNo'] = $requestNo;
$data['agreementNo'] = $agreementNo;
return $this->api('unbind-card', $data);
}
public function getBalance($loginNo)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['accountType'] = 'BALANCE';
return $this->api('get-balance-new', $data);
}
public function withdraw($params, $token)
{
$data = [];
$data['amount'] = $params['amount'];
$data['loginNo'] = $params['loginNo'];
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['outOrderNo'] = $params['outOrderNo'];
$data['notifyUrl'] = NotifyList::getNotifyUrl('withdraw', $token);
return $this->withReturnUrl($this->api('withdraw', $data), $token);
}
public function assurePaymentSplit($params, $token)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['amount'] = $params['amount'];
$data['outOrderNo'] = $params['outOrderNo'];
$data['paidType'] = $params['paidType'] ?: 'CARD';
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['splitInfoList'] = json_encode($params['splitInfoList']);
$data['expireDate'] = date('YmdHis', time() + 15*60);
$data['validDate'] = date('Ymd');
$data['notifyUrl'] = $params['notifyUrl'];
return $this->withReturnUrl($this->api('assure-payment-split', $data), $token);
}
public function paymentSplit($params, $token)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['amount'] = $params['amount'];
$data['outOrderNo'] = $params['outOrderNo'];
$data['paidType'] = $params['paidType'] ?: 'CARD';
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['splitInfoList'] = json_encode($params['splitInfoList']);
$data['pwdPayExpTime'] = $params['pwdPayExpTime'];
$data['notifyUrl'] = NotifyList::getNotifyUrl('payment', $token);
return $this->withReturnUrl($this->api('payment-split', $data), $token);
}
public function transferSplitPay($params, $token)
{
$dataContent = [
'goodsName' => $params['goodsName'],
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'agreementNo' => Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath()),
'notifyUrl' => NotifyList::getNotifyUrl('payment', $token),
'expireDate' => $params['expireDate'],
'validDate' => $params['validDate'],
'splitInfoList' => $params['splitInfoList'],
'marketInfo' => [
'marketAmount' => $params['marketInfo']['amount'],
'remark' => $params['marketInfo']['remark'],
],
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['callType'] = 'TRANSFER_ASSURE_PORTFOLIO_PAYMENT';
$data['dataContent'] = json_encode($dataContent, JSON_UNESCAPED_UNICODE);
$data['isDeductMarketAmount'] = '0';
return $this->api('transfer-split-pay', $data);
}
public function unifyPay($params)
{
$dataContent = [
'goodsName' => $params['goodsName'],
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'expireDate' => date('YmdHis', time() + 15*60),
'notifyUrl' => $params['notifyUrl'],
'splitInfoList' => $params['splitInfoList'],
'paidType' => $params['paidType'],
'validDate' => date('Ymd'),
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['orderType'] = 'SPLIT_PAY';
$data['dataContent'] = json_encode($dataContent);
// 1.微信支付(公众号、小程序)需填公众号授权获取微信用户openid
// 2.支付宝支付(生活号、小程序)需填生活号授权获取支付宝用户userid
// 3.支付宝主扫可不填写
$data['channelId'] = $params['channelId'] ?: '';
$data['forbidCredit'] = $params['forbidCredit'] ? 1 : 0;
$data['simId'] = $params['simId'] ?: $this->getSimID($params['paidType']);
$data['isRaw'] = 0;
$data['appId'] = $params['appId'] ?: '';
$data['anonymityPayRemark'] = $params['anonymityPayRemark'];
$res = base64_decode($this->api('unifyPay', $data)['data']);
if(self::PEYMENT_PAID_TYPE[$params['paidType']]){
$res = json_decode($res, true);
if($res['body']){
return $res['body'];
}
}
}
public function cloudB2cAssureRecharge($params){
$dataContent = [
'goodsName' => $params['goodsName'],
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'notifyUrl' => $params['notifyUrl'],
'splitInfoList' => $params['splitInfoList'],
'expireDate' => date('YmdHis', time() + 15*60),
'validDate' => date('Ymd'),
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['payId'] = $params['payId'];
$data['dataContent'] = json_encode($dataContent);
return $this->api('cloud-b2c-assure-recharge', $data);
}
public function payment($params, $token)
{
$dataContent = [
'goodsName' => $params['goodsName'],
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'expireDate' => $params['expireDate'],
'notifyUrl' => NotifyList::getNotifyUrl('payment', $token),
'splitInfoList' => $params['splitInfoList'],
'paidType' => $params['paidType'],
'validDate' => date('Ymd'),
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['simId'] = $this->getSimID($params['paidType']);
// 1.微信支付(公众号、小程序)需填公众号授权获取微信用户openid
// 2.支付宝支付(生活号、小程序)需填生活号授权获取支付宝用户userid
// 其余时间不传
$data['chanalId'] = $params['chanalId'] ?? '';
$data['dataContent'] = json_encode($dataContent);
// 1.微信支付(公众号、小程序)需填公众号授权获取微信的appid
// 2.支付宝支付(生活号、小程序)需填生活号授权获取支付宝的appid
// 支付宝主扫 不需要
$data['appId'] = $params['appId'] ?: '';
$data['isRaw'] = 0;
$data['callType'] = $params['callType'];
$res = base64_decode($this->api('payment', $data));
return json_decode($res, true);
}
public function recharge($params, $token)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['amount'] = $params['amount'];
$data['outOrderNo'] = $params['outOrderNo'];
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['notifyUrl'] = NotifyList::getNotifyUrl('recharge', $token);
return $this->withReturnUrl($this->api('recharge', $data), $token);
}
public function merchantTransferRecharge($params, $token)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['amount'] = $params['amount'];
$data['outOrderNo'] = $params['outOrderNo'];
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['notifyUrl'] = NotifyList::getNotifyUrl('recharge', $token);
return $this->api('merchant-transfer-recharge', $data);
}
public function confirmAssurePay($params)
{
$dataContent = [
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'assureConfirmSplitInfoList' => $params['assureConfirmSplitInfoList'],
'oldOutOrderNo' => $params['oldOutOrderNo'],
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['dataContent'] = json_encode($dataContent);
$data['notifyUrl'] = $params['notifyUrl'];
return $this->api('confirm-assure-pay', $data);
}
public function confirmAssurePortfolioPay($params, $token)
{
$dataContent = [
'amount' => $params['amount'],
'outOrderNo' => $params['outOrderNo'],
'assureConfirmSplitInfoList' => $params['assureConfirmSplitInfoList'],
'oldOutOrderNo' => $params['oldOutOrderNo'],
];
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['dataContent'] = json_encode($dataContent);
$data['notifyUrl'] = NotifyList::getNotifyUrl('confirm-assure-portfolio-pay', $token);
return $this->api('confirm-assure-portfoli-pay', $data);
}
public function findBindBankCards($params)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['contractNo'] = $params['contractNo'];
$data['tradeType'] = 'BASE_LIST';
return $this->api('find-bind-bank-cards', $data);
}
public function queryCustomerInfo($loginNo, $customerType = 'PERSON')
{
$requestNo = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_QUERY_CUSTOMER_INFO);
$data = [];
$data['loginNo'] = $loginNo;
$data['customerType'] = $customerType;
$data['requestNo'] = $requestNo;
$result = $this->api('query-customer-info', $data);
return json_decode($result, true);
}
public function pwdModify($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('pwd-modify', $data, $token);
}
public function enterprisePwdModify($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('enterprise-pwd-modify', $data, $token);
}
public function pwdForget($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('pwd-forget', $data, $token);
}
public function pwReset($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('pw-reset', $data, $token);
}
public function refund($params, $token)
{
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['refundTradeId'] = $params['refundTradeId'];
$data['orgTradeId'] = $params['orgTradeId'];
$data['refundMoney'] = $params['refundMoney'];
$data['refundReason'] = $params['refundReason'];
if ($params['refundSplitInfoList']) {
$data['refundSplitInfoList'] = json_encode($params['refundSplitInfoList']);
} else {
$data['oldSubOutOrderNo'] = $params['oldSubOutOrderNo'];
}
$data['notifyUrl'] = NotifyList::getNotifyUrl('refund', $token);
return $this->api('refund-request', $data);
}
public function modifyLoginNo($origLoginNo, $loginNo)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['origLoginNo'] = $origLoginNo;
return $this->api('modify-login-no', $data);
}
public function enterpriseUnbindCard($loginNo, $agreementNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['agreementNo'] = $agreementNo;
$data['notifyUrl'] = NotifyList::getNotifyUrl('enterprise-unbind-card', $token);
return $this->api('enterprise-unbind-card', $data);
}
public function sendBindCardEmail($loginNo)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->api('send-bind-card-email',$data);
}
public function applyBindCard($loginNo, $info, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['cardNo'] = BFRSA::encryptByCERFile($info['cardNo'], config('baofu.cerFilePath'));
$data['cardName'] = $info['cardName'];
$data['bankName'] = $info['bankName'];
$data['branchName'] = $info['branchName'];
$data['provinceName'] = $info['provinceName'];
$data['cityName'] = $info['cityName'];
$data['notifyUrl'] = NotifyList::getNotifyUrl('apply-bind-card', $token);
return $this->api('apply-bind-card', $data);
}
public function openAccount($loginNo, $info)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['customerName'] = BFRSA::encryptByCERFile($info['customerName'], config('baofu.cerFilePath'));
$data['certificateNo'] = BFRSA::encryptByCERFile($info['certificateNo'], config('baofu.cerFilePath'));
$data['base64imageFront'] = $info['imageFront'];
$data['base64imageBack'] = $info['imageBack'];
return $this->api('open-account', $data);
}
public function bindCard($loginNo, $info, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['cardNo'] = BFRSA::encryptByCERFile($info['cardNo'], config('baofu.cerFilePath'));
$data['bankMobile'] = BFRSA::encryptByCERFile($info['bankMobile'], config('baofu.cerFilePath'));
$data['notifyUrl'] = NotifyList::getNotifyUrl('card-bind', $token);
return $this->api('bind-card',$data);
}
public function fixedBinding($loginNo, $info, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
$data['requestNo'] = $info['requestNo'];
$data['verifyCode'] = $info['verifyCode'];
$result = $this->api('bind-card-check', $data);
return $this->withReturnUrl($result['url'], $token);
}
public function profitShareRefundApply($params){
$data = [];
$data['loginNo'] = $params['loginNo'] ?: '';// 匿名支付不需要传
$data['refundTradeId'] = $params['refundTradeId'];
$data['refundTradeType'] = 'REFUND_APPLY';
$data['orgTradeId'] = $params['orgTradeId'];
$data['refundAmount'] = $params['refundAmount']; // 单位:分
$data['refundReason'] = $params['refundReason'];
$data['refundSplitInfoList'] = $params['refundSplitInfoList'];
return $this->api('profit-share-refund-apply', $data);
}
public function profitShareRefundConfirm($params, $token){
$data = [];
$data['loginNo'] = $params['loginNo'] ?: '';// 匿名支付不需要传
$data['refundTradeId'] = $params['refundTradeId']; // 与退款申请中的退款请求订单号一致
$data['refundTradeType'] = 'REFUND_CONFIRM';
$data['orgTradeId'] = $params['orgTradeId']; // 原始支付商户订单号
$data['refundAmount'] = $params['refundAmount']; // 单位:分
$data['notifyUrl'] = NotifyList::getNotifyUrl('confirm-split', $token);
return $this->api('profit-share-refund-confirm', $data);
}
public function profitShareRefundCancel($params){
$data = [];
$data['loginNo'] = $params['loginNo'] ?: '';// 匿名支付不需要传
$data['refundTradeId'] = $params['refundTradeId']; // 与退款申请中的退款请求订单号一致
$data['refundTradeType'] = 'REFUND_CANCEL';
return $this->api('profit-share-refund-cancel', $data);
}
public function merchantReportCreate($params){
$data = [];
$data['uniqueId'] = $params['uniqueId'];
$data['loginNo'] = $params['loginNo'];
$data['reportType'] = $params['reportType'];
$data['bizContent'] = json_encode([
'servicePhone' => $params['servicePhone'],
'businessLicense' => $params['businessLicense'],
'businessLicenseType' => $params['businessLicenseType'],
'merchantName' => $params['merchantName'],
'merchantShortName' => $params['merchantShortName'],
'channelId' => $this->getChannelId(),
'channelName' => $this->getChannelName(),
'business' => $params['business'],
'contactWechatIdType' => $params['contactWechatIdType'],
'contactWechatId' => $params['contactWechatId'],
'serviceCodes' => $params['serviceCodes'],
'addressInfo' => $params['addressInfo'],
'bankCardInfo' => $params['bankCardInfo']
]);
return $this->api('merchant-report-create', $data);
}
public function authBind($params){
$data = [];
$data['simId'] = $params['simId'];
$data['authType'] = $params['authType'];
$data['authContent'] = $params['authContent'];
return $this->api('auth-bind', $data);
}
public function queryOrder($params){
$data = [];
$data['loginNo'] = $params['loginNo'];
$data['tradeId'] = $params['tradeId'];
return $this->api('query-order', $data);
}
public function transferWithdraw($params) {
$items = [];
foreach ($params['transContent'] as $transItem) {
$item = [];
$item['transNo'] = $transItem['transNo'];
$item['transMoney'] = $transItem['transMoney'];
$item['transType'] = $transItem['transType'];
$item['transAccNo'] = $transItem['transAccNo'];
$item['transAccName'] = $transItem['transAccName'];
$item['transSummary'] = $transItem['transSummary'] ?? '转账';
if ($transItem['transType'] == 0) {
// 付款类型为银行时 传银行信息
$item['cardBankName'] = $transItem['cardBankName'];
$item['cardProName'] = $transItem['cardProName'];
$item['cardCityName'] = $transItem['cardCityName'];
$item['cardAccDept'] = $transItem['cardAccDept'];
$item['cardCnap'] = $transItem['cardCnap'];
$item['transIdCard'] = $transItem['transIdCard'];
$item['transMobile'] = $transItem['transMobile'];
}
ksort($item);
$items[] = $item;
}
$body = ['transContent' => $items];
return $this->unionPayPost($body, 'T-1001-003-01');
}
public function transferWithdrawQuery($params) {
$items = [];
foreach ($params['transContent'] as $transItem) {
$item = [];
$item['transNo'] = $transItem['transNo'];
$item['transBatchId'] = $transItem['transBatchId'];
ksort($item);
$items[] = $item;
}
$body = ['transContent' => $items];
return $this->unionPayPost($body, 'T-1001-003-02');
}
private function unionPayPost($body, $serviceTp) {
$header = [];
$header['memberId'] = $this->getMerchantNo();
$header['terminalId'] = $this->getTerminalNo();
$header['serviceTp'] = $serviceTp;
$header['verifyType'] = 1;
$content = [
'header' => $header,
'body' => $body,
];
ksort($content);
echo json_encode($content);
echo PHP_EOL;
$contentSign = Rsa::encryptedByPrivateKey(json_encode($content), $this->getPfxFilePath(), $this->getPrivateKeyPassword());
$header['content'] = $contentSign;
echo json_encode($header);
echo PHP_EOL;
$url = 'https://public.baofu.com/union-gw/api/' . $header['serviceTp'] . '/transReq.do';
$response = HttpClient::post($url, $header);
echo $response;
echo PHP_EOL;
$result = Rsa::decryptByPublicKey($response, $this->getCerFilePath());
echo $result;
echo PHP_EOL;
$result = json_decode($result, true);
if ($result['header']['sysRespCode'] == 'S_0000' && $result['body']['transHeader']['returnCode'] == '0000') {
return $result['body']['transContent'];
} else {
$msg = $result['header']['sysRespCode'] == 'S_0000' ? $result['body']['transHeader']['returnMsg'] : $result['header']['sysRespDesc'];
throw new BusinessException($msg);
}
}
public function decryptUnionPayData($content) {
return Rsa::decryptByPublicKey($content, $this->getCerFilePath());
}
public function signWithdrawEntrust($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('sign-withdraw-entrust', $data, $token);
}
public function unbindWithdrawEntrust($loginNo, $token)
{
$data = [];
$data['loginNo'] = $loginNo;
return $this->h5('unbind-withdraw-entrust', $data, $token);
}
public function entrustWithdraw($params, $token)
{
$data = [];
$data['amount'] = $params['amount'];
$data['loginNo'] = $params['loginNo'];
$data['agreementNo'] = Rsa::encryptByCERFile($params['agreementNo'], $this->getCerFilePath());
$data['outOrderNo'] = $params['outOrderNo'];
$data['notifyUrl'] = NotifyList::getNotifyUrl('withdraw', $token);
return $this->withReturnUrl($this->api('entrust-withdraw', $data), $token);
}
public function findSignEntrustResult($loginNo, $agreementType = 'W_ENTRUST')
{
$data = [];
$data['loginNo'] = $loginNo;
$data['agreementType'] = $agreementType;
return $this->api('find-sign-entrust-result', $data);
}
}