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


/**
 * 基础支付网关类
 * @package app\service\domain\pay\gateway
 */
abstract class BaseGateway implements PaymentGatewayInterface
{
     /**
     * 将元转换为分
     * @param float $yuan 元
     * @return int
     */

    protected function toFen($amount): int
    {
        return (int)(round((float)$amount * 100, 0));
    }


     /**
     * 将分转换为元
     * @param int $fen 分
     * @return string
     */

    protected function toYuan(int $fen): string
    {
        return number_format($fen / 100, 2, '.', '');
    }


     /**
     * 规范终端
     * @param string $terminal 终端
     * @return string
     */

    protected function normalizeTerminal(string $terminal): string
    {
        $map = [
            'miniapp' => 'weapp',
            'h5' => 'wechat',
            'wxapp' => 'weapp',
        ];
        return $map[$terminal] ?? $terminal;
    }


     /**
     * 终端文本
     * @param string $terminal 终端
     * @return string
     */

    protected function terminalText(string $terminal): string
    {
        $map = [
            'weapp' => '',
            'wechat' => '',
            'pc' => '',
            'app' => 'APP',
            'h5' => 'H5',
        ];
        return $map[$terminal] ?? $terminal;
    }


     /**
     * 构建订单主题
     * @param array $payment 支付订单
     * @param array $order 订单
     * @return string
     */

    protected function buildSubject(array $payment, array $order): string
    {
        return (string)($order['order_no'] ?? $payment['payment_no'] ?? '');
    }


     /**
     * 是否为测试模式
     * @return bool
     */

    protected function isTestMode(): bool
    {
        return app()->isDebug();
    }


     /**
     * 测试模式创建响应
     * @param array $payment 支付订单
     * @param string $terminal 终端
     * @return array
     */

    protected function testModeCreateResponse(array $payment, string $terminal): array
    {
        $name = $this->name();
        $tt = $this->terminalText($terminal);
        return [
            'action' => 'test_mock',
            'message' => '' . $name . $tt . '',
            'params' => [
                'out_trade_no' => (string)($payment['payment_no'] ?? ''),
                'amount' => $payment['amount'] ?? 0,
                'terminal' => $terminal,
                '_test' => true,
            ],
        ];
    }


     /**
     * 测试模式退款响应
     * @param array $refund 退款订单
     * @param array $payment 支付订单
     * @return array
     */

    protected function testModeRefundResponse(array $refund, array $payment): array
    {
        $name = $this->name();
        return [
            'code' => 0,
            'msg' => '' . $name . '',
            'status' => 2,
            'remark' => '' . $name . '',
            'extra' => [
                'refund_no' => (string)($refund['refund_no'] ?? ''),
                'payment_no' => (string)($payment['payment_no'] ?? ''),
                'amount' => $refund['amount'] ?? 0,
                '_test' => true,
            ],
        ];
    }
}