<?php

use Kylin\Payment\Facades\Payment;

/**
 * 支付示例
 */
class PaymentExample
{
    /**
     * 微信支付示例
     */
    public function wechatPay()
    {
        $params = [
            'body' => '测试商品',
            'out_trade_no' => 'WECHAT_' . date('YmdHis') . rand(1000, 9999),
            'total_fee' => 1,
            'trade_type' => 'JSAPI',
            'openid' => '用户的openid',
        ];

        // 发起支付
        $result = Payment::wechat()->pay($params);
        
        return $result;
    }

    /**
     * 支付宝支付示例
     */
    public function alipayPay()
    {
        $params = [
            'subject' => '测试商品',
            'out_trade_no' => 'ALIPAY_' . date('YmdHis') . rand(1000, 9999),
            'total_amount' => 0.01,
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
            'method' => 'alipay.trade.page.pay',
        ];

        // 发起支付
        $result = Payment::alipay()->pay($params);
        
        return $result;
    }

    /**
     * 银联支付示例
     */
    public function unionpayPay()
    {
        $params = [
            'out_trade_no' => 'UNIONPAY_' . date('YmdHis') . rand(1000, 9999),
            'total_amt' => 100,
            'goods_name' => '测试商品',
        ];

        // 发起支付
        $result = Payment::unionpay()->pay($params);
        
        return $result;
    }

    /**
     * 抖音支付示例
     */
    public function douyinPay()
    {
        $params = [
            'subject' => '测试商品',
            'out_trade_no' => 'DOUYIN_' . date('YmdHis') . rand(1000, 9999),
            'total_amount' => 1,
            'currency' => 'CNY',
        ];

        // 发起支付
        $result = Payment::douyin()->pay($params);
        
        return $result;
    }

    /**
     * 江苏银行支付示例
     */
    public function jsbankPay()
    {
        $params = [
            'out_trade_no' => 'JSBANK_' . date('YmdHis') . rand(1000, 9999),
            'amount' => 1.00,
            'goods_name' => '测试商品',
        ];

        // 发起支付
        $result = Payment::jsbank()->pay($params);
        
        return $result;
    }

    /**
     * 支付宝国际版支付示例
     */
    public function alipayGlobalPay()
    {
        $params = [
            'subject' => 'Test Product',
            'out_trade_no' => 'ALIPAY_GLOBAL_' . date('YmdHis') . rand(1000, 9999),
            'total_amount' => 1,
            'currency' => 'USD',
        ];

        // 发起支付
        $result = Payment::alipayGlobal()->pay($params);
        
        return $result;
    }

    /**
     * PayPal支付示例
     */
    public function paypalPay()
    {
        $params = [
            'total_amount' => 1,
            'currency' => 'USD',
            'description' => 'Test Product',
            'brand_name' => 'Test Store',
        ];

        // 发起支付
        $result = Payment::paypal()->pay($params);
        
        return $result;
    }

    /**
     * Stripe支付示例
     */
    public function stripePay()
    {
        $params = [
            'amount' => 100,
            'currency' => 'cny',
            'description' => 'Test Product',
            'source' => 'tok_visa',
        ];

        // 发起支付
        $result = Payment::stripe()->pay($params);
        
        return $result;
    }

    /**
     * Google Pay配置示例
     */
    public function googlePayConfig()
    {
        $params = [
            'total_price' => '1.00',
            'currency_code' => 'CNY',
            'country_code' => 'CN',
        ];

        // 获取Google Pay配置
        $result = Payment::googlePay()->pay($params);
        
        return $result;
    }

    /**
     * Apple Pay配置示例
     */
    public function applePayConfig()
    {
        $params = [
            'total_amount' => '1.00',
            'currency_code' => 'CNY',
            'country_code' => 'CN',
            'display_name' => 'Test Store',
        ];

        // 获取Apple Pay配置
        $result = Payment::applePay()->pay($params);
        
        return $result;
    }

