完成基础数据

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 Illuminate\Http\Request;
use XuanChen\UnionPay\UnionPay; use XuanChen\UnionPay\UnionPay;
use Response;
class UnionPayController extends Controller class UnionPayController extends Controller
{ {
@@ -43,8 +44,11 @@ class UnionPayController extends Controller
unset($inputs['sign']); unset($inputs['sign']);
$action = new UnionPay($inputs, $sign); $action = new UnionPay($inputs, $sign);
$res = $action->start(); $action->start();
dd($res); $action->updateOutData();
return $action->respond();
} }
} }

View File

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

View File

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

@@ -9,8 +9,8 @@ use XuanChen\UnionPay\Contracts\CouponContracts;
/** /**
* Class 银联对接优惠券 * Class 银联对接优惠券
* @Author: 玄尘 * @Author : 玄尘
* @Date: 2020/9/28 14:02 * @Date : 2020/9/28 14:02
* @package XuanChen\Coupon\Action * @package XuanChen\Coupon\Action
*/ */
class UnionPayAction extends Init implements CouponContracts class UnionPayAction extends Init implements CouponContracts

View File

@@ -2,8 +2,10 @@
namespace XuanChen\UnionPay; namespace XuanChen\UnionPay;
use App\Models\UnionpayLog;
use App\Models\User; use App\Models\User;
use XuanChen\UnionPay\Action\Init; use XuanChen\UnionPay\Action\Init;
use XuanChen\Coupon\Coupon;
/** /**
* 银联入口 * 银联入口
@@ -17,43 +19,55 @@ class UnionPay extends Init
$this->sign = $sign; $this->sign = $sign;
$this->msg_txn_code = $params['msg_txn_code']; $this->msg_txn_code = $params['msg_txn_code'];
$this->msg_sender = config('unionpay.msg_sender'); $this->msg_sender = config('unionpay.msg_sender');
$this->outlet_id = config('unionpay.outlet_id');
} }
public function start() public function start()
{ {
$out = $this->out_data(); $this->updateInputData();
dump($out); $this->out_data();
} }
//处理流程
public function out_data() public function out_data()
{ {
$basics = [ $this->getOutBaseData();
"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" => "成功",
];
if ($this->msg_rsp_code == '0000') {
switch ($this->msg_txn_code) { switch ($this->msg_txn_code) {
//聚合营销优惠查询接口
case '002025': case '002025':
$basics = array_merge($basics, [ $res = Coupon::Query($this->params['mkt_code'], $this->outlet_id);
"discount" => 0, if (is_array($res)) {
"actual_amt" => 0, $this->outdata['pos_display'] = $res['name'];
"pos_display" => "POS显示", $this->outdata['discount'] = $res['price'];
// "pos_receipt" => config('unionpay.pos_receipt'), $this->outdata['actual_amt'] = bcsub($this->params['amount'], $res['price'], 4);
// "pos_ad" => config('unionpay.pos_ad'), } else {
"pos_mkt_ad" => config('unionpay.pos_receipt'), $this->outdata['msg_rsp_code'] = '9999';
]); $this->outdata['msg_rsp_desc'] = $res;
}
break; break;
//销账交易接口
case '002100': case '002100':
$data = array_merge($basics, [ $info = UnionpayLog::where('msg_crrltn_id', $this->params['msg_crrltn_id'])
'msg_ver' => 0.1, ->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; break;
case '002101': case '002101':
break; break;
@@ -63,122 +77,105 @@ class UnionPay extends Init
break; 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: 玄尘 * @Author: 玄尘
* @Date : 2020/7/21 12:00 * @Date : 2020/9/30 14:48
* @param $code
* @return string
*/ */
public static function getModelByCode($code) public function getOutBaseData()
{ {
$rules = config('xuanchen_coupon.rules'); $basics = [
if (!$rules) { "msg_type" => $this->msg_type,
return '系统出错,未找到配置文件'; "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 = ''; switch ($this->msg_txn_code) {
foreach ($rules as $rule) { case '002025':
if (preg_match($rule['pattern'], $code, $matches)) { $basics = array_merge($basics, [
$model = $rule['model']; "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; 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: 玄尘 * @Author: 玄尘
* @Date : 2020/8/21 13:33 * @Date : 2020/9/30 14:46
* @param \App\Models\User $user 渠道
* @param string $redemptionCode 要核销的券码
* @param float $total 订单金额
* @param string $outletId 网点id
* @param string $orderid 订单id
* @return string
*/ */
public static function Redemption(User $user, string $redemptionCode, float $total, string $outletId, string $orderid = '') public function checkInData()
{ {
if ($this->params['msg_txn_code'] == '002025' && (!isset($this->params['mkt_code']) || empty($this->params['mkt_code']))) {
try { $this->msg_rsp_code = 9999;
$model = self::getModelByCode($redemptionCode); $this->msg_rsp_desc = '聚合营销码不能为空';
if (is_string($model)) {
return $model;
} }
return $model->setUser($user)
->setCode($redemptionCode)
->setTotal($total)
->setOutletId($outletId)
->setOrderId($orderid)
->start();
} catch (\Exception $e) {
return $e->getMessage();
}
} }
} }