<?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 WxOrderArousePay extends OrderArousePayAbstract
{
    public function arouse(array $data)
    {
        $pay_merchant_id = (int)$data['pay_merchant_id'];
        $config          = PayMerchantService::findPayConfig($pay_merchant_id);
        $pay             = PayFactory::official($config);

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

        try {
            $payOrder = [
                'trade_type' => $data['trade_type'], // 交易类型
                'trade_no'   => $data['trade_no'], // 订单号
                'price'      => $data['price'], // 订单金额，单位：分
                'body'       => $body, // 订单描述
                'create_ip'  => _getIp(), // 支付人的 IP
                'remark'     => $pay_merchant_id
            ];
            $result   = $pay->send($payOrder);
            myLog('wxPay')->info('arouse', compact('data', 'payOrder', 'result'));
            if ($result) {
                return $result;
            } else {
                Log::error("创建微信订单失败: " . json_encode($result));
                return false;
            }
        } catch (Exception $e) {
            Log::error("创建微信订单失败: " . $e->getMessage());
            return false;
        }
    }
}