    /**
     * 统一支付示例
     */
    public function unifiedPay()
    {
        // 微信支付
        $wechatParams = [
            'body' => '测试商品',
            'out_trade_no' => 'WECHAT_' . date('YmdHis') . rand(1000, 9999),
            'total_fee' => 1,
            'trade_type' => 'JSAPI',
            'openid' => '用户的openid',
        ];
        $wechatResult = Payment::pay('wechat', $wechatParams);
        
        // 支付宝支付
        $alipayParams = [
            'subject' => '测试商品',
            'out_trade_no' => 'ALIPAY_' . date('YmdHis') . rand(1000, 9999),
            'total_amount' => 0.01,
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
        ];
        $alipayResult = Payment::pay('alipay', $alipayParams);
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }

    /**
     * 订单查询示例
     */
    public function queryOrder()
    {
        // 微信支付查询
        $wechatResult = Payment::query('wechat', 'WECHAT_202602251200000000');
        
        // 支付宝支付查询
        $alipayResult = Payment::query('alipay', 'ALIPAY_202602251200000000');
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }

    /**
     * 退款示例
     */
    public function refundOrder()
    {
        // 微信支付退款
        $wechatParams = [
            'out_trade_no' => 'WECHAT_202602251200000000',
            'out_refund_no' => 'REFUND_' . date('YmdHis') . rand(1000, 9999),
            'total_fee' => 1,
            'refund_fee' => 1,
        ];
        $wechatResult = Payment::refund('wechat', $wechatParams);
        
        // 支付宝支付退款
        $alipayParams = [
            'out_trade_no' => 'ALIPAY_202602251200000000',
            'out_request_no' => 'REFUND_' . date('YmdHis') . rand(1000, 9999),
            'refund_amount' => 0.01,
        ];
        $alipayResult = Payment::refund('alipay', $alipayParams);
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }

    /**
     * 关闭订单示例
     */
    public function closeOrder()
    {
        // 微信支付关闭
        $wechatResult = Payment::close('wechat', 'WECHAT_202602251200000000');
        
        // 支付宝支付关闭
        $alipayResult = Payment::close('alipay', 'ALIPAY_202602251200000000');
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }

    /**
     * 回调处理示例
     */
    public function handleCallback()
    {
        // 微信支付回调
        $wechatCallbackData = [
            'return_code' => 'SUCCESS',
            'result_code' => 'SUCCESS',
            'out_trade_no' => 'WECHAT_202602251200000000',
            'transaction_id' => '4200000000000000000000000000',
            'total_fee' => 1,
            'time_end' => '20260225120000',
            'sign' => 'test_sign'
        ];
        $wechatResult = Payment::callback('wechat', $wechatCallbackData);
        
        // 支付宝支付回调
        $alipayCallbackData = [
            'out_trade_no' => 'ALIPAY_202602251200000000',
            'trade_no' => '202602252200140000000000',
            'total_amount' => '0.01',
            'trade_status' => 'TRADE_SUCCESS',
            'sign' => 'test_sign'
        ];
        $alipayResult = Payment::callback('alipay', $alipayCallbackData);
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }

    /**
     * 沙盒模式示例
     */
    public function sandboxMode()
    {
        // 微信支付沙盒模式
        $wechatGateway = Payment::wechat();
        $wechatGateway->setSandbox(true);
        
        $wechatParams = [
            'body' => '测试商品',
            'out_trade_no' => 'WECHAT_SANDBOX_' . date('YmdHis') . rand(1000, 9999),
            'total_fee' => 1,
            'trade_type' => 'JSAPI',
            'openid' => '用户的openid',
        ];
        $wechatResult = $wechatGateway->pay($wechatParams);
        
        // 支付宝支付沙盒模式
        $alipayGateway = Payment::alipay();
        $alipayGateway->setSandbox(true);
        
        $alipayParams = [
            'subject' => '测试商品',
            'out_trade_no' => 'ALIPAY_SANDBOX_' . date('YmdHis') . rand(1000, 9999),
            'total_amount' => 0.01,
        ];
        $alipayResult = $alipayGateway->pay($alipayParams);
        
        return [
            'wechat' => $wechatResult,
            'alipay' => $alipayResult
        ];
    }
}
