This commit is contained in:
Hankin 2026-04-27 09:27:17 +08:00
parent 8cdfe0c4ef
commit 1f1d2d26d5
2 changed files with 44 additions and 6 deletions

View File

@ -60,6 +60,7 @@ class WechatPayController extends Controller
Log::info("请求头", ["headers" => $headers]); Log::info("请求头", ["headers" => $headers]);
Log::info("请求体", ["body" => $body]); Log::info("请求体", ["body" => $body]);
$res = WechatPayService::mchTransferCallback($headers, $body); $res = WechatPayService::mchTransferCallback($headers, $body);
dd($res);
if (empty($res)) { if (empty($res)) {
return $this->failure("回调失败"); return $this->failure("回调失败");
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace App\Services; namespace App\Services;
use App\Http\Response\ResponseJson;
use Exception; use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use WeChatPay\Builder; use WeChatPay\Builder;
@ -10,6 +11,7 @@ use WeChatPay\Crypto\Rsa;
class WechatPayService class WechatPayService
{ {
use ResponseJson;
/** /**
* 初始化一个APIv3客户端 * 初始化一个APIv3客户端
* @return void * @return void
@ -116,13 +118,13 @@ class WechatPayService
// Log::error('微信回调验签失败', ['headers' => $headers]); // Log::error('微信回调验签失败', ['headers' => $headers]);
// return null; // 返回 500 让微信重试 // return null; // 返回 500 让微信重试
// } // }
$decryptData = $this->decryptToString($rawBody);
// 4. 解密回调数据 // 4. 解密回调数据
$decryptData = $this->decryptNotifyData($rawBody); // $decryptData = $this->decryptNotifyData($rawBody);
if (!$decryptData) { // if (!$decryptData) {
Log::error('数据解密失败'); // Log::error('数据解密失败');
return null; // return null;
} // }
return $decryptData; return $decryptData;
} }
@ -218,6 +220,41 @@ class WechatPayService
return json_decode($decrypted, true); return json_decode($decrypted, true);
} }
public function decryptToString($data)
{
try {
$resource = $data['resource'];
$nonceStr = $resource['nonce'];
$ciphertext = $resource['ciphertext'];
$associatedData = $resource['associated_data'] ?? '';
$apiV3Key = config("wechatpay.payment.api3_key");
$ciphertext = base64_decode($ciphertext);
if (strlen($ciphertext) <= 16) {
return false;
}
if (in_array('aes-256-gcm', openssl_get_cipher_methods())) {
$ctext = substr($ciphertext, 0, -16);
$authTag = substr($ciphertext, -16);
$res = openssl_decrypt(
$ctext,
'aes-256-gcm',
$apiV3Key,
OPENSSL_RAW_DATA,
$nonceStr,
$authTag,
$associatedData
);
return json_decode($res, true);
}
return null;
} catch (Exception $e) {
$this->getError($e);
return null;
}
}
/** /**
* 商家批量转账到零钱 * 商家批量转账到零钱
*/ */