<?php

namespace Leady\YeePay\Commands;

use Exception;
use Yeepay\Yop\Sdk\Service\Trade\Model\OrderQueryRequest;
use Yeepay\Yop\Sdk\Service\Trade\Model\OrderRequest;
use Yeepay\Yop\Sdk\Service\Trade\Model\RefundQueryRequest;
use Yeepay\Yop\Sdk\Service\Trade\Model\RefundRequest;
use Yeepay\Yop\Sdk\Service\Trade\TradeClientBuilder;

class Trade extends InitConfig
{

    protected $client;

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

    /**
     * Notes: 统一下单
     *
     * @Author: 玄尘
     * @Date: 2024/4/1 10:00
     * @param  array  $data
     * @return array
     */
    public function order(array $data)
    {
        try {
            $request = new OrderRequest();
            $request->setParentMerchantNo(config('yeepay.merchantNo'))
                ->setMerchantNo(config('yeepay.merchantNo'))
                ->setOrderId($data['order_id'])
                ->setOrderAmount($data['amount'])
                ->setGoodsName($data['goods_name'])
                ->setFundProcessType($data['fundProcessType'] ?? 'REAL_TIME')
                ->setExpiredTime($data['expiredTime'] ?? now()->addMinutes(30))
                ->setNotifyUrl($data['notifyUrl']);
            $respnonse = $this->client->order($request);

            $result = $respnonse->getResult();
            if ($result['code'] == 'OPR00000') {
                return $this->success($result);
            } else {
                return $this->error($result['message']);
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Notes: 查询订单
     *
     * @Author: 玄尘
     * @Date: 2024/4/1 9:57
     * @param $orderId
     * @return array
     */
    public function query($orderId)
    {
        try {
            $request = new OrderQueryRequest();
            $request->setParentMerchantNo(config('yeepay.merchantNo'))
                ->setMerchantNo(config('yeepay.merchantNo'))
                ->setOrderId($orderId);

            $respnonse = $this->client->orderQuery($request);

            $result = $respnonse->getResult();
            if ($result['code'] == 'OPR00000') {
                return $this->success($result);
            } else {
                return $this->error($result['message']);
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Notes: 退款
     *
     * @Author: 玄尘
     * @Date: 2024/4/1 10:01
     * @param  array  $data
     * @return array
     */
    public function refund(array $data)
    {
        try {
            $request = new RefundRequest();
            $request->setParentMerchantNo(config('yeepay.merchantNo'))
                ->setMerchantNo(config('yeepay.merchantNo'))
                ->setOrderId($data['order_id'])
                ->setRefundRequestId($data['refundRequestId'])
                ->setRefundAmount($data['refundAmount'])
                ->setDescription($data['description'] ?? '')
                ->setNotifyUrl($data['notifyUrl']);

            $respnonse = $this->client->refund($request);

            $result = $respnonse->getResult();
            if ($result['code'] == 'OPR00000') {
                return $this->success($result);
            } else {
                return $this->error($result['message']);
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Notes: 查询退款状态
     *
     * @Author: 玄尘
     * @Date: 2024/4/1 10:13
     */
    public function refundQuery(array $data)
    {
        try {
            $request = new RefundQueryRequest();
            $request->setParentMerchantNo(config('yeepay.merchantNo'))
                ->setMerchantNo(config('yeepay.merchantNo'))
                ->setOrderId($data['order_id'])
                ->setRefundRequestId($data['refundRequestId']);

            $respnonse = $this->client->refundQuery($request);

            $result = $respnonse->getResult();
            if ($result['code'] == 'OPR00000') {
                return $this->success($result);
            } else {
                return $this->error($result['message']);
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}