32) { throw new \RuntimeException('Paytaro 回调参数过多'); } foreach ($payload as $value) { if (!is_scalar($value) && $value !== null) { throw new \RuntimeException('Paytaro 回调参数格式无效'); } } if (array_key_exists('pid', $payload)) { return self::mapiNotification($payload, $settings, $orderPattern); } if (array_key_exists('app_id', $payload)) { return self::legacyMgateNotification($payload, $settings, $orderPattern); } throw new \RuntimeException('Paytaro 回调协议无法识别'); } private static function mapiNotification(array $payload, array $settings, $orderPattern) { if (!PaytaroSigner::verify($payload, $settings[PaytaroConfig::SECRET_KEY])) { throw new \RuntimeException('Paytaro MAPI 回调签名错误'); } if (strtoupper(trim((string) ($payload['sign_type'] ?? ''))) !== 'MD5') { throw new \RuntimeException('Paytaro MAPI 回调签名类型无效'); } if (!hash_equals((string) $settings['paytaro_app_id'], trim((string) ($payload['pid'] ?? '')))) { throw new \RuntimeException('Paytaro 回调 PID 不匹配'); } if (strtolower(trim((string) ($payload['type'] ?? ''))) !== 'alipay') { throw new \RuntimeException('Paytaro 回调支付方式不匹配'); } if (strtoupper(trim((string) ($payload['trade_status'] ?? ''))) !== 'TRADE_SUCCESS') { throw new \RuntimeException('Paytaro 回调状态不是支付成功'); } $orderNo = self::returnOrderNo($payload, $orderPattern); $tradeNo = trim((string) ($payload['trade_no'] ?? '')); if (!preg_match('/^[A-Za-z0-9_.:-]{1,80}$/', $tradeNo)) { throw new \RuntimeException('Paytaro 平台订单号格式无效'); } $money = trim((string) ($payload['money'] ?? '')); if (!preg_match('/^\d{1,10}(?:\.\d{1,2})?$/', $money)) { throw new \RuntimeException('Paytaro 回调金额格式无效'); } return self::result( $payload, $orderNo, $tradeNo, self::amountToCents($money), PaytaroSigner::canonical($payload) ); } private static function legacyMgateNotification(array $payload, array $settings, $orderPattern) { if (!PaytaroSigner::verifyLegacyMgate($payload, $settings[PaytaroConfig::SECRET_KEY])) { throw new \RuntimeException('Paytaro MGate 回调签名错误'); } if (!hash_equals((string) $settings['paytaro_app_id'], trim((string) ($payload['app_id'] ?? '')))) { throw new \RuntimeException('Paytaro 回调 App ID 不匹配'); } $method = strtolower(trim((string) ($payload['method'] ?? ''))); if ($method !== '' && $method !== 'alipay') { throw new \RuntimeException('Paytaro 回调支付方式不匹配'); } foreach (['trade_status', 'payment_status', 'status'] as $statusKey) { if (isset($payload[$statusKey]) && !in_array(strtoupper(trim((string) $payload[$statusKey])), ['1', 'PAID', 'SUCCESS', 'TRADE_SUCCESS'], true)) { throw new \RuntimeException('Paytaro 回调状态不是支付成功'); } } $orderNo = self::returnOrderNo($payload, $orderPattern); $tradeNo = trim((string) ($payload['trade_no'] ?? '')); if (!preg_match('/^[A-Za-z0-9_.:-]{1,80}$/', $tradeNo)) { throw new \RuntimeException('Paytaro 平台订单号格式无效'); } if (!isset($payload['total_amount']) || trim((string) $payload['total_amount']) === '') { throw new \RuntimeException('Paytaro MGate 回调金额缺失'); } return self::result( $payload, $orderNo, $tradeNo, self::callbackAmountToCents($payload['total_amount']), PaytaroSigner::legacyMgateCanonical($payload) ); } private static function result(array $payload, $orderNo, $tradeNo, $amountCents, $canonical) { $json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE); if (!is_string($json)) { throw new \RuntimeException('Paytaro 回调内容序列化失败'); } return [ 'order_no' => $orderNo, 'trade_no' => $tradeNo, 'amount_cents' => $amountCents, 'event_key' => hash('sha256', $canonical), 'payload' => $json, ]; } public static function returnOrderNo(array $payload, $orderPattern) { if (count($payload) > 32) { throw new \RuntimeException('Paytaro 返回参数过多'); } $orderNo = trim((string) ($payload['out_trade_no'] ?? '')); if (preg_match($orderPattern, $orderNo) !== 1) { throw new \RuntimeException('Paytaro 商户订单号格式无效'); } return $orderNo; } public static function amountToCents($amount) { $amount = PaymentAmount::normalize($amount); if (!preg_match('/^(\d+)\.(\d{2})$/', $amount, $matches)) { throw new \InvalidArgumentException('Paytaro 订单金额格式无效'); } return ltrim($matches[1] . $matches[2], '0') ?: '0'; } public static function centsToAmount($cents) { $cents = ltrim(trim((string) $cents), '0'); $cents = $cents === '' ? '0' : $cents; if (!ctype_digit($cents) || strlen($cents) > 12) { throw new \RuntimeException('Paytaro 回调金额格式无效'); } $cents = str_pad($cents, 3, '0', STR_PAD_LEFT); return PaymentAmount::normalize(substr($cents, 0, -2) . '.' . substr($cents, -2)); } private static function callbackAmountToCents($value) { $value = trim((string) $value); if (preg_match('/^\d{1,12}$/', $value)) { return ltrim($value, '0') ?: '0'; } if (preg_match('/^\d{1,10}\.\d{2}$/', $value)) { return self::amountToCents($value); } throw new \RuntimeException('Paytaro 回调金额格式无效'); } }