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

use app\service\api\BaseApiService;
use Applications\sample\modules\order\repository\OrderRepository;

/**
 * Pay Service
 * @package Applications\sample\modules\pay\service\api
 */
class PayService extends BaseApiService
{
    /**
     * 发起支付
     */
    public function create(string $orderNo, string $terminal): array
    {
        $order = $this->app->make(OrderRepository::class)->findByOrderNo($orderNo);
        if (!$order) {
            return ['code' => 1, 'msg' => '订单不存在'];
        }
        if ((int)$order->status === 1) {
            return ['code' => 1, 'msg' => '订单已支付'];
        }

        // 实际生产接入微信支付 / 支付宝 SDK
        // 示例：返回模拟支付参数
        return [
            'code' => 0,
            'data' => [
                'order_no' => $orderNo,
                'amount' => (float)$order->amount,
                'terminal' => $terminal,
                'pay_params' => [
                    'timeStamp' => (string)time(),
                    'nonceStr' => bin2hex(random_bytes(16)),
                    'package' => 'prepay_id=example_' . $orderNo,
                    'signType' => 'MD5',
                ],
            ],
        ];
    }
}
