love_php/app/Services/LiveAlipayService.php
2026-04-02 09:20:51 +08:00

355 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services;
use App\Jobs\SendTemplateMsg;
use App\Models\Live\Viewer;
use App\Models\Live\Asset;
use App\Models\Live\AssetLog;
use Illuminate\Http\Request;
use App\library\alipay\AopClient;
use App\library\alipay\AopCertClient;
use App\library\alipay\request\AlipayFundTransUniTransferRequest;
use App\library\alipay\request\AlipayFundTransCommonQueryRequest;
use App\Http\Response\ResponseJson;
class LiveAlipayService
{
use ResponseJson;
protected $aop;
public function __construct()
{
$aop = new AopCertClient();
//应用证书路径
$appCertPath = storage_path('alipay/appCertPublicKey_2021001147602775_1.crt');
//支付宝公钥路径
$alipayCertPath = storage_path('alipay/alipayCertPublicKey_RSA2_1.crt');
//支付宝根证书路径
$rootCertPath = storage_path('alipay/alipayRootCert_1.crt');
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = env('ALIPAY_APPID');
$aop->rsaPrivateKey = env('ALIPAY_APP_SECRET');
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='utf-8';
$aop->format='json';
$aop->isCheckAlipayPublicCert = true;
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
// $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
$aop->alipayRootCertSN = '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';
$this->aop = $aop;
// dd($aop);
// $aop = new AopCertClient();
// $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
// $aop->appId = '2021001162609611';
// $aop->rsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBA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phmfsURB7hqLr6LbkQgvceIJaXXJ3bHBRIe7Ww4BuiCJNubGiNaSEpDgAp6cm6aZaoN/hpliR3gL3TZwz5Zj+py/CI6EhCC8V2MfglLplRaY4En5XthqhejDIFh8+gFh0wvl6fbVQKBgGG9I95l9Lu3oC3rnrPyWgjNRm+r6e/s8QxuEo7Y7Ue2DTJ0pwORBGvPiAELUlbXCFqxFKcw2xz29R0Rq19Glm1NYaEwfTCbi7mmCo6PZebbDt5GBNw5X6m9IYT5qoSYYdPKTxZPb63EhqWsxEcEqK7cBSaN/z5vgKIuLA7IY9YBAoGAOXCCK6bexRUF5LST3lqBOIBwB+/51yW0D17lJYtR5gLVsyEe6GWl9DPbeB1BZujUNQ+01wc59DUYQyYiekYb7399mdRyMJ/0bEADaPU3AlGAQQpoQovmz+aKdxKHnMsm7Zdy3I2v6oDcfwDYpTs+vInLY51iyj1+Vh+3+IIQ21M=';
// $aop->alipayrsaPublickey = $aop->getPublicKey(app_path().'/library/alipay/alipayCertPublicKey.crt');
// $aop->isCheckAlipayPublicCert = true;
// $aop->appCertSN = $aop->getCertSN(app_path().'/library/alipay/appCertPublicKey_2021001162609611.crt');
//// $aop->alipayRootCertSN = $aop->getRootCertSN(app_path().'/library/alipay/alipayRootCert.crt');
// $aop->alipayRootCertSN = '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';
// $aop->apiVersion = '1.0';
// $aop->signType = 'RSA2';
// $aop->postCharset='UTF-8';
// $aop->format='json';
//
// $this->aop = $aop;
}
public function transfer($data){
$aop = $this->aop;
$request = new AlipayFundTransUniTransferRequest ();
$request->setBizContent("{" .
"\"out_biz_no\":\"".$data['out_biz_no']."\"," .
"\"trans_amount\":".$data['amount']."," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"biz_scene\":\"DIRECT_TRANSFER\"," .
"\"mutiple_currency_detail\":{" .
"\"payment_amount\":\"100.00\"," .
"\"payment_currency\":\"CNY\"," .
"\"trans_currency\":\"CNY\"," .
"\"settlement_amount\":\"10.00\"," .
"\"settlement_currency\":\"CNY\"," .
"\"ext_info\":\"key=value\"" .
" }," .
"\"order_title\":\"直播提现\"," .
// "\"original_order_id\":\"20190620110075000006640000063056\"," .
// "\"payer_info\":{" .
// "\"identity\":\"info@ufutx.com\"," .
// "\"identity_type\":\"ALIPAY_LOGIN_ID\"," .
// "\"name\":\"福恋智能\"," .
// " }," .
"\"payee_info\":{" .
"\"identity\":\"".$data['payee_account']."\"," .
"\"identity_type\":\"ALIPAY_LOGON_ID\"," .
"\"name\":\"".$data['payee_real_name']."\"" .
" }," .
"\"remark\":\"直播余额提现到支付宝\"," .
"\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"," .
"\"passback_params\":\"{\\\"merchantBizType\\\":\\\"peerPay\\\"}\"" .
" }");
$result = $aop->execute( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
$log = AssetLog::where('out_biz_no', $data['out_biz_no'])->first();
if(!empty($resultCode)&&$resultCode == 10000){
$log->cash_status = 2;
$log->save();
//模板消息-提现失败通知
$log = AssetLog::select('num', 'viewer_id', 'account')->where('out_biz_no', $data['out_biz_no'])->first();
$viewer = Viewer::select('openid', 'alipay_account', 'nickname')->where('id', $log->viewer_id)->first();
$account_type = $log->account == 2 ? '支付宝账户'.$viewer->alipay_account : '微信'.$viewer->nickname;
$data['touser'] = $viewer->openid;
$data['template_id'] = 'aV4ic7jr5bOlf55CgR0jmMsFYyhdRAiVmqqXEjnUqQU';
$data['url'] = env('APP_URL').'/h5/#/myWallet';
$data['data'] = [
'first' => '您好,您的提现已到账',
'keyword1' => $log->num,
'keyword2' => date('Y-m-d H:i:s'),
'keyword3' => '提现到'.$account_type,
'reamrk' => '感谢您的使用',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
return true;
} else {
$reason = '';
switch ($result->$responseNode->sub_code) {
case 'PAYEE_NOT_EXIST':
$reason = '支付宝账户或者真实姓名有误';
break;
case 'INVALID_PARAMETER':
$reason = '缺少支付宝或者真实姓名';
break;
default :
$reason = '系统错误';
}
$log->reason = $reason;
$log->score = $log->score + $log->num;
$log->cash_status = 3;
$log->save();
$asset = Asset::where('viewer_id', $log->viewer_id)->first();
$asset->increment('cash', $log->num);
//模板消息-提现失败通知
$log = AssetLog::select('num', 'viewer_id', 'account')->where('out_biz_no', $data['out_biz_no'])->first();
$viewer = Viewer::select('openid', 'alipay_account', 'nickname')->where('id', $log->viewer_id)->first();
$account_type = $log->account == 2 ? '支付宝账户'.$viewer->alipay_account : '微信'.$viewer->nickname;
$data['touser'] = $viewer->openid;
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = env('APP_URL').'/h5/#/myWallet';
$data['data'] = [
'first' => '您好,您的提现已到账',
'keyword1' => $log->num,
'keyword2' => '提现到'.$account_type,
'keyword3' => $reason,
'reamrk' => '请稍后重试',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
return false;
}
}
public function transferAccount($data)
{
try {
$aop = $this->aop;
$request = new AlipayFundTransUniTransferRequest ();
$request->setBizContent("{" .
"\"out_biz_no\":\"".$data['out_biz_no']."\"," .
"\"trans_amount\":".$data['amount']."," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"biz_scene\":\"DIRECT_TRANSFER\"," .
"\"mutiple_currency_detail\":{" .
"\"payment_amount\":\"100.00\"," .
"\"payment_currency\":\"CNY\"," .
"\"trans_currency\":\"CNY\"," .
"\"settlement_amount\":\"10.00\"," .
"\"settlement_currency\":\"CNY\"," .
"\"ext_info\":\"key=value\"" .
" }," .
"\"order_title\":\"直播提现\"," .
// "\"original_order_id\":\"20190620110075000006640000063056\"," .
// "\"payer_info\":{" .
// "\"identity\":\"info@ufutx.com\"," .
// "\"identity_type\":\"ALIPAY_LOGIN_ID\"," .
// "\"name\":\"福恋智能\"," .
// " }," .
"\"payee_info\":{" .
"\"identity\":\"".$data['payee_account']."\"," .
"\"identity_type\":\"ALIPAY_LOGON_ID\"," .
"\"name\":\"".$data['payee_real_name']."\"" .
" }," .
"\"remark\":\"".$data['remark']."\"," .
"\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"," .
"\"passback_params\":\"{\\\"merchantBizType\\\":\\\"peerPay\\\"}\"" .
" }");
if (env('APP_ENV') != 'production') {
return true;
}
$result = $aop->execute( $request);
$result = $result->alipay_fund_trans_uni_transfer_response;
if(!empty($result)&&$result->code == 10000){
return true;
}else{
return ['code'=>1, 'msg'=>$result->sub_msg];
}
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
//查询订单
public function info($data){
$aop = $this->aop;
$request = new AlipayFundTransCommonQueryRequest ();
$request->setBizContent("{" .
"\"product_code\":\"STD_RED_PACKET\"," .
"\"biz_scene\":\"PERSONAL_PAY\"," .
"\"out_biz_no\":\"".$data['out_biz_no']."\"," .
"\"order_id\":\"".$data['order_id']."\"," .
"\"pay_fund_order_id\":\"".$data['pay_fund_order_id']."\"" .
" }");
$result = $aop->execute ( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
return $result;
} else {
return $result;
}
}
//商家版支付宝提现
public function platTransferAccount($data)
{
try {
$aop = $this->aop;
$request = new AlipayFundTransUniTransferRequest ();
$request->setBizContent("{" .
"\"out_biz_no\":\"".$data['out_biz_no']."\"," .
"\"trans_amount\":".$data['amount']."," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"biz_scene\":\"DIRECT_TRANSFER\"," .
"\"mutiple_currency_detail\":{" .
"\"payment_amount\":\"100.00\"," .
"\"payment_currency\":\"CNY\"," .
"\"trans_currency\":\"CNY\"," .
"\"settlement_amount\":\"10.00\"," .
"\"settlement_currency\":\"CNY\"," .
"\"ext_info\":\"key=value\"" .
" }," .
"\"order_title\":\"商家版现金提现\"," .
// "\"original_order_id\":\"20190620110075000006640000063056\"," .
// "\"payer_info\":{" .
// "\"identity\":\"info@ufutx.com\"," .
// "\"identity_type\":\"ALIPAY_LOGIN_ID\"," .
// "\"name\":\"福恋智能\"," .
// " }," .
"\"payee_info\":{" .
"\"identity\":\"".$data['payee_account']."\"," .
"\"identity_type\":\"ALIPAY_LOGON_ID\"," .
"\"name\":\"".$data['payee_real_name']."\"" .
" }," .
"\"remark\":\"".$data['remark']."\"," .
"\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"," .
"\"passback_params\":\"{\\\"merchantBizType\\\":\\\"peerPay\\\"}\"" .
" }");
// if (env('APP_ENV') != 'production') {
// return true;
// }
$result = $aop->execute( $request);
$result = $result->alipay_fund_trans_uni_transfer_response;
if(!empty($result)&&$result->code == 10000){
return true;
}else{
return ['code'=>1, 'msg'=>$result->sub_msg, 'aliapy_code'=>$result->code];
}
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
// 商家版支付宝提现-用户id
public function UserTransferAccount($data)
{
try {
$aop = $this->aop;
$request = new AlipayFundTransUniTransferRequest ();
$request->setBizContent("{" .
"\"out_biz_no\":\"".$data['out_biz_no']."\"," .
"\"trans_amount\":".$data['amount']."," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"biz_scene\":\"DIRECT_TRANSFER\"," .
"\"mutiple_currency_detail\":{" .
"\"payment_amount\":\"100.00\"," .
"\"payment_currency\":\"CNY\"," .
"\"trans_currency\":\"CNY\"," .
"\"settlement_amount\":\"10.00\"," .
"\"settlement_currency\":\"CNY\"," .
"\"ext_info\":\"key=value\"" .
" }," .
"\"order_title\":\"商家版现金提现\"," .
"\"payee_info\":{" .
"\"identity\":\"".$data['payee_account']."\"," .
"\"identity_type\":\"ALIPAY_USER_ID\"," .
" }," .
"\"remark\":\"".$data['remark']."\"," .
"\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"," .
"\"passback_params\":\"{\\\"merchantBizType\\\":\\\"peerPay\\\"}\"" .
" }");
// if (env('APP_ENV') != 'production') {
// return true;
// }
$result = $aop->execute( $request);
$result = $result->alipay_fund_trans_uni_transfer_response;
if(!empty($result)&&$result->code == 10000){
return true;
}else{
return ['code'=>1, 'msg'=>$result->sub_msg];
}
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
// 获取阿里云用户信息
public function AlipayUserInfo($auth_code)
{
$c = $this->aop;
$code = $auth_code;
$request = new \AlipaySystemOauthTokenRequest();
$request->setGrantType("authorization_code");
$request->setCode($code);
$result = $c->execute($request);
if(!isset($result->alipay_system_oauth_token_response)){
return false;
}
$accessToken = $result->alipay_system_oauth_token_response->access_token;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称alipay.user.userinfo.share
$request= new \AlipayUserInfoShareRequest();
//授权类接口执行API调用时需要带上accessToken
$result= $c->execute($request,$accessToken);
$user = $result->alipay_user_info_share_response;
return $user;
}
}