Files
xk-api-yii/member/modules/v1/controllers/EplpayCallbackController.php
2024-09-30 11:12:14 +08:00

368 lines
14 KiB
PHP

<?php
namespace member\modules\v1\controllers;
use common\forms\LedgerForm;
use common\jobs\ProductOrderRefundJob;
use common\jobs\ProductOrderSyncPlatformJob;
use common\models\ChargeCashPayRecord;
use common\models\PaymentRegister;
use common\models\PaymentRegisterRefund;
use common\models\Register;
use common\models\RegisterRefund;
use member\models\forms\ProductOrderSubmitForm;
use common\models\Callback;
use common\models\Order;
use common\models\PaymentProductOrder;
use common\models\PaymentProductRefund;
use common\models\ProductOrderRefund;
use common\models\ProductOrder;
use common\models\FundWater;
use common\enums\ProductOrderEnum;
use common\forms\DrugRollBackForm;
use common\models\CashAccount;
use common\models\CashApply;
use member\models\forms\RegisterSubmitForm;
use Yii;
use yii\base\Exception;
use yii\helpers\Json;
use yii\web\Response;
class EplpayCallbackController extends \yii\web\Controller
{
public $enableCsrfValidation = false;
/**
* 下单回调
* @return mixed
*/
public function actionNotify()
{
Yii::$app->response->format = Response::FORMAT_RAW;
$content = file_get_contents('php://input');
$callback = new Callback();
$callback->content = $content;
$callback->type = 'eplpay_order';
$callback->save();
$data = json_decode($content, true);
if(!isset($data['payState']) || $data['payState'] == '01'){ //支付失败
return Json::encode([
'returnCode' => '0001',
'returnMsg' => '支付失败'
]);
}
$t = \Yii::$app->db->beginTransaction();
try {
$out_trade_no = $data['outTradeNo'];
$paymentProductOrder = PaymentProductOrder::find()->where([
'pay_order_no' => $out_trade_no
])->one();
if($paymentProductOrder){ // 产品订单
$data['pay_order_no'] = $out_trade_no;
$data['order_no'] = $paymentProductOrder->order_no;
$data['transaction_id'] =$data['transactionNo'];
$data['pay_type'] = 2;
$productOrderPayForm = new ProductOrderSubmitForm();
$order = $productOrderPayForm->paid($data);
$order_type=1;
} else { // 挂号订单
$PaymentRegister=PaymentRegister::find()->where([
'pay_order_no' => $out_trade_no
])->one();
if (!$PaymentRegister){
throw new Exception('订单不存在');
}
$data['order_no']= $PaymentRegister->order_no;
$data['transaction_id'] =$data['transactionNo'];
$data['pay_type'] = 2;
$RegisterSubmitForm = new RegisterSubmitForm();
$order = $RegisterSubmitForm->paid($data);
$order_type=2;
}
// 增加流水记录
$fundWater = new FundWater();
$fundWater->store_id = $order->store_id; // 入账
$fundWater->type = 'enter'; // 入账
$fundWater->order_type = $order_type;
$fundWater->order_id = $order->id;
$fundWater->user_id = $order->user_id;
$fundWater->service_user_id = $order_type==1?$order->su_id:$order->service_user_id;
$fundWater->order_no = $order->order_no;
$fundWater->price = $order_type==1 ? $order->total_pay_price : $order->price;
$fundWater->pay_type = 2; //易票联
$fundWater->saveOrFail();
$callback->status = 1;
$callback->save();
$t->commit();
} catch (Exception $e){
$t->rollBack();
$callback->message = $e->getMessage();
$callback->save();
return Json::encode([
'returnCode' => '0001',
'returnMsg' => '通信失败'
]);
}
return Json::encode([
'returnCode' => '0000',
'returnMsg' => '通信成功'
]);
}
/**
* 退款回调
*/
public function actionRefundNotify()
{
Yii::$app->response->format = Response::FORMAT_RAW;
$content = file_get_contents('php://input');
$callback = new Callback();
$callback->content = $content;
$callback->type = 'eplpay_refund';
$callback->save();
$data = json_decode($content, true);
$t = \Yii::$app->db->beginTransaction();
try {
$out_trade_no = $data['outTradeNo'];
$out_refund_no = $data['outRefundNo'];
$paymentProductOrder = PaymentProductOrder::find()->where([
'pay_order_no' => $out_trade_no,
])->one();
if ($paymentProductOrder){//产品订单
if ($paymentProductOrder->is_pay!=1){
throw new Exception('支付订单不存在');
}
$order = ProductOrder::find()->where([
'order_no' => $paymentProductOrder->order_no,
])->one();
if(!$order || $order->is_pay!=1){
throw new Exception('订单不存在');
}
$order_type = 1;
$refund = ProductOrderRefund::find()->where(['refund_no'=>$out_refund_no])->one();//退款订单
$payment_refund = PaymentProductRefund::find()->where(['refund_no'=>$out_refund_no])->one();
} else { //挂号订单
$PaymentRegister=PaymentRegister::find()->where([
'pay_order_no' => $out_trade_no
])->one();
if (!$PaymentRegister || $PaymentRegister->is_pay!=1){
throw new \yii\db\Exception('支付订单不存在');
}
$order = Register::find()->where([
'order_no' => $PaymentRegister->order_no,
])->one();
if(!$order || $order->is_pay!=1){
throw new Exception('订单不存在');
}
$order_type = 2;
$refund= RegisterRefund::find()->where(['refund_no'=>$out_refund_no])->one();//退款订单
$payment_refund= PaymentRegisterRefund::find()->where(['refund_no'=>$out_refund_no])->one();
}
if(!$refund || !$payment_refund){
throw new Exception('退款订单不存在');
}
if(isset($data['payState']) && $data['payState'] == '00'){
if ($order_type == 1){ //产品订单
$order->refund_status =3;//已退款
$order->status = ProductOrderEnum::REFUND;//已退款
$order->refund_time = time();
$order->save();
if($order->order_type ==1){//处方订单
$prescription = $order->prescription;
$prescription->refund_status = 2;//已退款
$prescription->save();
//药品库存回滚
$DrugRollBackForm = new DrugRollBackForm();
$DrugRollBackForm->rollBack($order->p_id);
}else{
//药品库存回滚
$DrugRollBackForm = new DrugRollBackForm();
$DrugRollBackForm->rollBack($order->id, $order->order_type);
}
// 退款分账结算更新
\Yii::$app->queue->delay(0)->push(new ProductOrderRefundJob([
'orderId' => $order->id
]));
if($order->is_online){ //平台订单状态同步
\Yii::$app->queue->delay(0)->push(new ProductOrderSyncPlatformJob([
'orderId' => $order->id,
'status' => 3
]));
}
}else{
//分账状态更新
$ledgerForm = new LedgerForm();
$ledgerForm->order_id = $order->id;
$ledgerForm->status = 2;
$ledgerForm->update('register');
}
$refund->is_refund = 1;
$refund->refund_time = date('Y-m-d H:i:s', strtotime($data['payTime']));
$refund->save();
$payment_refund->is_pay = 1;
$payment_refund->pay_type = 1;
$payment_refund->save();
// 增加流水记录
$fundWater = new FundWater();
$fundWater->store_id = $order->store_id; // 门店
$fundWater->type = 'refund'; // 出账
$fundWater->order_type = 1; // 产品订单
$fundWater->order_id = $order->id;
$fundWater->user_id = $order->user_id;
$fundWater->service_user_id = $order_type==1?$order->su_id:$order->service_user_id;
$fundWater->refund_no = $refund->refund_no;
$fundWater->price = $order_type==1 ? $order->total_pay_price : $order->price;
$fundWater->pay_type = 2; //易票联
$fundWater->saveOrFail();
} else {//退款失败
if($order_type == 1 && $order->is_send != 1){
$order->status = ProductOrderEnum::WAIT_SEND;//待发货
}
$order->refund_status = 0;
$order->refund_time = 0;
$order->save();
$refund->fail_reason = '退款失败';
$refund->is_refund = -1;
$refund->save();
$payment_refund->is_pay = -1;
$payment_refund->save();
}
$callback->status = 1;
$callback->save();
$t->commit();
}catch (\Exception $exception){
$t->rollBack();
$callback->message = $exception->getMessage();
$callback->save();
return Json::encode([
'returnCode' => '0001',
'returnMsg' => '通信失败'
]);
}
return Json::encode([
'returnCode' => '0000',
'returnMsg' => '通信成功'
]);
}
/**
* 提现回调
*/
public function actionWithdrawNotify()
{
Yii::$app->response->format = Response::FORMAT_RAW;
$content = file_get_contents('php://input');
$callback = new Callback();
$callback->content = $content;
$callback->type = 'eplpay_withdraw';
$callback->save();
$data = json_decode($content, true);
$out_trade_no = $data['outTradeNo'];
$t = \Yii::$app->db->beginTransaction();
try {
$applyOrder = CashApply::findOne(['order_no' => $out_trade_no,'check_status' => 2]); //已审核通过的提现申请
if(!$applyOrder){
throw new Exception('提现申请不存在');
}
$CashAccount = CashAccount::findOne([
'user_type' => $applyOrder->user_type,
'user_id' => $applyOrder->user_id
]);
if(!$CashAccount){
throw new Exception('分账账号不存在');
}
if(!isset($data['payState']) || $data['payState'] == '01'){ //提现失败
// $applyOrder->check_staus = 4;
// $applyOrder->check_result = '提现失败';
// $applyOrder->check_time = date('Y-m-d H:i:s');
$applyOrder->dakuan_status = -1;
$applyOrder->saveOrFail();
$CashAccount->able_cash = $CashAccount->able_cash + $applyOrder->apply_cash;
$CashAccount->frozen_cash -= $applyOrder->apply_cash;
$CashAccount->saveOrFail();
} else {
// 获取代扣订单
$chargeCpr = ChargeCashPayRecord::find()->where([
'order_no' => $applyOrder->order_no
])->one();
$applyOrder->dakuan_status = 1;
$applyOrder->dakuan_time = date('Y-m-d H:i:s');
$applyOrder->saveOrFail();
$CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $applyOrder->apply_cash, 2);
$CashAccount->withdrawn_cash = bcadd($CashAccount->withdrawn_cash, $applyOrder->apply_cash, 2);
$CashAccount->charge_cash = bcadd($CashAccount->charge_cash, $applyOrder->charge_cash, 2);
$CashAccount->saveOrFail();
// 扣除平台账户的金额
$platformAccount = CashAccount::find()->where([
'user_id' => 0,
'user_type' => 2
])->one();
if (!$platformAccount) {
throw new Exception('平台账户不存在');
}
$platformAccount->able_cash = bcsub($platformAccount->able_cash, $chargeCpr->charge_cash, 2);
$platformAccount->charge_cash = bcadd($platformAccount->charge_cash, $chargeCpr->charge_cash, 2);
$platformAccount->saveOrFail();
// 增加流水记录
$fundWater = new FundWater();
$fundWater->type = 'withdraw'; // 提现
$fundWater->order_type = 3; // 分账提现
$fundWater->order_id = $applyOrder->id;
$fundWater->user_id = $applyOrder->user_id;
$fundWater->user_type = $applyOrder->user_type;
$fundWater->price = $applyOrder->apply_cash;
$fundWater->pay_type = 2; //易票联
$fundWater->saveOrFail();
}
$callback->status = 1;
$callback->save();
$t->commit();
} catch (Exception $e){
$t->rollBack();
$callback->message = $e->getMessage();
$callback->save();
return Json::encode([
'returnCode' => '0001',
'returnMsg' => '通信失败'
]);
}
return Json::encode([
'returnCode' => '0000',
'returnMsg' => '通信成功'
]);
}
}