完成基础数据

This commit is contained in:
2020-09-30 16:35:15 +08:00
parent 8db6bc92c6
commit f6c4ba3ae5
5 changed files with 195 additions and 139 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Api\Controllers;
use Illuminate\Http\Request;
use XuanChen\UnionPay\UnionPay;
use Response;
class UnionPayController extends Controller
{
@@ -43,8 +44,11 @@ class UnionPayController extends Controller
unset($inputs['sign']);
$action = new UnionPay($inputs, $sign);
$res = $action->start();
dd($res);
$action->start();
$action->updateOutData();
return $action->respond();
}
}

View File

@@ -9,7 +9,14 @@ return [
'pos_ad' => '',
//营销联盟广告,用于打印在小票上
'pos_mkt_ad' => '',
//银联渠道id
'agent_id' => '299',
//银联网点id
'outlet_id' => '2009300919918',
//用于银商与sp分润的金额(是佣金的一部分), 以分为单位
'serv_chg' => 0,
//佣金
'commission' => 0,
'check' => [
'self' => [
'private' => storage_path('cert/unionpay/self/private_rsa.pem'),
@@ -28,6 +35,9 @@ return [
"msg_sender",
"msg_time",
"msg_ver",
"msg_sys_sn",//自己添加的基础数据
"req_serial_no",//自己添加的基础数据
"mkt_code",//自己添加的基础数据
],
'002100' => [
"msg_type",
@@ -38,6 +48,7 @@ return [
"msg_time",
"msg_sys_sn",
"msg_ver",
"req_serial_no",//自己添加的基础数据
],
'002101' => [
"msg_type",
@@ -48,6 +59,7 @@ return [
"msg_time",
"msg_sys_sn",
"msg_ver",
"req_serial_no",//自己添加的基础数据
],
'002102' => [
"msg_type",
@@ -58,6 +70,8 @@ return [
"msg_time",
"msg_sys_sn",
"msg_ver",
"req_serial_no",//自己添加的基础数据
],
],
'fields' => [

View File

@@ -11,16 +11,31 @@ class Init
public $sign;
public $msg_type = '00';
public $msg_txn_code;
public $msg_sender;
public $msg_rsp_code = '0000';
public $msg_rsp_desc = '成功';
//入库的模型
public $model;
//返回的数据
public $outdata;
//网点id
public $outlet_id;
/**
* RSA验签
* @param $params 待签名数据
* @param $public_key 公钥字符串
* @param $sign 要校对的的签名结果
* return 验证结果
* Notes: 验签
* @Author: 玄尘
* @Date : 2020/9/30 8:39
* @param false $self 是否是自己的证书
* @return bool|string
*/
public function checkSign($self = false)
{
@@ -49,9 +64,9 @@ class Init
* @Date : 2020/9/29 9:56
* @return string
*/
public function getSign()
public function getSign($out = true)
{
$signStr = $this->getSignString();
$signStr = $this->getSignString($out);
$private_key = $this->getPrivate();
$privKeyId = openssl_pkey_get_private($private_key);
@@ -66,13 +81,20 @@ class Init
}
/**
* 获取待签名字符串
* @param array $params 参数数组
* Notes: 获取待签名字符串
* @Author: 玄尘
* @Date : 2020/9/30 9:38
* @param $out 是否是获取返回值的数据
* @return string
*/
public function getSignString()
public function getSignString($out)
{
if ($out) {
$params = array_filter($this->outdata);
} else {
$params = array_filter($this->params);
}
ksort($params);
$signStr = http_build_query($params);
@@ -103,4 +125,23 @@ class Init
return file_get_contents($public);
}
//更新返回值
public function updateOutData()
{
$this->outdata['sign'] = $this->getSign();
$this->model->out_source = $this->outdata;
$this->model->save();
}
//输出数据
public function respond()
{
$rt = microtime(true) - LARAVEL_START;
$header = ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1)];
return \Response::json($this->outdata, 200, $header);
}
}

View File

@@ -2,8 +2,10 @@
namespace XuanChen\UnionPay;
use App\Models\UnionpayLog;
use App\Models\User;
use XuanChen\UnionPay\Action\Init;
use XuanChen\Coupon\Coupon;
/**
* 银联入口
@@ -17,43 +19,55 @@ class UnionPay extends Init
$this->sign = $sign;
$this->msg_txn_code = $params['msg_txn_code'];
$this->msg_sender = config('unionpay.msg_sender');
$this->outlet_id = config('unionpay.outlet_id');
}
public function start()
{
$out = $this->out_data();
dump($out);
$this->updateInputData();
$this->out_data();
}
//处理流程
public function out_data()
{
$basics = [
"msg_type" => "00",
"msg_txn_code" => $this->msg_txn_code,
"msg_crrltn_id" => $this->params['msg_crrltn_id'],
"msg_flg" => 1,
"msg_sender" => $this->msg_sender,
"msg_time" => now()->format('YmdHis'),
"msg_sys_sn" => $this->params['msg_sys_sn']??,
"msg_rsp_code" => "0000",
"msg_rsp_desc" => "成功",
];
$this->getOutBaseData();
if ($this->msg_rsp_code == '0000') {
switch ($this->msg_txn_code) {
//聚合营销优惠查询接口
case '002025':
$basics = array_merge($basics, [
"discount" => 0,
"actual_amt" => 0,
"pos_display" => "POS显示",
// "pos_receipt" => config('unionpay.pos_receipt'),
// "pos_ad" => config('unionpay.pos_ad'),
"pos_mkt_ad" => config('unionpay.pos_receipt'),
]);
$res = Coupon::Query($this->params['mkt_code'], $this->outlet_id);
if (is_array($res)) {
$this->outdata['pos_display'] = $res['name'];
$this->outdata['discount'] = $res['price'];
$this->outdata['actual_amt'] = bcsub($this->params['amount'], $res['price'], 4);
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $res;
}
break;
//销账交易接口
case '002100':
$data = array_merge($basics, [
'msg_ver' => 0.1,
]);
$info = UnionpayLog::where('msg_crrltn_id', $this->params['msg_crrltn_id'])
->where('msg_txn_code', '002025')
->latest()
->first();
$conf_unionpay = config('unionpay');
$user = User::find($conf_unionpay['agent_id']);
if ($info) {
$coupon = Coupon::Redemption($user, $info->mkt_code, $this->params['orig_amt'], $conf_unionpay['outlet_id'], '');
if (!is_array($coupon)) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $coupon;
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。';
}
break;
case '002101':
break;
@@ -63,122 +77,105 @@ class UnionPay extends Init
break;
}
return $basics;
}
/**
* Notes: 查询接口
* @Author: 玄尘
* @Date : 2020/7/21 11:58
* @param $redemptionCode
*/
public static function Query(Request $request)
{
$inputs = $request->all();
$sign = $inputs['sign'];
unset($inputs['sign']);
if (!$redemptionCode) {
return '查询失败,未获取到券码';
}
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
}
return $model->setCode($redemptionCode)
->setOutletId($outletId)
->detail();
}
/**
* Notes: 卡券作废
* @Author: 玄尘
* @Date : 2020/9/2 16:54
* @param $redemptionCode
* @param $outletId
* @return string
*/
public static function Destroy($redemptionCode, $outletId)
{
try {
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
}
return $model->setCode($redemptionCode)
->setOutletId($outletId)
->destroy();
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* Notes: 根据券码 获取class
* Notes: 返回的基础数据
* @Author: 玄尘
* @Date : 2020/7/21 12:00
* @param $code
* @return string
* @Date : 2020/9/30 14:48
*/
public static function getModelByCode($code)
public function getOutBaseData()
{
$rules = config('xuanchen_coupon.rules');
if (!$rules) {
return '系统出错,未找到配置文件';
}
$basics = [
"msg_type" => $this->msg_type,
"msg_txn_code" => $this->msg_txn_code,
"msg_crrltn_id" => $this->params['msg_crrltn_id'],
"msg_flg" => 1,
"msg_sender" => $this->msg_sender,
"msg_time" => now()->format('YmdHis'),
"msg_sys_sn" => $this->params['msg_sys_sn'],
"msg_rsp_code" => $this->msg_rsp_code,
"msg_rsp_desc" => $this->msg_rsp_desc,
];
$model = '';
foreach ($rules as $rule) {
if (preg_match($rule['pattern'], $code, $matches)) {
$model = $rule['model'];
switch ($this->msg_txn_code) {
case '002025':
$basics = array_merge($basics, [
"discount" => 0,
"actual_amt" => 0,
"pos_display" => "",
// "pos_receipt" => config('unionpay.pos_receipt'),
// "pos_ad" => config('unionpay.pos_ad'),
"pos_mkt_ad" => config('unionpay.pos_receipt'),
]);
break;
case '002100':
$basics = array_merge($basics, [
'msg_ver' => 0.1,
'orig_amt' => $this->params['orig_amt'],
'discount_amt' => $this->params['discount_amt'],
'pay_amt' => $this->params['pay_amt'],
'serv_chg' => config('unionpay.serv_chg'),
'commission' => $this->params['orig_amt'],
'event_no' => '',//什么是活动号
]);
break;
case '002101':
break;
case '002102':
break;
default:
break;
}
$this->outdata = $basics;
}
if (!$model) {
throw new \Exception('卡券核销失败。未查到卡券所属');
/**
* Notes: 入库数据
* @Author: 玄尘
* @Date : 2020/9/30 8:46
*/
private function updateInputData()
{
try {
if (!isset($this->params['msg_sys_sn'])) {
$this->params['msg_sys_sn'] = date('ymdHis') . sprintf('%06d', rand(1, 99999999));
}
return new $model;
$base = config('unionpay.regular')[$this->msg_txn_code];
$data = [];
foreach ($this->params as $key => $param) {
if (in_array($key, $base)) {
$data[$key] = $param;
} else {
$data['in_source'][$key] = $param;
}
}
$this->model = UnionpayLog::create($data);
return true;
} catch (\Exception $e) {
$this->msg_rsp_code = 9999;
$this->msg_rsp_desc = $e->getMessage();
return false;
}
}
/**
* Notes: description
* Notes: 校验输入的数据
* @Author: 玄尘
* @Date : 2020/8/21 13:33
* @param \App\Models\User $user 渠道
* @param string $redemptionCode 要核销的券码
* @param float $total 订单金额
* @param string $outletId 网点id
* @param string $orderid 订单id
* @return string
* @Date : 2020/9/30 14:46
*/
public static function Redemption(User $user, string $redemptionCode, float $total, string $outletId, string $orderid = '')
public function checkInData()
{
try {
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
if ($this->params['msg_txn_code'] == '002025' && (!isset($this->params['mkt_code']) || empty($this->params['mkt_code']))) {
$this->msg_rsp_code = 9999;
$this->msg_rsp_desc = '聚合营销码不能为空';
}
return $model->setUser($user)
->setCode($redemptionCode)
->setTotal($total)
->setOutletId($outletId)
->setOrderId($orderid)
->start();
} catch (\Exception $e) {
return $e->getMessage();
}
}
}