<?php

namespace app\api\controller;

use app\api\service\AlipayTradeService;
use app\BaseController;
use think\facade\Log;

class Wttest extends BaseController
{


    //测试支付宝支付
    public function paytest()
    {

        $pay = new AlipayTradeService();
        $pay->ReturnUrl = url('Wttest/payhtml', [], true, true);
        $pay->NotifyUrl = url('Wttest/notifyurl', [], true, true);
        $biz_content['body'] = '测试测试';
        $biz_content['subject'] = '苹果手机5G';
        $biz_content['total_amount'] = '0.01';
        $biz_content['product_code'] = 'FAST_INSTANT_TRADE_PAY';
        $biz_content['out_trade_no'] = 'z' . time();
        $pay->pcpaypage($biz_content);
    }

    //查询支付宝订单
    public function checkpay()
    {
//        [code] => 10000
//    [msg] => Success
//    [buyer_logon_id] => 137******91
//    [buyer_pay_amount] => 0.00
//    [buyer_user_id] => 2088802284861832
//    [invoice_amount] => 0.00
//    [out_trade_no] => z1748403522
//    [point_amount] => 0.00
//    [receipt_amount] => 0.00
//    [send_pay_date] => 2025-05-28 11:38:52
//    [total_amount] => 0.01
//    [trade_no] => 2025052822001461831415557350
//    [trade_status] => TRADE_SUCCESS
        $pay = new AlipayTradeService();
        $biz_content['out_trade_no'] = 'z1748403522';
//        $biz_content['out_trade_no'] = '2025052822001461831415557350';
        $result = $pay->payquery($biz_content);
        if ($result->code == 10000 && $result->trade_status == 'TRADE_SUCCESS') {


        }
        print_r($result);
    }

    public function payhtml()
    {
        $get = input();
        Log::write('支付宝前台回调');
        Log::write(json_encode($get));
        Log::write($get);
        print_r($get);
        exit;
    }

    public function notifyurl()
    {
        $post = input();
        $alipaySevice = new AlipayTradeService();
        $result = $alipaySevice->check($post);
        Log::write('支付宝后台回调');
        Log::write($post);
        if ($result) {//验证成功
            Log::write('支付宝后台回调验签成功');
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //请在这里加上商户的业务逻辑程序代
            //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
            //商户订单号
            $out_trade_no = $post['out_trade_no'];
            //支付宝交易号
            $trade_no = $post['trade_no'];
            //交易状态
            $trade_status = $post['trade_status'];
            $total_amount = $post['total_amount'];
            if ($post['trade_status'] == 'TRADE_FINISHED') {

                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //请务必判断请求时的total_amount与通知时获取的total_fee为一致的
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
            } elseif ($post['trade_status'] == 'TRADE_SUCCESS') {

                $biz_content['out_trade_no'] = $out_trade_no;
                $result = $alipaySevice->payquery($biz_content);
                if ($result->code == 10000 && $result->trade_status == 'TRADE_SUCCESS') {

                    Log::write('支付宝后台回调支付查询');
                    Log::write($result);
                }
            }
            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
            echo "success";    //请不要修改或删除
        } else {
            //验证失败
            Log::write('支付宝后台回调验签失败');
            echo "fail";

        }

    }




//
//[2025-05-28T13:33:50+08:00][info] 支付宝后台回调
//[2025-05-28T13:33:50+08:00][info] array (
//'gmt_create' => '2025-05-28 13:33:44',
//'charset' => 'utf-8',
//'gmt_payment' => '2025-05-28 13:33:48',
//'notify_time' => '2025-05-28 13:33:49',
//'subject' => '苹果手机5G',
//'sign' => 'B5f83l47uzBW92Gav8qaA5UplRHFG+pXmt2jLornvYSXoPSalmg60uqU6Jd/MvLdAPwke4PeSPtB6dRNZJ1GF+zPFBy0QHi113P6SxTNbhqqWHmqTQT9x9olcYeNhqdI1NFfk/ALrqMwR/q+LGZhcsMPmX3Q4AqfiE1FXWxVck7STucnEUxpYpqsqjnVP6kyD+NQKB/MOkGaQM79ox9xIsfkvm2f/aqeSobG6+Yj9RuEEvXxP1CtPSPaCRuKH0wI7f0/KzFUY0f+TxbuBKYX2FydsAxRoRqjxFHFOMAsO6b6IO1pEZ0g0qklTHD52DtskL2lj/z8p2U6kyDB38sfVA==',
//'buyer_id' => '2088802284861832',
//'body' => '测试测试',
//'invoice_amount' => '0.01',
//'version' => '1.0',
//'notify_id' => '2025052801222133349061831412509666',
//'fund_bill_list' => '[{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]',
//'notify_type' => 'trade_status_sync',
//'out_trade_no' => 'z1748410415',
//'total_amount' => '0.01',
//'trade_status' => 'TRADE_SUCCESS',
//'trade_no' => '2025052822001461831415577006',
//'auth_app_id' => '2021005147679343',
//'receipt_amount' => '0.01',
//'point_amount' => '0.00',
//'buyer_pay_amount' => '0.01',
//'app_id' => '2021005147679343',
//'sign_type' => 'RSA2',
//'seller_id' => '2088370543757256',
//)
//[2025-05-28T13:33:50+08:00][info] 支付宝后台回调验签成功
//[2025-05-28T13:33:50+08:00][info] 支付宝后台回调支付查询
//[2025-05-28T13:33:50+08:00][info] (object) array(
//'code' => '10000',
//'msg' => 'Success',
//'buyer_logon_id' => '137******91',
//'buyer_pay_amount' => '0.00',
//'buyer_user_id' => '2088802284861832',
//'invoice_amount' => '0.00',
//'out_trade_no' => 'z1748410415',
//'point_amount' => '0.00',
//'receipt_amount' => '0.00',
//'send_pay_date' => '2025-05-28 13:33:48',
//'total_amount' => '0.01',
//'trade_no' => '2025052822001461831415577006',
//'trade_status' => 'TRADE_SUCCESS',


}