<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\api\controller\pay;

use app\api\controller\BaseApiController;
use Applications\sample\modules\pay\service\api\NotifyService;
use Applications\sample\modules\pay\service\api\PayService;
use Applications\sample\modules\pay\validate\api\PayValidator;
use Applications\sample\modules\pay\validate\domain\PayDomainValidator;
use think\Request;

/**
 * Pay
 * @package Applications\sample\api\controller\pay
 */
class Pay extends BaseApiController
{
    /**
     * 发起支付
     */
    public function create(Request $request)
    {
        $params = $request->param();
        $this->validate($params, PayValidator::class . '.create');
        $this->validateDomain($this->app->make(PayDomainValidator::class), $params, 'create');

        return json($this->app->make(PayService::class)->create($params['order_no'], $params['terminal']));
    }

    /**
     * 支付回调
     */
    public function callback($terminal, Request $request)
    {
        $rawData = $request->param();
        $orderNo = $rawData['out_trade_no'] ?? '';

        if ($orderNo) {
            $result = $this->app->make(NotifyService::class)->handle($orderNo, $rawData);
            return json($result);
        }

        return 'success';
    }
}
