|
|
@@ -411,6 +411,7 @@
|
|
|
<h3>其他</h3>
|
|
|
<ul>
|
|
|
<li @click="activeSection = 'errors'" :class="{ active: activeSection === 'errors' }">常见错误</li>
|
|
|
+ <li @click="activeSection = 'php'" :class="{ active: activeSection === 'php' }">PHP示例代码</li>
|
|
|
</ul>
|
|
|
</div>
|
|
|
</el-scrollbar>
|
|
|
@@ -912,6 +913,90 @@ Content-Disposition: form-data; name="content"
|
|
|
<li><strong>500 Internal Server Error</strong>:服务器内部错误</li>
|
|
|
</ul>
|
|
|
</div>
|
|
|
+
|
|
|
+ <div v-else-if="activeSection === 'php'" class="section-content">
|
|
|
+ <h2>8. PHP 示例代码</h2>
|
|
|
+ <h3>8.1 以下是签名生成的 PHP 示例代码:</h3>
|
|
|
+ <pre class="code-block"><code class="language-php"><?php
|
|
|
+
|
|
|
+class SignatureGenerator
|
|
|
+{
|
|
|
+ private static function ksortRecursive(&$array) {
|
|
|
+ if (!is_array($array)) return;
|
|
|
+ ksort($array, SORT_STRING);
|
|
|
+ foreach ($array as &$value) {
|
|
|
+ self::ksortRecursive($value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function generateSignature(
|
|
|
+ string $apiSecret,
|
|
|
+ array $requestData,
|
|
|
+ array $excludeParams = ['sign']
|
|
|
+ ): string {
|
|
|
+ $filteredData = [];
|
|
|
+ foreach ($requestData as $key => $value) {
|
|
|
+ if (in_array($key, $excludeParams, true)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($value === null || $value === '') {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (is_array($value) && empty($value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $filteredData[$key] = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ self::ksortRecursive($filteredData);
|
|
|
+
|
|
|
+ $collect = [];
|
|
|
+ foreach ($filteredData as $key => $value) {
|
|
|
+ if (is_array($value)) {
|
|
|
+ $value = json_encode($value, JSON_UNESCAPED_SLASHES);
|
|
|
+ }
|
|
|
+ $encodedValue = rawurlencode((string)$value);
|
|
|
+ $collect[] = "{$key}={$encodedValue}";
|
|
|
+ }
|
|
|
+
|
|
|
+ $signStr = implode('&', $collect);
|
|
|
+ return hash_hmac('sha256', $signStr, $apiSecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function verifySignature(
|
|
|
+ string $apiSecret,
|
|
|
+ array $requestData,
|
|
|
+ string $signature
|
|
|
+ ): bool {
|
|
|
+ $expectedSignature = self::generateSignature($apiSecret, $requestData);
|
|
|
+ return hash_equals($expectedSignature, $signature);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// ================= 测试调用 =================
|
|
|
+
|
|
|
+$apiSecret = 'your_api_secret_here';
|
|
|
+$requestData = [
|
|
|
+ "account_book_id" => "2088480770900000",
|
|
|
+ "amount" => "1.00",
|
|
|
+ "order_title" => "Apikey转账",
|
|
|
+ "third_biz_no" => "1234242026042700111",
|
|
|
+ "payee_info" => [
|
|
|
+ "identity_type" => "ALIPAY_ACCOUNT",
|
|
|
+ "name" => "钱先生",
|
|
|
+ "identity" => "1xx9xx9xxxxx"
|
|
|
+ ]
|
|
|
+];
|
|
|
+
|
|
|
+// 生成签名
|
|
|
+$signature = SignatureGenerator::generateSignature($apiSecret, $requestData);
|
|
|
+echo "生成的签名: {$signature}\n";
|
|
|
+
|
|
|
+// 验证签名
|
|
|
+$isValid = SignatureGenerator::verifySignature($apiSecret, $requestData, $signature);
|
|
|
+echo "签名验证结果: " . ($isValid ? '有效' : '无效') . "\n";
|
|
|
+?></code></pre>
|
|
|
+ </div>
|
|
|
</el-card>
|
|
|
</div>
|
|
|
</div>
|
|
|
@@ -1045,7 +1130,8 @@ function getSectionTitle() {
|
|
|
transfer: "发起转账",
|
|
|
transfer_query: "查询转账",
|
|
|
callback: "回调通知",
|
|
|
- errors: "常见错误"
|
|
|
+ errors: "常见错误",
|
|
|
+ php: "PHP示例代码"
|
|
|
};
|
|
|
return titles[activeSection.value] || "API文档";
|
|
|
}
|