371 行
14 KiB
PHP
可执行文件
371 行
14 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace CashierApi\Business;
|
||
|
||
|
||
use Common\Business\Merchant;
|
||
use Common\Business\VipRechargeActivity;
|
||
use Common\Model\RetailBillDiscountModel;
|
||
use Common\Model\RetailBillOrderModel;
|
||
use PayApi\Controller\RechargeController;
|
||
|
||
//[1.9.20]
|
||
|
||
/**
|
||
* 支付回调类
|
||
* Class PaymentCallBack
|
||
* [
|
||
* 购买次卡卡包支付成功回调 cardCallBackHandle
|
||
* 会员充值余额支付成功回调处理 rechargeCallBackHandle rechargeVerify
|
||
* 会员充值余额支付成功回调处理的参数验证 rechargeVerify
|
||
* 购买商品支付回调处理 callBackHandle
|
||
* 购买商品更新订单及库存 updateBillOrder
|
||
* 购买商品支付回调处理参数验证
|
||
* 更新流水表 updatePayOrder
|
||
* ]
|
||
* @package CashierApi\Business
|
||
*/
|
||
class PaymentCallBack
|
||
{
|
||
/**
|
||
* 购买次卡卡包支付成功回调
|
||
* @param $param
|
||
* [
|
||
* upData 流水更新的信息
|
||
* pay_order 流水信息
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function cardCallBackHandle($param)
|
||
{
|
||
$verifyResult = $this->cardVerify($param); //参数验证
|
||
if ($verifyResult['status'] != 1) {
|
||
return $verifyResult;
|
||
}
|
||
//更新流水表
|
||
if (!empty($param['upData'])) {
|
||
$payOrderResult = $this->updatePayOrder($param['pay_order']['id'], $param['upData']);
|
||
if ($payOrderResult['status'] != 1) {
|
||
return $payOrderResult;
|
||
}
|
||
}
|
||
$res = A('Api/SubCard')->updataCardOrder($param['pay_order']);
|
||
if ($res['status'] == 1) {
|
||
return callBack(statusCode()['success'], "发放成功");
|
||
} else {
|
||
return callBack(statusCode()['error'], $res['msg']);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 购买次卡卡包支付成功回调的参数验证
|
||
* @param $param
|
||
* [
|
||
* upData 流水更新的信息
|
||
* pay_order 流水信息
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function cardVerify($param)
|
||
{
|
||
if (empty($param['upData']) || empty($param['pay_order'])) {
|
||
return callBack(statusCode()['error'], "缺少流水信息");
|
||
}
|
||
return callBack(statusCode()['success'], "验证成功");
|
||
}
|
||
|
||
|
||
/**
|
||
* 会员充值余额支付成功回调处理
|
||
* @param $param
|
||
* [
|
||
* merchant_id
|
||
* store_id
|
||
* pay_order 流水信息
|
||
* upData 修改流水的信息
|
||
* recharge_id 充值活动id
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function rechargeCallBackHandle($param)
|
||
{
|
||
$verifyResult = $this->rechargeVerify($param); //参数验证
|
||
if ($verifyResult['status'] != 1) {
|
||
return $verifyResult;
|
||
}
|
||
$upData = $param['upData']; //流水修改的信息
|
||
$pay_order = $param['pay_order']; //流水信息
|
||
if (!empty($param['upData'])) {
|
||
$payOrderResult = $this->updatePayOrder($pay_order['id'], $upData);
|
||
if ($payOrderResult['status'] != 1) {
|
||
return $payOrderResult;
|
||
}
|
||
}
|
||
$MerchantClass = new Merchant();
|
||
$merchantResult = $MerchantClass->getSetting([
|
||
'merchant_id' => $param['merchant_id']
|
||
]);
|
||
if ($merchantResult['status'] != 1) {
|
||
return $merchantResult;
|
||
}
|
||
$setting = $merchantResult['data']; //商户分期设置
|
||
$install_account = []; //返回前端的分期信息
|
||
if (!empty($param['recharge_id'])) {
|
||
$ActivityClass = new VipRechargeActivity();
|
||
$activityResult = $ActivityClass->basisIdGetInfo([
|
||
'merchant_id' => $param['merchant_id'],
|
||
'store_id' => $param['store_id'],
|
||
'id' => $param['recharge_id'],
|
||
'activity_type' => 2
|
||
]); //依据活动id查询可使用的活动信息
|
||
if ($activityResult['status'] != 1) {
|
||
return $activityResult;
|
||
}
|
||
$activity = $activityResult['data']; //活动信息
|
||
if ($activity['aging_repay'] == 1) {
|
||
//分期到账
|
||
$cycle = $setting['aging_cycle'] == 0 ? '天' : '月';
|
||
$aging_repay_name = '分期到账';
|
||
$install_account = [
|
||
'aging_repay' => $setting['aging_repay'],
|
||
'aging_cycle' => $cycle,
|
||
'aging_time' => $setting['aging_time'],
|
||
'active_aging_repay' => $aging_repay_name,
|
||
'active_aging_time' => $activity['aging_time'],
|
||
];
|
||
}
|
||
}
|
||
if (!empty($upData['pay_coupon_id_str']) && !empty($upData['pay_coupon_fee'])) {
|
||
//微信支付使用了代金券
|
||
$pay_order['pay_coupon_id_str'] = $upData['pay_coupon_id_str'];
|
||
$pay_order['pay_coupon_fee'] = $upData['pay_coupon_fee'];
|
||
}
|
||
$RechargeController = new RechargeController();
|
||
$result = $RechargeController->updateOrder($pay_order); // 更新余额记录信息
|
||
|
||
$result['order_id'] = $pay_order['id']; // 返回订单号(打印小票)
|
||
$result['install_data'] = $install_account ? $install_account : []; // 返回分期到账设置
|
||
// 判断
|
||
if ($result['status'] == 1) {
|
||
// wifi打印
|
||
A('PayApi/Printer')->rechargeWifiPrint(['order_id' => $pay_order['id'], 'store_id' => $pay_order['store_id']]);
|
||
return callBack(statusCode()['success'], "充值成功", $result);
|
||
} else {
|
||
return callBack(statusCode()['error'], $result['msg']);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 会员充值余额支付成功回调处理的参数验证
|
||
* @param $param
|
||
* @return mixed
|
||
*/
|
||
public function rechargeVerify($param)
|
||
{
|
||
if (empty($param['merchant_id']) || empty($param['store_id'])) {
|
||
return callBack(statusCode()['error'], "缺少唯一标识");
|
||
}
|
||
if (empty($param['pay_order']) || empty($param['upData'])) {
|
||
return callBack(statusCode()['error'], "未找到流水单信息");
|
||
}
|
||
return callBack(statusCode()['success'], "验证成功");
|
||
}
|
||
|
||
|
||
/**
|
||
* 购买商品支付回调处理
|
||
* @param $param
|
||
* [
|
||
* amount_received 实收金额
|
||
* billOrder 订单信息
|
||
* is_zero_storage 是否零存 1是 0否
|
||
* pay_type 支付类型 1微信 2支付宝 3现金 5其他 6余额 7赊账
|
||
* pay_order 流水信息
|
||
* pay_money 支付金额
|
||
* upData 流水单修改的数据 (包含代金券优惠的总金额与代金券id)
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function callBackHandle($param)
|
||
{
|
||
$verifyResult = $this->callBackHandleVerify($param); //参数验证
|
||
if ($verifyResult['status'] != 1) {
|
||
return $verifyResult;
|
||
}
|
||
$pay_type = $param['pay_type']; //支付方式
|
||
$pay_money = $param['pay_money']; //支付金额
|
||
$amount_received = $param['amount_received']; //实收金额
|
||
$billOrder = $param['billOrder']; //订单信息
|
||
$is_zero_storage = $param['is_zero_storage']; //是否零存
|
||
$pay_order = $param['pay_order']; //流水信息
|
||
$upData = $param['upData'] ? $param['upData'] : []; //流水信息修改的数据
|
||
if (!empty($upData)) {
|
||
$payOrderResult = $this->updatePayOrder($pay_order['id'], $upData);
|
||
if ($payOrderResult['status'] != 1) {
|
||
return $payOrderResult;
|
||
}
|
||
if (!empty($upData['pay_coupon_id_str']) && !empty($upData['pay_coupon_fee'])) {
|
||
//使用了代金券
|
||
$DiscountModel = new RetailBillDiscountModel();
|
||
$DiscountModel->updateOrder([
|
||
'bill_id' => $billOrder['id']
|
||
], [
|
||
'pay_coupon_id_str' => $upData['pay_coupon_id_str'],
|
||
'pay_coupon_fee' => $upData['pay_coupon_fee']
|
||
]);
|
||
}
|
||
}
|
||
switch ($pay_type) {
|
||
case 1:
|
||
case 2:
|
||
//微信 支付宝
|
||
$result = $this->updateBillOrder([
|
||
'pay_money' => $pay_money,
|
||
'amount_received' => $amount_received,
|
||
'billOrder' => $billOrder,
|
||
'pay_coupon_id_str' => $upData['pay_coupon_id_str'],
|
||
'pay_coupon_fee' => $upData['pay_coupon_fee']
|
||
]);
|
||
break;
|
||
case 3:
|
||
case 5:
|
||
//现金 其他支付
|
||
$itemData = array(
|
||
'pay_money' => $pay_money,
|
||
'amount_received' => $amount_received,
|
||
'billOrder' => $billOrder,
|
||
'is_zero_storage' => $is_zero_storage,
|
||
'pay_type' => 3
|
||
);
|
||
$result = $this->updateBillOrder($itemData);
|
||
break;
|
||
case 6:
|
||
//会员余额
|
||
$itemData = [
|
||
'pay_money' => $pay_money,
|
||
'amount_received' => $amount_received,
|
||
'billOrder' => $billOrder
|
||
];
|
||
$result = $this->updateBillOrder($itemData);
|
||
break;
|
||
case 8:
|
||
//德育币
|
||
$itemData = [
|
||
'pay_money' => $pay_money,
|
||
'amount_received' => $amount_received,
|
||
'billOrder' => $billOrder
|
||
];
|
||
$result = $this->updateBillOrder($itemData);
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
|
||
/**
|
||
* 购买商品更新订单及库存
|
||
* @param $param
|
||
* [
|
||
* amount_received 实收金额
|
||
* billOrder 订单信息
|
||
* is_zero_storage 是否零存 1是 0否
|
||
* pay_type 支付类型 1微信 2支付宝 3现金 5其他 6余额 7赊账
|
||
* pay_money 支付金额
|
||
* pay_coupon_id_str 微信为代金券唯一分配的id
|
||
* pay_coupon_fee 微信代金券优惠的总金额
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function updateBillOrder($param)
|
||
{
|
||
$pay_money = $param['pay_money'];
|
||
$amount_received = $param['amount_received'];
|
||
$billOrder = $param['billOrder'];
|
||
$pay_type = $param['pay_type'];
|
||
$is_zero_storage = $param['is_zero_storage'];
|
||
$pay_coupon_id_str = $param['pay_coupon_id_str'] ? $param['pay_coupon_id_str'] : '';
|
||
$pay_coupon_fee = $param['pay_coupon_fee'] ? $param['pay_coupon_fee'] : 0;
|
||
try {
|
||
$billModel = new RetailBillOrderModel();
|
||
$upData = array(
|
||
'pay_money' => round($billOrder['pay_money'] + $pay_money, 2),
|
||
'amount_received' => round($billOrder['amount_received'] + $amount_received, 2),
|
||
);
|
||
if ($pay_type == 3 && $is_zero_storage && $amount_received > $pay_money) {
|
||
$upData['zero_storage'] = round($amount_received - $pay_money + $billOrder['zero_storage'], 2);
|
||
$billOrder['zero_storage'] = $upData['zero_storage'];
|
||
} else if ($pay_type == 3 && $amount_received > $pay_money) {
|
||
$upData['give_change'] = round($amount_received - $pay_money + $billOrder['give_change'], 2);
|
||
}
|
||
//微信代金券的id 与优惠总金额
|
||
$billOrderMoney = $billOrder['all_money'];
|
||
if (!empty($pay_coupon_id_str) && !empty($pay_coupon_fee)) {
|
||
$billOrderMoney = round($billOrder['all_money'] - $pay_coupon_fee, 2);
|
||
$upData['all_money'] = $billOrderMoney;
|
||
$upData['pay_money'] = round($upData['pay_money'] - $pay_coupon_fee, 2);
|
||
$upData['amount_received'] = round($upData['amount_received'] - $pay_coupon_fee, 2);
|
||
}
|
||
$upBill = $billModel->updateOrder(['id' => $billOrder['id']], $upData);
|
||
if ($upBill) {
|
||
if (round($upData['pay_money'] - $billOrderMoney, 2) == 0) {
|
||
$res = A('PayApi/Order')->paySuccess_v1($billOrder['id']); //订单已经支付完成开始更新商品
|
||
$res['message'] = $res['msg'];
|
||
} else {
|
||
$res['status'] = 2;
|
||
$res['message'] = '请继续支付';
|
||
}
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['message'] = '数据库发生错误,bill更新失败,请联系系统人员进行退款';
|
||
}
|
||
} catch (\Exception $e) {
|
||
$res['status'] = 0;
|
||
$res['message'] = '更新订单与库存失败';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 购买商品支付回调处理参数验证
|
||
* @param $param
|
||
* [
|
||
* amount_received 实收金额
|
||
* billOrder 订单信息
|
||
* is_zero_storage 是否零存 1是 0否
|
||
* pay_type 支付类型 1微信 2支付宝 3现金 5其他 6余额 7赊账
|
||
* pay_order 流水信息
|
||
* pay_money 支付金额
|
||
* upData 流水单修改的数据
|
||
* ]
|
||
* @return mixed
|
||
*/
|
||
public function callBackHandleVerify($param)
|
||
{
|
||
if (!in_array($param['pay_type'], ['1', '2', '3', '5', '6', '7', '8'])) {
|
||
return callBack(statusCode()['error'], "支付类型有误");
|
||
}
|
||
if (empty($param['billOrder'])) {
|
||
return callBack(statusCode()['error'], "缺少订单信息");
|
||
}
|
||
return callBack(statusCode()['success'], "验证成功");
|
||
}
|
||
|
||
|
||
/**
|
||
* 更新流水表
|
||
* @param $id 流水id
|
||
* @param $data 更新的数据
|
||
* @return mixed
|
||
*/
|
||
public function updatePayOrder($id, $data)
|
||
{
|
||
$PayOrderClass = new \Common\Business\PayOrder();
|
||
$payOrderResult = $PayOrderClass->basisIdModify($id, $data);
|
||
if ($payOrderResult['status'] == 1 && !empty($data['pay_coupon_id_str']) && !empty($data['pay_coupon_fee'])) {
|
||
$payOrderResult = $PayOrderClass->ModifyPayOrderTrade($id, $data['pay_coupon_fee']);
|
||
}
|
||
return $payOrderResult;
|
||
}
|
||
} |