canyin-project/ybcy/commands/tasks/InStoreOrderController.php

97 lines
4.5 KiB
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\commands\tasks;
use app\models\common\CallbackOrder;
use app\models\common\Config;
use app\models\common\WeChat;
use Yansongda\Pay\Pay;
use yii;
use yii\console\Controller;
use yii\console\ExitCode;
class InStoreOrderController extends Controller
{
public function actionWechatPayQuery()
{
$data = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['state' => 1, 'payAt' => 0])
->andWhere(['<=', 'createdAt', time() - 60])
->orderBy('id desc')->limit(20)->all();
if (!empty($data)) {
foreach ($data as $key => $order) {
$uniacid = $order['uniacid'];
$config = WeChat::getWxPayConfig($uniacid, 1, $order['storeId']);
if ($config['sub_mch_id']) {
$order['sub_mch_id'] = $config['sub_mch_id'];
$order['out_trade_no'] = $order['outTradeNo'];
try {
$wechat = Pay::wechat($config)->find($order);
} catch (\Exception $e) {
echo json_encode(['code' => 2, 'msg' => $e->getMessage()]);
die;
}
} else {
try {
$wechat = Pay::wechat($config)->find($order['outTradeNo']);
if ($wechat->return_code == 'SUCCESS' && $wechat->return_msg == 'OK' && $wechat->result_code == 'SUCCESS' && $wechat->trade_state =='SUCCESS') {
if ($config['sub_mch_id']) {
$storeSet = Config::getStoreSet('serviceCharge', $order['storeId']);//商户子商户号设置
if ($storeSet['sonService'] == 1 and $storeSet['subMchId']) {
$orderId = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['out_trade_no' => $order['outTradeNo']])
->one()['id'];
}
}
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['tableId' => $order['tableId'], 'state' => 1, 'storeId' => $order['storeId'], 'uniacid' => $uniacid])->execute();
CallbackOrder::payOrder($order['id'], 1, $order['userId']);
echo $order['outTradeNo'].'订单更新支付状态成功';
}
} catch (\Exception $e) {
echo json_encode(['code' => 2, 'msg' => $e->getMessage()],320);
echo "\n";
}
}
}
}
}
public function actionAliPayQuery(){
$data = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['state' => 1, 'payAt' => 0])
->andWhere(['<=', 'createdAt', time() - 60])
->orderBy('id desc')->limit(20)->all();
if (!empty($data)) {
foreach ($data as $key => $order) {
$uniacid = $order['uniacid'];
$aliConfig=Config::getSystemSet('cashierSet',$uniacid);
if(!$aliConfig){
echo json_encode(['code'=>2,'msg'=>'请在应用收银台配置支付宝设置'],320);die;
}
if($aliConfig['aliPay']==2){
echo json_encode(['code'=>2,'msg'=>'请在应用收银台开启支付宝支付'],320);die;
}
$config =array(
'app_id' =>$aliConfig['aliAppId'],
'ali_public_key' =>$aliConfig['publicKey'],
'private_key' =>$aliConfig['privateKey'],
);
try {
$alipay = Pay::alipay($config)->find($order['outTradeNo']);
if ($alipay->msg == 'Success' && $alipay->code == 10000) {
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['tableId' => $order['tableId'], 'state' => 1, 'storeId' => $order['storeId'], 'uniacid' => $uniacid])->execute();
CallbackOrder::payOrder($order['id'], 1, $order['userId']);
echo $order['outTradeNo']."订单更新支付状态成功\n";
}
}catch (\Exception $e){
echo json_encode(['code'=>2,'msg'=>$e->getMessage()],320);
echo "\n";
}
}
}
}
}