<?php

namespace Leady\YeePay\Commands;

use Exception;
use Yeepay\Yop\Sdk\Service\Aggpay\AggpayClientBuilder;
use Yeepay\Yop\Sdk\Service\Aggpay\Model\PrePayRequest;
use Yeepay\Yop\Sdk\Service\Aggpay\Model\TutelagePrePayRequest;
use Yeepay\Yop\Sdk\Service\Aggpay\Model\WechatConfigAdd0Request;
use Yeepay\Yop\Sdk\Service\Aggpay\Model\WechatConfigQuery0Request;

class Aggpay extends InitConfig
{

    protected $client;

    public function __construct()
    {
        parent::__construct();
        $this->client = AggpayClientBuilder::builder($this->getSdkConfig())->build();
    }

    public function PrePay(array $data)
    {
        try {
            $request = new PrePayRequest();
            $request->setParentMerchantNo(config('yeepay.merchantNo'))
                ->setMerchantNo(config('yeepay.merchantNo'))
                ->setOrderId($data['orderId'] ?? '')
                ->setOrderAmount($data['orderAmount'] ?? '')
                ->setExpiredTime(now()->addMinutes(30)->toDateTimeString())
                ->setNotifyUrl($data['notifyUrl'] ?? '')
                ->setRedirectUrl($data['redirectUrl'] ?? '')
                ->setMemo($data['memo'] ?? '')
                ->setGoodsName($data['goodsName'] ?? '')
                ->setPayWay($data['payWay'] ?? 'USER_SCAN')
                ->setChannel($data['channel'] ?? '')
                ->setUserIp($data['userIp'])
                ->setFundProcessType($data['fundProcessType'] ?? 'REAL_TIME')
                ->setAppId($data['appId'] ?? '')
                ->setUserId($data['userId'] ?? '')
                ->setScene('OFFLINE');

            $response = $this->client->prePay($request);
            $result   = $response->getResult();
            if ($result['code'] == '00000') {
                return $this->success($result);
            } else {
                return $this->error($result['returnMsg']);
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Notes: 聚合支付托管下单
     *
     * @Author: 玄尘
     * @Date: 2024/4/1 15:19
     */
    public function tutelagePrePay(array $data)
    {
        $request = new TutelagePrePayRequest();
        $request->setParentMerchantNo(config('yeepay.merchantNo'))
            ->setMerchantNo(config('yeepay.merchantNo'))
            ->setOrderId($data['orderId'] ?? '')
            ->setOrderAmount($data['orderAmount'] ?? '')
            ->setExpiredTime(now()->addMinutes(30)->toDateTimeString())
            ->setNotifyUrl($data['notifyUrl'] ?? '')
            ->setRedirectUrl($data['redirectUrl'] ?? '')
            ->setMemo($data['memo'] ?? '')
            ->setGoodsName($data['goodsName'] ?? '')
            ->setPayWay($data['payWay'] ?? 'USER_SCAN')
            ->setChannel($data['channel'] ?? '')
            ->setUserIp($data['userIp'])
            ->setToken($data['token'] ?? '')
            ->setFundProcessType($data['fundProcessType'] ?? 'REAL_TIME')
            ->setScene('OFFLINE');

        try {
            $response = $this->client->tutelagePrePay($request);
            $result   = $response->getResult();
            if ($result['code'] == '00000') {
                return $this->success($result);
            } else {
                return $this->error($result['returnMsg']);
            }
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function WechatConfig(array $data)
    {
        $url     = $this->getJson($data['url'] ?? '');
        $appId   = $this->getJson($data['appId'] ?? '');
        $request = new WechatConfigAdd0Request();
        $request->setParentMerchantNo(config('yeepay.merchantNo'))
            ->setMerchantNo(config('yeepay.merchantNo'))
            ->setTradeAuthDirList($url)
            ->setAppIdList($appId);
        $response = $this->client->wechatConfigAdd_0($request);
        $result   = $response->getResult();
        if ($result['code'] == '00000') {
            return $this->success($result);
        } else {
            return $this->error($result['returnMsg']);
        }
    }

    public function WechatConfigQuery(array $data)
    {
        $request = new WechatConfigQuery0Request();
        $request->setParentMerchantNo(config('yeepay.merchantNo'))
            ->setMerchantNo(config('yeepay.merchantNo'))
            ->setAppIdType($data['appIdType'] ?? '');
        $response = $this->client->wechatConfigQuery_0($request);
        $result   = $response->getResult();
        if ($result['code'] == '00000') {
            return $this->success($result);
        } else {
            return $this->error($result['returnMsg']);
        }
    }

}