Bladeren bron

feat: 添加PHP示例

gatsby 3 weken geleden
bovenliggende
commit
b8706d2aca

+ 108 - 0
frontend/src/views/module_payment/apikey/docs/APIKey签名验签 - PHP示例代码.md

@@ -0,0 +1,108 @@
+## APIKey签名/验签 - PHP示例代码
+
+
+
+```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);
+        }
+    }
+	
+    /**
+     * 生成签名
+     *
+     * @param string $apiSecret API密钥
+     * @param array $requestData 请求参数字典
+     * @param array $excludeParams 需要排除的参数列表
+     * @return string 签名字符串
+     */
+    public static function generateSignature(
+        string $apiSecret,
+        array $requestData,
+        array $excludeParams = ['sign']
+    ): string {
+        // 1. 过滤参数
+        $filteredData = [];
+        foreach ($requestData as $key => $value) {
+            // 排除指定参数
+            if (in_array($key, $excludeParams, true)) {
+                continue;
+            }
+            // 排除 null 和 空字符串
+            if ($value === null || $value === '') {
+                continue;
+            }
+            // 排除空数
+            if (is_array($value) && empty($value)) {
+                continue;
+            }
+            $filteredData[$key] = $value;
+        }
+
+        // 2. 字典序排序 (ASCII)
+        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);
+        // 3. HMAC-SHA256 签名
+        return hash_hmac('sha256', $signStr, $apiSecret);
+    }
+
+    /**
+     * 验证签名
+     *
+     * @param string $apiSecret API密钥
+     * @param array $requestData 请求参数字典
+     * @param string $signature 待验证的签名
+     * @return bool
+     */
+    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";
+```
+

+ 87 - 1
frontend/src/views/module_payment/apikey/index.vue

@@ -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">&lt;?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";
+?&gt;</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文档";
 }