<?php
 namespace App\Util; use App\Jobs\WechatUserPayJob; use App\Types\UserOauthType; use App\Types\UserPayBiz; use App\Types\UserPayJobPayType; use App\Types\UserPayJobStatus; use App\Types\UserPayOauthType; use EdwinFound\Utils\RandomUtil; use Illuminate\Support\Facades\Log; use TechOnline\Laravel\Http\Response; use TechSoft\Laravel\Config\ConfigUtil; use TechSoft\Laravel\MemberMoney\MemberMoneyUtil; use TechSoft\Laravel\Oauth\OauthType; use TechSoft\Laravel\Wechat\Types\WechatAuthType; use TechSoft\Laravel\Wechat\WechatUtil; class PlatformUtil { public static function getWechatApp($memberUserId) { return WechatUtil::app(0, [ 'enable' => true, 'appId' => ConfigUtil::getWithEnv('oauthWechatMobileAppId', ''), 'appId' => ConfigUtil::getWithEnv('oauthWechatMobileAppId', ''), 'appSecret' => ConfigUtil::getWithEnv('oauthWechatMobileAppSecret', ''), 'appToken' => '', 'appEncodingKey' => '', 'authType' => WechatAuthType::CONFIG, ], ['payment' => false,]); } public static function getWechatPaymentApp($memberUserId) { if (!ConfigUtil::get('payWechatMobileOn', false)) { return null; } return WechatUtil::app(0, [ 'enable' => true, 'appId' => ConfigUtil::getWithEnv('payWechatMobileAppId', ''), 'appSecret' => ConfigUtil::getWithEnv('payWechatMobileAppSecret', ''), 'appToken' => '', 'appEncodingKey' => '', 'authType' => WechatAuthType::CONFIG, ], ['payment' => [ 'merchantId' => ConfigUtil::getWithEnv('payWechatMobileMerchantId'), 'key' => ConfigUtil::getWithEnv('payWechatMobileKey'), 'dataCert' => ConfigUtil::getWithEnv('payWechatMobileFileCert'), 'dataKey' => ConfigUtil::getWithEnv('payWechatMobileFileKey'), ]]); } public static function remitMoneyToUser($memberUserId, $userId, $change, $remark) { MemberMoneyUtil::change($userId, $change, $remark); } public static function userPayOpenId($userId) { $memberOauth = ModelHelper::load('member_oauth', [ 'memberUserId' => $userId, 'type' => OauthType::WECHAT_MOBILE, ]); if (empty($memberOauth)) { return null; } return $memberOauth['openId']; } public static function cashMoneyToUser($memberUserId, $userId, $money) { if ($money < 1.00) { return \Edwin404\Base\Support\Response::generate(-1, '最小金额为1元'); } $app = self::getWechatPaymentApp($memberUserId); if (empty($app)) { return Response::generate(-1, "微信支付未开启"); } MemberMoneyUtil::change($userId, -$money, '用户提现'); $merchantPay = $app->merchant_pay; $user = ModelHelper::load('member_user', ['id' => $userId]); $openId = self::userPayOpenId($userId); if (empty($openId)) { return Response::generate(-1, "用户OpenId为空"); } $merchantPayData = [ 'partner_trade_no' => 'mzBigWheelT' . time() . '' . RandomUtil::number(4), 'openid' => $openId, 'check_name' => 'NO_CHECK', 're_user_name' => $user['username'], 'amount' => intval(bcmul($money, 100, 2)), 'desc' => '用户提现', 'spbill_create_ip' => '127.0.0.1', ]; try { $result = $merchantPay->send($merchantPayData)->toArray(); if ( isset($result['return_code']) && $result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == 'SUCCESS' ) { return Response::generate(0, null); } else { return Response::generate(-1, '提现失败:' . json_encode($result, JSON_UNESCAPED_UNICODE)); } } catch (\Exception $e) { Log::error('支付失败:' . $e->getMessage()); Log::error('支付失败:' . $e->getTraceAsString()); return Response::generate(0, null); } } }