<?php

namespace app\api\controller;

use app\common\enum\PayEnum;
use app\common\logic\PayNotifyLogic;
use app\common\model\retail\Cashierclass;
use app\common\model\store_order\StoreOrder;
use app\common\service\pay\PayService;
use support\Cache;
use support\Log;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]

/**
 * 支付
 * Class PayController
 * @package app\api\controller
 */
class PayController extends BaseApiController
{

    public $notNeedLogin = ['notifyMnp', 'alipay_return', 'alipay_notify'];

    /**
     * @notes 小程序支付回调
     */
    public function notifyMnp()
    {
        $app = new PayService(1);
        $result  = $app->wechat->callback(Request()->post());
        if ($result && $result->event_type == 'TRANSACTION.SUCCESS') {
            $ciphertext = $result->resource['ciphertext'];
            if ($ciphertext['trade_state'] === 'SUCCESS') {
                $attach = $ciphertext['attach'];
                switch ($attach) {
                    case 'recharge':
                        PayNotifyLogic::handle('recharge', $ciphertext['out_trade_no'], $ciphertext);
                        $app->wechat->success();
                        break;
                    case 'wechat_common':
                    default:
                        PayNotifyLogic::handle('wechat_common', $ciphertext['out_trade_no'], $ciphertext);
                        $app->wechat->success();
                        break;
                }
            }
        }else{
            if ($result && $result->event_type == 'REFUND.SUCCESS') {
                $ciphertext = $result->resource['ciphertext'];
                if ($ciphertext['refund_status'] === 'SUCCESS') {
                    //处理订单  -1判断是退的一单还是拆分的订单
                    $out_trade_no = $ciphertext['out_trade_no'].'-1';
                    $check = StoreOrder::where('order_id',$out_trade_no)->count();
                    if($check){
                        $ciphertext['out_trade_no'] =$out_trade_no;
                    }
                    PayNotifyLogic::handle('refund', $ciphertext['out_trade_no'], $ciphertext);
                    $app->wechat->success();

                }
            }
        }
    }

    /**
     * @notes 查询订单支付状态
     */
    public function wechatQuery()
    {
        $order_no = $this->request->get('order_no');
        $order = [
            'out_trade_no' => $order_no,
        ];
        $app = new PayService(0);

        $res = $app->wechat->query($order);
        if ($res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功') {
            PayNotifyLogic::handle('wechat_common', $res['out_trade_no'], $res);
            return $this->success('支付成功');
        } else {
            return $this->fail('订单支付中');
        }
    }

    /**
     * 支付宝同步回调地址
     */
    public function alipay_return()
    {
        $app = new PayService();
        $result  = $app->alipay->callback(Request()->post());
        if ($result) {
            $data = $result->toArray();
            if ($data['trade_status'] === 'TRADE_SUCCESS') {
                $attach = $data['extend_params']['attach']??'';
                switch ($attach) {
                    case 'alipay_cashier':
                        PayNotifyLogic::handle('alipay_cashier', $data['out_trade_no'], $data);
                        $app->alipay->success();
                        break;
                }
            }
        }
    }
    /**
     * 支付宝异步回调地址
     */
    public function alipay_notify()
    {
        $app = new PayService();
        $result  = $app->alipay->callback(Request()->post());
        if ($result) {
            $data = $result->toArray();
            if ($data['trade_status'] === 'TRADE_SUCCESS') {
                $attach = $data['extend_params']['attach']??'';
                switch ($attach) {
                    case 'alipay_cashier':
                        PayNotifyLogic::handle('alipay_cashier', $data['out_trade_no'], $data);
                        $app->alipay->success();
                        break;
                }
            }
        }
    }
}