cxgj/modules/api/models/PaymentOrderForm.php
2023-11-27 09:45:13 +08:00

192 lines
7.0 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021-5-13
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\components\SiteHelper;
use app\components\SysConst;
use app\components\YopointApi;
use app\models\common\CommonPaymentSettingForm;
use app\models\common\payment\Payment;
use app\models\common\payment\PaymentOrder;
use app\models\integral\Integral;
use app\models\Model;
use app\models\Order;
use app\models\PaymentTypes;
use app\models\StoreEarnings;
use app\models\UniqueOrderNo;
use app\models\User;
use app\models\sms\SmsMsgHelper;
use app\models\sms\SmsTpl;
use app\components\EncryptHelper;
use app\models\YopointNotify;
class PaymentOrderForm extends ApiModel
{
public $cx_mch_id;
public $user_id;
public $id;
public $pay_type;
public $redis_key_order = 'payment_order';
public function rules()
{
return [
[['cx_mch_id', 'user_id','id'], 'integer'],
[['id','pay_type'], 'string'],
[['cx_mch_id', 'user_id','id'], 'required'],
];
}
public function getPayType($cx_mch_id, $platform = 'wxmp')
{
$form = new CommonPaymentSettingForm();
$form->cx_mch_id = $cx_mch_id;
$res = $form->search();
$pay_type_list = $res['pay_types'];
$pay_types = [];
foreach($pay_type_list as $index => $item){
if(!$item['is_show'] || !$item['is_open'])
continue;
//排除
if(in_array($item['short_name'], [SysConst::$cxPayTypeEnAlipay,SysConst::$cxPayTypeEnBank,SysConst::$cxPayTypeEnIntegral,SysConst::$cxPayTypeEnUnionpay,SysConst::$cxPayTypeEnWxpayPoints]))
continue;
//@TODO 根据平台来源显示支付方式
$pay_types[] = $item;
}
return $pay_types;
}
//创建支付订单
public function createPayment()
{
if(!$this->validate()){
return $this->getModelError();
}
$orderTimeoutCancel = SiteHelper::getCustomiseOptionByKey("orderTimeoutCancel", "hump");
$orderTimeoutCancel = $orderTimeoutCancel * 60;
$order = Order::findOne(['id' => $this->id,'user_id' => $this->user_id]);
if(empty($order)){
return Model::asReturnError("订单不存在");
}
if(!empty($order->is_pay)){
return Model::asReturnError("订单已支付");
}
$time = $order->created_at + $orderTimeoutCancel;
if((time() >= $time || $order->cancel_status == 1) && $order->plugin_sign == 'box_book'){
$order->cancel_status = 1;
$order->cancel_time = time();
$order->status = 3;
if(!$order->save()){
return Model::asReturnError("订单异常");
}
return Model::asReturnError("订单超时!");
}
$t = \Yii::$app->db->beginTransaction();
// if(!empty($order->integral) && $order->plugin_sign == 'box_book'){
// $order_type = UniqueOrderNo::getOrderTypeByOrderNo($order->order_no);
// $desc = $order->plugin_sign == 'box_book' ? '包厢预约抵扣' : '商品下单抵扣';
// $r = Integral::userIntegralWalletLog($order->user_id,Integral::TYPE_PAY,$order->integral,$desc,$order_type,$order->order_no);
// if(!$r){
// $t->rollBack();
// return Model::asReturnError("订单积分异常");
// }
// }
$order->total_goods_price = sprintf("%.2f",$order->total_goods_price);
if($order->total_goods_price <= 0.00){
if($order->plugin_sign == SysConst::$cxPluginSceneBallCart){
$order->status = 1;
}else{
$order->status = 0;
}
$order->is_pay = 1;
$order->pay_type = 0;
if(!$order->save()){
$t->rollBack();
return Model::asReturnError("订单异常");
}
$order_type = UniqueOrderNo::getOrderTypeByOrderNo($order->order_no);
if(!empty($order->integral) && $order->integral > 0){
if($order->plugin_sign == SysConst::$cxPluginSceneBallCart){
$desc = '包厢预约抵扣';
} elseif($order->plugin_sign == SysConst::$cxPluginSceneGoods){
$desc = '商品下单抵扣';
}else{
$desc = '未知';
}
$integral = sprintf("%.2f",$order->integral);
$r = Integral::userIntegralWalletLog($order->user_id,Integral::TYPE_PAY,$integral,$desc,$order_type,$order->order_no);
if(!$r){
$t->rollBack();
return Model::asReturnError("订单积分异常");
}
}
$t->commit();
if(!empty($order->yopoint_notify_id)){
// 查找友朋回调接口
$find_yopoint = YopointNotify::findOne([
'id' => $order->yopoint_notify_id,
]);
if(!empty($find_yopoint->return_data)){
$obj = new YopointApi();
$json_de = json_decode($find_yopoint->return_data,true);
$obj->openDevCall($json_de['data']['NotifyUrl'],$json_de['data']['ReceiptNo'],$order->order_no,1);
}
}
return Model::asReturnSuccess("ok");
}
$paymentOrders = [];
//支付方式检查
$pay_types = $this->getPayType($this->cx_mch_id);
if(!in_array($this->pay_type, array_column($pay_types, 'short_name'))){
return $this->apiReturnError('不支持此支付方式');
}
if($order->plugin_sign == SysConst::$cxPluginSceneBallCart){
$title = '预定包厢';
$notifyClass = '\\app\\models\\common\\notify\\payment\\BoxBookPayMentNotify';
} elseif($order->plugin_sign == SysConst::$cxPluginSceneGoods){
$title = '商品下单';
$notifyClass = '\\app\\models\\common\\notify\\payment\\GoodsPayMentNotify';
}else{
return Model::asReturnError("支付项目异常");
}
if(empty($title) || empty($notifyClass)){
$t->rollBack();
return Model::asReturnError("支付数据异常");
}
$order->total_goods_price = sprintf("%.2f",$order->total_goods_price);
$paymentOrder = new PaymentOrder([
'orderNo' => $order->order_no,
'amount' => (float) $order->total_goods_price,
'title' => $title,
'notifyClass' => $notifyClass,
'supportPayTypes' => array_column($pay_types, 'short_name'),
'payType' => PaymentTypes::payTypeKeyToId($this->pay_type)
]);
$payment = new Payment();
$paymentOrders[] = $paymentOrder;
$res = $payment->createOrder($paymentOrders);
if($res['code'] != 0){
$t->rollBack();
return $res;
}
$t->commit();
return $res;
}
}