<?php

namespace App\Modules\Trade\Pay;

use App\Libs\Pay\PayFactory;
use App\Modules\Trade\Services\PayMerchantService;
use Exception;
use Illuminate\Support\Facades\Log;

class AliOrderArousePay extends OrderArousePayAbstract
{
    public function arouse(array $data)
    {
        $pay_merchant_id = (int)$data['pay_merchant_id'];

        // 订单描述，针对聚裕的包无法支付的问题（注册时传入了仙女阅读的包名，导致支付时取的支付信息不一致）
        $package = getProp($data, 'package');
        if ($package && $package === 'com.juyu.kuaiying.rmyq') {
            $body = $this->getSubjectNameByPackage($package);
        } else {
            $body = $this->getSubjectName($data['distribution_channel_id']);
        }

        $ali_param = [
            'body'            => '小说阅读',
            'subject'         => $body,
            'out_trade_no'    => $data['trade_no'],
            'timeout_express' => '90m',
            'total_amount'    => $data['price'] / 100,
            'product_code'    => 'QUICK_WAP_WAY',
            'return_url'      => '',
            'type'            => $data['type'],
            'pay_merchant_id' => $pay_merchant_id,
        ];
        myLog('aliPay')->info('arouse', compact('data', 'ali_param'));
        try {
            $config = PayMerchantService::findAliPayConfig($pay_merchant_id);
            $pay    = PayFactory::aliPay($config);
            $result = $pay->send($ali_param);
            myLog('aliPay')->info('result', compact('result'));
            return $result;
        } catch (Exception $e) {
            Log::error("创建支付宝订单失败: " . $e->getTraceAsString());
        }
    }
}
