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

use think\Request;


/**
 * Balance Gateway 支付网关类
 * @package app\service\domain\pay\gateway
 */
class BalanceGateway extends BaseGateway
{
    /**
     * 返回支付网关标识
     * @return string
     */
    public function provider(): string
    {
        return 'balance';
    }

     /**
     * 支付网关名称
     * @return string
     */

    public function name(): string
    {
        return '';
    }


     /**
     * 支付网关是否支持指定终端
     * @param string $terminal 终端
     * @return bool
     */

    public function supportsTerminal(string $terminal): bool
    {
        return true;
    }

     /**
     * 支付网关是否可分配
     * @return bool
     */

    public function isAllocatable(): bool
    {
        return true;
    }

     /**
     * 支付网关成功响应
     * @return array
     */

    public function successResponse(): array
    {
        return ['body' => '', 'headers' => []];
    }


     /**
     * 支付网关创建支付订单
     * @param array $payment 支付订单
     * @param array $order 订单信息
     * @param string $terminal 终端
     * @param array $context 上下文信息
     * @return array
     */

    public function create(array $payment, array $order, string $terminal, array $context): array
    {
        return [
            'action' => 'balance',
            'message' => '',
            'params' => [
                'payment_no' => (string)($payment['payment_no'] ?? ''),
                'amount' => $payment['amount'] ?? 0,
            ],
        ];
    }


     /**
     * 支付网关通知处理
     * @param Request $request 请求对象
     * @param string $terminal 终端
     * @return array
     */

    public function notify(Request $request, string $terminal): array
    {
        return ['code' => 0, 'payment_no' => '', 'trade_no' => '', 'amount' => '0', 'raw' => []];
    }


     /**
     * 支付网关退款处理
     * @param array $refund 退款订单
     * @param array $payment 支付订单
     * @return array
     */

    public function refund(array $refund, array $payment): array
    {
        if ($this->isTestMode()) {
            return $this->testModeRefundResponse($refund, $payment);
        }

        return [
            'code' => 0,
            'msg' => '',
            'status' => 2,
            'remark' => '',
            'extra' => [
                'refund_no' => (string)($refund['refund_no'] ?? ''),
                'payment_no' => (string)($payment['payment_no'] ?? ''),
                'amount' => $refund['amount'] ?? 0,
            ],
        ];
    }

     /**
     * 支付网关退款通知处理
     * @param Request $request 请求对象
     * @param string $terminal 终端
     * @return array
     */

    public function refundNotify(Request $request, string $terminal): array
    {
        return ['code' => 0, 'refund_no' => '', 'status' => 'SUCCESS', 'raw' => []];
    }
}