canyin-project/ybcy/controllers/channelApi/PayController.php
2024-11-01 16:07:54 +08:00

366 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controllers\channelApi;
use app\controllers\common\FuiouController;
use app\models\common\WeChatPay;
use Illuminate\Support\Facades\DB;
use Yii;
use EasyWeChat\Factory;
use app\models\common\WeChat;
use app\models\common\Config;
use app\models\common\Member;
use app\models\common\Ali;
use app\models\common\ZiJie;
use app\models\common\CallbackOrder;
class PayController extends CommonController{
public $enableCsrfValidation = false;
public function actionPay(){
//file_put_contents('toutiaopay.log',1111);
// $res=file_get_contents('toutiao.log');
// print_R(json_decode(json_decode($res,true)['msg'],true));die;
$request = Yii::$app->request;
if ($request->isGet) {
return $this->result(2, '请求异常');
}
$result=axios_request();
$appType = $this->wqData['appType'];
$orderType = $result['orderType'];
$storeId = 0;
$note = '订单支付';
if ($orderType == 1) {
//外卖
$note = '外卖订单';
$order=(new \yii\db\Query())
// ->select(['id','money','storeId','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_takeout_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$storeId = $order['storeId'];
$userId=$this->wqData['userId'];
$url="channelApi/pay-notice/order-notices";
//file_put_contents('toutiaopay.log',$appType);
$tableName='ybwm_takeout_order';
}
if ($orderType == 2) {
//充值
$note = '充值订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_user_balance_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$storeId = $order['storeId'];
$userId = $order['userId'];
$url="channelApi/pay-notice/balance-order-notices";
$tableName='ybwm_user_balance_order';
}
if ($orderType == 3) {
//积分商城
$note = '积分商城订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_integral_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$userId = $order['userId'];
$url="channelApi/pay-notice/integral-order-notice";
}
if ($orderType == 4) {
//券包
$note = '券包订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_roll_bag_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$userId = $order['userId'];
$url="channelApi/pay-notice/roll-bag-order-notice";
$tableName='ybwm_roll_bag_order';
}
if ($orderType == 5) {
//收银
$note = '收银订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid','storeId'])
->from('{{%ybwm_cashier_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$storeId = $order['storeId'];
$userId = $order['userId'];
$url="channelApi/pay-notice/cashier-order-notice";
$tableName='ybwm_cashier_order';
}
if ($orderType == 6) {
//快餐
$note = '快餐订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid','storeId'])
->from('{{%ybwm_instore_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$storeId = $order['storeId'];
$userId = $order['userId'];
$url="channelApi/pay-notice/fast-order-notice";
$tableName='ybwm_instore_order';
}
if ($orderType == 7) {
//店内
$note = '店内订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid','storeId','prepay_params'])
->from('{{%ybwm_instore_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] >2) {
return $this->result(2, '订单状态异常');
}
$storeId = $order['storeId'];
$userId = $order['userId'];
$url="channelApi/pay-notice/instore-order-notices";
$tableName='ybwm_instore_order';
}
if ($orderType == 8) {
//会员
$note = '会员订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_user_vip_order}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] !=1) {
return $this->result(2, '订单状态异常');
}
$userId = $order['userId'];
$url="channelApi/pay-notice/vip-order-notice";
$tableName='ybwm_user_vip_order';
}
if ($orderType == 9) {
//预约订单
$note = '预约订单';
$order=(new \yii\db\Query())
// ->select(['id','money','state','outTradeNo','userId','uniacid'])
->from('{{%ybwm_appointment}}')
->where('id=:id',[':id'=>$result['orderId']])->one();
if ($order['state'] !=1) {
return $this->result(2, '订单状态异常');
}
$userId = $order['userId'];
$url="channelApi/pay-notice/appointment-order-notice";
$tableName='ybwm_appointment';
}
if(!$order){
return $this->result(2, '订单不存在');
}
// $user=(new \yii\db\Query())
// ->select(['id','uniacid','openId','balance','wechatOpenId'])
// ->from('{{%ybwm_member}}')
// ->where('id=:id',[':id'=>$userId])->one();
$user=(new \yii\db\Query())
->select(['id','uniacid','openId','balance','wechatOpenId'])
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$this->wqData['userId']])->one();
if ($result['payType'] == 1) {
$userBindData=(new \yii\db\Query())
// ->select('balance')
->from('{{%ybwm_member_bind}}')
->where('userId=:userId and storeId=:storeId', [':userId' => $this->wqData['userId'],':storeId' => $storeId])->one();
//余额支付
if ($userBindData['balance'] < $order['money']) {
return $this->result(2, '余额不足!');
}
Member::saveBalance($order['userId'],2,$order['money'],2,$note,$order['uniacid'],0,$storeId);//扣除余额
if($orderType==1){
Yii::$app->db->createCommand()->update('{{%ybwm_takeout_order}}', ['profitSharing'=>2,'changeAt'=>time()], ['id'=>$result['orderId']])->execute();
CallbackOrder::order($order['id'],5);//外卖回调
}
if($orderType==3){
CallbackOrder::integralOrder($order['id'],5);//积分商城回调
}
if($orderType==4){
CallbackOrder::rollBagOrder($order['id'],5);//券包回调
}
if($orderType==5){
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_order}}', ['profitSharing'=>2,'changeAt'=>time()], ['id'=>$result['orderId']])->execute();
CallbackOrder::cashierOrder($order['id'],5);//收银回调
}
if($orderType==6){
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['profitSharing'=>2,'changeAt'=>time()], ['id'=>$result['orderId']])->execute();
CallbackOrder::fastOrder($order['id'],5);//快餐回调
}
if($orderType==7){
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['profitSharing'=>2,'changeAt'=>time()], ['id'=>$result['orderId']])->execute();
CallbackOrder::instoreOrder($order['id'],5);//店内回调
}
if($orderType==8){
CallbackOrder::vipOrder($order['id'],5);//会员卡回调
}
if($orderType==9){
CallbackOrder::appointmentOrder($order['id'],5);//预约回调
}
return $this->result(1, '成功');
}
if ($result['payType'] == 2) {
//货到付款
if($orderType==1){
CallbackOrder::order($order['id'],10);//外卖回调
}
return $this->result(1, '成功');
}
$uniacid = $order['uniacid'];
$bind_data = $this->isBindPayChannel($storeId,$uniacid,2);
if(!$bind_data){
return $this->result(2, '请求异常');
}
if ($appType == 1||$appType == 2) {
$profit_sharing='N';
if($storeId){
$storeSet=Config::getStoreSet('serviceCharge',$storeId);
if($storeSet['profit_sharing']){
$profit_sharing=$storeSet['profit_sharing'];
}
if($profit_sharing=="Y" AND $storeSet['subMchId'] AND $storeSet['sonService']==1){
$crontab=(new \yii\db\Query())
->from('{{%ybwm_crontab}}')
->where('name=:name AND switch=1',[':name'=>'堂食分账'])->one();
$fzData['name']='堂食分账';
$fzData['route']='tasks/common/profit-sharing';
$fzData['crontab_str']='*/5 * * * *';
$fzData['switch']=1;
if(!$crontab){
Yii::$app->db->createCommand()->insert('{{%ybwm_crontab}}', $fzData)->execute();
}
$orderData['profitSharing']=1;
}
if($storeSet['subMchId'] AND $storeSet['sonService']==1){
$orderData['payPort']=1;//商家服务商支付
}else{
$orderData['payPort']=2;//平台服务商支付
}
if($orderData){
if($orderType==1){
Yii::$app->db->createCommand()->update('{{%ybwm_takeout_order}}', $orderData, ['id'=>$result['orderId']])->execute();
}
if($orderType==5){
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_order}}', $orderData, ['id'=>$result['orderId']])->execute();
}
if($orderType==6 || $orderType==7){
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', $orderData, ['id'=>$result['orderId']])->execute();
}
if($orderType==9){
Yii::$app->db->createCommand()->update('{{%ybwm_appointment}}', $orderData, ['id'=>$result['orderId']])->execute();
}
}
}
//微信小程序支付
try{
$origin=$appType;
if($appType==2){
$user['openId']=$user['wechatOpenId']?:$user['openId'];
}
//如果是公众号支付origin就传2
//$pay = WeChatPay::test($user['uniacid'],1, $order['outTradeNo'], $order['money'],$url,$note,$this->wqData['module'],$user['openId'], $storeId,$profit_sharing);
// $pay = WeChat::wxPay($user['uniacid'],$origin, $order['outTradeNo'], $order['money'],$url,$note,$this->wqData['module'],$user['openId'], $storeId,$profit_sharing);
if(empty($order['prepay_params'])){
$payData=Config::getSystemSet('miniConfig',$uniacid);
$fuiou_obj = new FuiouController();
$res = $fuiou_obj->getMiniPayData($payData,$user,$order,$bind_data,$url);
if($res){
$jsonString = json_encode($res,true);
// 使用json_decode将JSON字符串解码为数组
$array = json_decode($jsonString, true);
$res_data = [];
$res_data['appId'] = $array['sdk_appid'] ? $array['sdk_appid'] : '';
$res_data['timeStamp'] = $array['sdk_timestamp'] ? $array['sdk_timestamp'] : '';
$res_data['nonceStr'] = $array['sdk_noncestr'] ? $array['sdk_noncestr'] : '';
$res_data['package'] = $array['sdk_package'] ? $array['sdk_package'] : '';
$res_data['signType'] = $array['sdk_signtype'] ? $array['sdk_signtype'] : '';
$res_data['paySign'] = $array['sdk_paysign'] ? $array['sdk_paysign'] : '';
$store_data['prepay_params'] = json_encode($res_data,true);
$store_data['bind_id'] = $bind_data['bind_id'];
if($orderType == 2) {//充值订单
Yii::$app->db->createCommand()->update('{{%ybwm_user_balance_order}}',$store_data, ['id'=>$result['orderId']])->execute();
}
if($orderType == 1) {//外卖订单
Yii::$app->db->createCommand()->update('{{%ybwm_takeout_order}}',$store_data, ['id'=>$result['orderId']])->execute();
}
if($orderType == 7) {//店内订单
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}',$store_data, ['id'=>$result['orderId']])->execute();
}
return $this->result(1, '下单成功', $res_data);
} else {
return $this->result(2, '订单异常请重新下单!');
}
} else {
return $this->result(1, '下单成功', json_decode($order['prepay_params'],true));
}
}catch (\Exception $e){
return $this->result(2, '支付参数配置有误',$e);
}
}
if ($appType == 2) {
//微信公众号支付
}
if ($appType == 3) {
//支付宝小程序支付
try{
$pay = Ali::pay($user['uniacid'], $order['outTradeNo'], $order['money'],$note,$user['openId'],$orderType);
if($pay['code']==1){
$pay=$pay['data'];
Yii::$app->db->createCommand()->update('{{%'.$tableName.'}}', ['transaction_id'=>$pay['alipay_trade_create_response']['trade_no']], ['id'=>$order['id']])->execute();
}else{
return $this->result(2, $pay['msg']);
}
}catch (\Exception $e){
return $this->result(2, '支付参数配置有误',$e);
}
}
if ($appType == 4) {
//百度小程序支付
}
if ($appType == 7) {
//字节跳动小程序支付
$url=Yii::$app->request->hostInfo.'/channelApi/pay-notice/tou-tiao-notice';
if(Yii::$app->params['isDev']==true){
$url=Yii::$app->request->hostInfo.'/addons/yb_wm/channelApi/pay-notice/tou-tiao-notice';
}
$pay = ZiJie::pay2($user['uniacid'], $order['outTradeNo'], $order['money'],$note,$user['openId'],$orderType,$url);
if($pay['order_id']){
//CallbackOrder::order($order['id'],3);
//Yii::$app->db->createCommand()->update('{{%'.$tableName.'}}', ['transaction_id'=>$pay['order_id'],'state'=>2], ['id'=>$order['id']])->execute();
}else{
return $this->result(2, $pay['msg']);
}
}
if(!is_array($pay)){
return $this->result(2, '支付参数配置有误',$pay);
}
return $this->result(1, '成功', $pay);
}
}