NotifyCommonController.php 1.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/10/3
 * Time: 13:34
 */

namespace app\portal\controller;

use app\portal\model\OrderModel;
use cmf\controller\HomeBaseController;
use think\Db;

class NotifyCommonController extends HomeBaseController
{

    /**
     * 公共订单处理方法(支付宝、微信)
     * @param $order_sn 订单号
     * @param $pay_type 支付类型
     * @param $data 支付端返回的所有参数
     */
    public function handle($order_sn, $pay_type, $total_fee, $transaction_id, $data) {
        // 获取订单金额
        $where = [
            'order_sn' => $order_sn,
            'delete_time' => 0
        ];
        $where ['delete_time'] = 0;
        $orderInfo = Db::name('Order')
            ->where($where)
            ->find();
        $order_price = 0;
        $orderInfo['options'] = json_decode($orderInfo['options'],true);
        foreach ($orderInfo['options'] as $k=>$v) {
            if($v['id'] == 64) {
                $order_price = $v['user_choose'];
            }
        }
        if($order_price == $total_fee) {
            $order_model = new OrderModel();
            $orderInfo = $order_model->where($where)->find();
            if(!empty($orderInfo) && $orderInfo['status'] == 0) {
                $order_model->startTrans();
                $update = [
                    'status' => 1,
                    'pay_type' => $pay_type,
                    'pay_time' => time(),
                    'transaction_id' => $transaction_id,
                    'more' => json_encode($data)
                ];
                $status = $order_model->where($where)->update($update);
                if(!$status) {
                    $order_model->rollback();
                    return false;
                } else {
                    $order_model->commit();
                    return true;
                }
            }
        }
    }
}