This repository has been archived on 2021-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
Files
pingan_unionpay/packages/unionpay/src/UnionPay.php
2020-10-10 14:57:22 +08:00

271 lines
9.4 KiB
PHP

<?php
namespace XuanChen\UnionPay;
use App\Models\UnionpayLog;
use App\Models\User;
use XuanChen\UnionPay\Action\Init;
use XuanChen\Coupon\Coupon;
/**
* 银联入口
*/
class UnionPay extends Init
{
/**
* UnionPay constructor.
* @param $params
* @param string $sign
*/
public function __construct($params, $sign = '')
{
$this->params = $params;
$this->sign = $sign;
$this->msg_txn_code = $params['msg_txn_code'] ?? '';
$this->msg_sender = config('unionpay.msg_sender');
$this->agent_id = config('unionpay.agent_id');
$this->outlet_id = config('unionpay.outlet_id');
}
/**
* Notes: 入口
* @Author: 玄尘
* @Date : 2020/10/9 9:33
*/
public function start()
{
//设置基础数据
$this->getOutBaseData();
try {
//校验数据
$this->checkInData();
//查询是否是幂等 就是重复查询
$this->idempotent();
//入库请求参数
$this->InputData();
//返回值
$this->out_data();
//更新数据
$this->updateOutData();
} catch (\Exception $e) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $e->getMessage();
if (empty($this->model->out_source)) {
$this->updateOutData();
}
}
}
//处理流程
public function out_data()
{
//是幂等
if ($this->info && !empty($this->info->out_source)) {
$this->outdata = $this->info->out_source;
} else {
if ($this->msg_rsp_code == '0000') {
switch ($this->msg_txn_code) {
//聚合营销优惠查询接口
case '002025':
$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'] * 100;
$this->outdata['actual_amt'] = (int)bcsub($this->params['amount'], $res['price'] * 100);
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $res;
}
break;
//销账交易接口
case '002100':
//查询聚合信息
$query = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
->where('msg_txn_code', '002025')
->latest()
->first();
$this->outdata['orig_amt'] = $query->in_source['amount']; //订单金额 原始金额
$this->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额
$this->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额
//获取银联渠道
$user = User::find($this->agent_id);
if ($query) {
$coupon = [];
$coupon = Coupon::Redemption($user, $query->mkt_code, $this->params['orig_amt'] / 100, $this->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;
case '002102':
//查询聚合信息
$info = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
->where('msg_txn_code', '002025')
->latest()
->first();
if ($info) {
if ($info->coupon) {
$ret = $info->coupon->reprofit();
if ($ret !== true) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $ret;
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '为查询到优惠券信息。';
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。';
}
break;
default:
break;
}
}
}
}
/**
* Notes: 入库数据
* @Author: 玄尘
* @Date : 2020/9/30 8:46
*/
private function InputData()
{
//获取基础数据
$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);
if (empty($this->model)) {
throw new \Exception('数据入库失败');
}
}
/**
* Notes: 校验输入的数据
* @Author: 玄尘
* @Date : 2020/9/30 14:46
*/
public function checkInData()
{
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 = '聚合营销码不能为空';
}
}
/**
* Notes: 返回的基础数据
* @Author: 玄尘
* @Date : 2020/9/30 14:48
*/
public function getOutBaseData()
{
$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,
];
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' => config('unionpay.commission'),
'event_no' => '',//活动号 直接为空就可以
]);
break;
case '002101':
$basics = array_merge($basics, [
'msg_ver' => 0.1,
]);
break;
case '002102':
$basics = array_merge($basics, [
'msg_ver' => 0.1,
]);
break;
default:
break;
}
return $this->outdata = $basics;
}
/**
* Notes: 查询是否是幂等
* @Author: 玄尘
* @Date : 2020/10/10 13:25
*/
public function idempotent()
{
$this->info = UnionpayLog::where('req_serial_no', $this->params['req_serial_no'])
->where('msg_txn_code', $this->msg_txn_code)
->where('status', 1)
->latest()
->first();
}
//更新返回值
public function updateOutData()
{
$this->outdata['sign'] = $this->getSign();
$this->model->out_source = $this->outdata;
if ($this->outdata['msg_rsp_code'] == '9999') {
$this->model->status = 0;
}
$this->model->save();
}
}