微信PC端二维码支付-PHP后端

public function __construct()
{
    $this->app = Factory::payment(config("wechat.payment.default"));
}
// 调用支付生成二维码
public function doPay(Request $request)
{
    $order_id = $request->param('order_id');
    $where = [['id', '=', $order_id],];
    $info = Db::name('order')->where($where)->field('id,order_no,real_money,total_num,title,pay_status,order_status')->find();
    $order_data['order_no'] = createOrderNo();
    if (empty($info)) {
        return $this->apiReturn(ERROR, [], '订单信息不存在');
    }
    if ($info['order_status'] != 1) {
        return $this->apiReturn(ERROR, [], '订单已支付');
    }
    if ($info['pay_status'] == 1) {
        return $this->apiReturn(ERROR, [], '订单已支付');
    }
    $order_data['pay_time'] = time();
    $order_data['payment_type'] = 'wx_pay';
    $order_data['trade_type'] = 'native';
    Db::name('order')->where('id', $order_id)->update($order_data);
    $order_data['total_fee'] = APP_STATUS == 'localhost' ? 1 : $info['real_money'];
    $result = $this->unifiedOrder($order_data);
    $object = new QrCode($result['code_url']);
    $save_path = "./Uploads/Qrcode/" . date('Ym');
    if (!is_dir($save_path)) {
        echo $save_path;
        mkdir($save_path, 0777, true);
        chmod($save_path, 0777);
    }
    $filename = $save_path . "/" . date('YmdHis') . $info['order_no'] . '.png';
    $object->writeFile($filename);
    return $this->apiReturn(SUCCESS, ['qrcode_url' => $this->request->domain() . ltrim($filename, '.')], 'ok');
}
/*
 * 统一下单
 */
public function unifiedOrder($params)
{
    return $this->app->order->unify([
        'body' => '游行迹旅行网',
        'out_trade_no' => $params['order_no'],
        'total_fee' => $params['total_fee'],
        'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
        //'openid' => $params['openid'],
        'notify_url' => $this->request->domain() . '/api/Apinotfy/wx_page_payment_notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
    ]);
}

/**
 * 微信pc端扫码支付回调通知
 */
public function wx_page_payment_notify()
{
    $response = $this->app->handlePaidNotify(function ($message, $fail) {
        if ($message['return_code'] === 'SUCCESS') {
            // 取出订单,检测是否存在
            $order = Db::name("order")->where("order_no", $message['out_trade_no'])->find();
            logs(json_encode($order), 'wx_page_refund');

            if (!$order) { // 如果订单不存在
                logs('无效的订单信息', 'wx_page_refund_log');
                return $fail("无效的订单信息");
            }

            if ($order['pay_status'] == 1) {
                logs('请勿重复支付', 'wx_page_refund_log');
                return $fail("请勿重复支付");
            }

            if ($order['order_status'] != 1) {
                logs('请勿重复支付', 'wx_page_refund_log');
                return $fail("请勿重复支付");
            }

            ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
            $check = $this->app->order->queryByOutTradeNumber($message['out_trade_no']);

            //未支付
            if ($check['trade_state'] != 'SUCCESS') {
                logs('支付失败', 'wx_page_refund_log');
                return $fail("支付失败");
            }

            //支付金额
            $money = bcdiv($message['total_fee'], 100, 2); //通知信息里的金额是分,需换成元
            $money = $message['total_fee']; //通知信息里的金额是分,需换成元
            if (APP_STATUS != 'localhost') {
                if ($order['real_money'] != $money) {
                    logs('支付金额不匹配--'.$money, 'wx_page_refund_log');
                    return $fail('支付金额不匹配');
                }
            }

            // 用户是否支付成功
            if ($message['result_code'] == 'SUCCESS') {
                //根据商户订单号查询
                $this->app->order->queryByOutTradeNumber("商户系统内部的订单号(out_trade_no)");

                //根据微信订单号查询
                $this->app->order->queryByTransactionId("微信订单号(transaction_id)");

                //更新订单状态等信息
                $data = [
                    "transaction_id" => $message['transaction_id'],
                    "order_status" => 2,
                    "pay_status" => 1,
                    "update_time" => time(),
                ];
                if ($order['source_type'] == 2) {
                    $data['check_status'] = 1;
                }

                $bool = Db::name("order")->where("order_no", $message['out_trade_no'])->update($data);
                if (!$bool) {
                    logs('更新订单信息失败', 'wx_page_refund_log');
                    return $fail("更新订单信息失败");
                }
                // 用户支付失败
            } elseif ($message['result_code'] == 'FAIL') {
                logs('支付失败', 'wx_page_refund_log');
                return $fail("支付失败");
            }
            logs('支付成功', 'wx_page_refund_log');
            return true; // 返回处理完成
        } else {
            logs('通信失败,请稍后再通知我', 'wx_page_refund_log');
            return $fail('通信失败,请稍后再通知我');
        }
    });
    $response->send();
    //return $response;
}

标签

发表评论