208 lines
6.3 KiB
PHP
208 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace Yunshop\HftxPay\services;
|
||
|
||
use app\common\exceptions\AppException;
|
||
use app\common\helpers\Url;
|
||
use app\common\models\Member;
|
||
use app\common\models\PayOrder;
|
||
use app\common\services\Pay;
|
||
use app\common\services\Session;
|
||
use BsPaySdk\core\BsPay;
|
||
use BsPaySdk\core\BsPayClient;
|
||
use BsPaySdk\request\V2TradePaymentJspayRequest;
|
||
use BsPaySdk\request\V2TradePaymentScanpayRefundRequest;
|
||
|
||
# SDK 初始化文件加载
|
||
require_once dirname(__FILE__) . "/../BsPaySdk/init.php";
|
||
require_once dirname(__FILE__) . "/../BsPaySdk/request/V2TradePaymentJspayRequest.php";
|
||
require_once dirname(__FILE__) . "/../BsPaySdk/request/V2TradePaymentScanpayRefundRequest.php";
|
||
|
||
class HfPay extends Pay
|
||
{
|
||
protected $notify_url;
|
||
protected $paySet;
|
||
protected $TradeType;
|
||
|
||
public $codeId = '';
|
||
public $payTpye = [
|
||
200 => 'T_JSAPI',
|
||
201 => 'A_NATIVE',
|
||
202 => 'T_MINIAPP',
|
||
203 => 'A_JSAPI',
|
||
204 => 'U_NATIVE',
|
||
205 => 'U_JSAPI',
|
||
206 => 'D_NATIVE',
|
||
207 => 'T_H5',
|
||
208 => 'T_APP',
|
||
209 => 'T_NATIVE',
|
||
127 => '',
|
||
'T_JSAPI' => '微信公众号',
|
||
'T_MINIAPP' => '微信小程序',
|
||
'A_JSAPI' => '支付宝JS',
|
||
'A_NATIVE' => '支付宝正扫',
|
||
'U_NATIVE' => '银联正扫',
|
||
'U_JSAPI' => '银联JS',
|
||
'D_NATIVE' => '数字人民币正扫',
|
||
'T_H5' => '微信直连H5支付',
|
||
'T_APP' => '微信APP支付(只支持直连)',
|
||
'T_NATIVE' => '微信正扫(只支持直连)'
|
||
];
|
||
|
||
/**
|
||
* WechatH5Pay constructor.
|
||
* @throws AppException
|
||
*/
|
||
public function __construct($type = '')
|
||
{
|
||
if (is_numeric($type)) {
|
||
$this->codeId = $type;
|
||
$this->TradeType = $this->payTpye[$type];
|
||
} else {
|
||
$this->TradeType = $type;
|
||
}
|
||
$this->paySet = \Setting::get('shop.pay');
|
||
$config = [
|
||
'sys_id' => $this->paySet['sys_id'],
|
||
'product_id' => $this->paySet['product_id'],
|
||
'rsa_merch_private_key' => $this->paySet['hf_rsa_private_key'],
|
||
'rsa_huifu_public_key' => $this->paySet['hf_rsa_public_key'],
|
||
];
|
||
BsPay::init($config, true);
|
||
}
|
||
|
||
/***
|
||
* 发起支付
|
||
* @return void
|
||
*/
|
||
public function doPay($data)
|
||
{
|
||
|
||
$op = "{$this->payTpye[$this->TradeType]}订单支付 订单号:" . $data['order_no'];
|
||
$this->log($data['extra']['type'], $this->payTpye[$this->TradeType], $data['amount'], $op, $data['order_no'], Pay::ORDER_STATUS_NON, \YunShop::app()->getMemberId());
|
||
|
||
|
||
$this->notify_url = Url::shopSchemeUrl('payment/hfpay/notifyUrl.php');
|
||
// 2.组装请求参数
|
||
$request = new V2TradePaymentJspayRequest();
|
||
// 请求日期
|
||
$request->setReqDate(date("Ymd"));
|
||
// 请求流水号
|
||
$request->setReqSeqId($data['order_no']);
|
||
// 商户号
|
||
$request->setHuifuId($this->paySet['sys_id']);
|
||
// 交易类型
|
||
$request->setTradeType($this->TradeType);
|
||
// 交易金额
|
||
$request->setTransAmt($data['amount']);
|
||
// 商品描述
|
||
$request->setGoodsDesc($data['body']);
|
||
// // 设置非必填字段
|
||
$extendInfoMap = $this->getExtendInfos();
|
||
$request->setExtendInfo($extendInfoMap);
|
||
// // 3. 发起API调用
|
||
$client = new BsPayClient();
|
||
$result = $client->postRequest($request);
|
||
if (!$result || $result->isError()) { //失败处理
|
||
$data['msg'] = $result->getErrorInfo();
|
||
} else { //成功处理
|
||
$data = $result->getRspDatas()['data'];
|
||
if ($data['resp_code'] != '00000100') {
|
||
$data['msg'] = $data['resp_desc'];
|
||
} else {
|
||
$data['mode'] = $this->codeId;
|
||
}
|
||
return $data;
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 退款
|
||
* @param $out_trade_no
|
||
* @param $totalmoney
|
||
* @param $refundmoney
|
||
* @return void
|
||
*/
|
||
public function doRefund($out_trade_no, $OrgReqDate, $refundmoney, $org_req_seq_id = '')
|
||
{
|
||
$this->notify_url = Url::shopSchemeUrl('payment/hfpay/returnUrl.php');
|
||
// 2.组装请求参数
|
||
$request = new V2TradePaymentScanpayRefundRequest();
|
||
// 请求日期
|
||
$request->setReqDate(date("Ymd"));
|
||
// 请求流水号
|
||
$request->setReqSeqId($out_trade_no);
|
||
// 商户号
|
||
$request->setHuifuId($this->paySet['sys_id']);
|
||
|
||
$request->setOrgReqDate(date('Ymd', $OrgReqDate)); //原交易日
|
||
// 交易金额
|
||
$request->setOrdAmt($refundmoney);
|
||
// 设置非必填字段
|
||
$extendInfoMap = $this->getExtendInfos();
|
||
$extendInfoMap['org_req_seq_id'] = PayOrder::getPayOrderInfo($out_trade_no)->value('trade_no');
|
||
$request->setExtendInfo($extendInfoMap);
|
||
// 3. 发起API调用
|
||
$client = new BsPayClient();
|
||
$result = $client->postRequest($request);
|
||
if (!$result || $result->isError()) { //失败处理
|
||
$data['msg'] = $result->getErrorInfo();
|
||
} else { //成功处理
|
||
$data = $result->getRspDatas()['data'];
|
||
return $data;
|
||
}
|
||
}
|
||
|
||
|
||
/***
|
||
* 获取扩展参数
|
||
* @return array
|
||
*/
|
||
public function getExtendInfos()
|
||
{
|
||
$data['notify_url'] = $this->notify_url;
|
||
$data['remark'] = \YunShop::app()->uniacid;
|
||
switch ($this->TradeType) {
|
||
case 'T_JSAPI':
|
||
case 'T_MINIAPP':
|
||
case 'T_H5':
|
||
$openid = Member::getOpenIdForType(\YunShop::app()->getMemberId());
|
||
$data['wx_data'] = [
|
||
'openid' => $openid?: Session::get('wx_open_id')
|
||
];
|
||
break;
|
||
case 'A_JSAPI':
|
||
$data['alipay_data'] = [
|
||
'buyer_id' => ''
|
||
];
|
||
break;
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 提现
|
||
*
|
||
* @param $member_id 提现者用户ID
|
||
* @param $out_trade_no 提现批次单号
|
||
* @param $money 提现金额
|
||
* @param $desc 提现说明
|
||
* @param $type 只针对微信 1-企业支付(钱包) 2-红包
|
||
* @return mixed
|
||
*/
|
||
public function doWithdraw($member_id, $out_trade_no, $money, $desc, $type)
|
||
{
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 构造签名
|
||
* @return mixed
|
||
*/
|
||
public function buildRequestSign()
|
||
{
|
||
|
||
}
|
||
}
|