接入说明

欢迎使用SIMPAY的接口服务!请按照以下步骤进行接入:

  1. 注册并获得 API 密钥。你可以在联系我们的官方人员注册并获取 API 密钥。

  2. 阅读接口文档。详细了解每个接口的功能,请求和响应格式。

                    
                        POST {{base_url}}/api/v1/resource
                        Headers:
                          - Content-Type: application/json
                          - Secret-Key: Your hmacHex
                        
                        Body:
                          {
                            "key1": "value1",
                            "key2": "value2",
                            "key3": "value3"
                          }
    
                        Response:
                        {
                            "success": true(or false),
                            "message": "Reply message",
                            "data": {
                                // Your data here
                            }
                        }
                    
                
  3. Secret-Key加密方式。加密方式为 HMAC-SHA512 算法。例 hash_hmac('sha512', "jsonData", "Your API key")

  4. 使用示例代码。我们为多种编程语言提供了示例代码,帮助你更快速地集成我们的接口。

    在 Bash 中使用 curl 的示例

                    
                        #!/bin/bash
    
                        # JSON data
                        jsonData='{"key1":"value1","key2":"value2","key3":"value3"}'
                        
                        # API URL
                        apiUrl="https://example.com/api/v1/resource"
                        
                        # API key
                        apiKey="Your API key"
                        
                        # Calculate HMAC-SHA512
                        hmacHex=$(echo -n "$jsonData" | openssl dgst -sha512 -hmac "$apiKey" -binary | xxd -p)
                        
                        # Sending POST request using curl
                        response=$(curl -X POST -d "$jsonData" -H "Content-Type: application/json" -H "Secret-Key: $hmacHex" "$apiUrl")
                        
                        # Printing the response
                        echo "Response: $response"
    
                    
                

    在 php 中使用curl的示例

      
                    
                        // JSON data
                        $jsonData = json_encode([
                            "key1" => "value1",
                            "key2" => "value2",
                            "key3" => "value3"
                        ]);
                        
                        $apiUrl = "https://example.com/api/v1/resource";
                        $hmacHex = hash_hmac('sha512', $jsonData, "Your API key");
                        
                        // Setting up cURL
                        $ch = curl_init($apiUrl);
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                            "Content-Type: application/json",
                            "Secret-Key: $hmacHex"
                        ]);
                        
                        // Executing cURL and getting the response
                        $response = curl_exec($ch);
                        
                        // Closing cURL session
                        curl_close($ch);
                        
                        // Printing the response
                        echo "Response: $response";
                    
                

    在 java 中使用 HttpURLConnection 的示例

                    
                        import java.io.DataOutputStream;
                        import java.io.InputStreamReader;
                        import java.io.BufferedReader;
                        import java.net.HttpURLConnection;
                        import java.net.URL;
                        import java.nio.charset.StandardCharsets;
                        import java.security.MessageDigest;
                        import java.util.Base64;
                        
                        public class ApiRequest {
                        
                            public static void main(String[] args) {
                                try {
                                    // JSON data
                                    String jsonData = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}";
                        
                                    // API URL
                                    String apiUrl = "https://example.com/api/v1/resource";
                        
                                    // API key
                                    String apiKey = "Your API key";
                        
                                    // Calculate HMAC-SHA512
                                    String hmacHex = calculateHmacSHA512(jsonData, apiKey);
                        
                                    // Sending POST request using HttpURLConnection
                                    URL url = new URL(apiUrl);
                                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                    connection.setRequestMethod("POST");
                                    connection.setRequestProperty("Content-Type", "application/json");
                                    connection.setRequestProperty("Secret-Key", hmacHex);
                                    connection.setDoOutput(true);
                        
                                    // Write JSON data to the request body
                                    try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                                        byte[] jsonDataBytes = jsonData.getBytes(StandardCharsets.UTF_8);
                                        outputStream.write(jsonDataBytes, 0, jsonDataBytes.length);
                                    }
                        
                                    // Get the response
                                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                                        String line;
                                        StringBuilder response = new StringBuilder();
                                        while ((line = reader.readLine()) != null) {
                                            response.append(line);
                                        }
                                        System.out.println("Response: " + response.toString());
                                    }
                        
                                    // Disconnect the connection
                                    connection.disconnect();
                        
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        
                            private static String calculateHmacSHA512(String data, String key) throws Exception {
                                Mac sha512Hmac = Mac.getInstance("HmacSHA512");
                                SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA512");
                                sha512Hmac.init(secretKey);
                                byte[] hmacData = sha512Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
                        
                                // Convert byte array to hexadecimal string
                                StringBuilder sb = new StringBuilder();
                                for (byte b : hmacData) {
                                    sb.append(String.format("%02x", b));
                                }
                                return sb.toString();
                            }
                        }
                    
                

    在nodejs中使用axios的示例

                    
                        const axios = require('axios');
                        const crypto = require('crypto');
                        
                        // JSON data
                        const jsonData = {
                          key1: 'value1',
                          key2: 'value2',
                          key3: 'value3',
                        };
                        
                        // API URL
                        const apiUrl = 'https://example.com/api/v1/resource';
                        
                        // API key
                        const apiKey = 'Your API key';
                        
                        // Calculate HMAC-SHA512
                        const hmacHex = calculateHmacSHA512(JSON.stringify(jsonData), apiKey);
                        
                        // Sending POST request using Axios
                        axios.post(apiUrl, jsonData, {
                          headers: {
                            'Content-Type': 'application/json',
                            'Secret-Key': hmacHex,
                          },
                        })
                          .then(response => {
                            console.log('Response:', response.data);
                          })
                          .catch(error => {
                            console.error('Error:', error.response ? error.response.data : error.message);
                          });
                        
                        function calculateHmacSHA512(data, key) {
                          const hmac = crypto.createHmac('sha512', key);
                          hmac.update(data);
                          return hmac.digest('hex');
                        }
                    
                

    .NET 中的示例

                    
                        using System;
                        using System.Net.Http;
                        using System.Security.Cryptography;
                        using System.Text;
                        using System.Threading.Tasks;
                        
                        class Program
                        {
                            static async Task Main()
                            {
                                // JSON data
                                var jsonData = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}";
                        
                                // API URL
                                var apiUrl = "https://example.com/api/v1/resource";
                        
                                // API key
                                var apiKey = "Your API key";
                        
                                // Calculate HMAC-SHA512
                                var hmacHex = CalculateHmacSHA512(jsonData, apiKey);
                        
                                // Sending POST request using HttpClient
                                using (var httpClient = new HttpClient())
                                {
                                    // Set headers
                                    httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
                                    httpClient.DefaultRequestHeaders.Add("Secret-Key", hmacHex);
                        
                                    // Prepare content
                                    var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
                        
                                    // Send POST request
                                    var response = await httpClient.PostAsync(apiUrl, content);
                        
                                    // Read response
                                    var responseContent = await response.Content.ReadAsStringAsync();
                        
                                    // Print or use the responseContent as needed
                                    Console.WriteLine("Response: " + responseContent);
                                }
                            }
                        
                            static string CalculateHmacSHA512(string data, string key)
                            {
                                using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(key)))
                                {
                                    var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
                                    return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
                                }
                            }
                        }
                    
                
  5. 接口使用。使用任何API接口都需要使用以上方式发送,接下来的说明里我们忽略了HEADER部分,但在实际仍需要此类操作。

  6. 数据回调。我们在数据回调时会在HEADER发送同样的SECRET-KEY给你。请注意判别SECRET-KEY及回调IP来源。并且在收到回调的同时给以下反馈信息。请注意我们只给一次回调。

                    
                        {"success":true}
                    
                
  7. 测试接口。在集成到你的应用程序之前,务必在我们的测试环境中进行测试。

  8. 集成到你的应用程序。将我们的接口集成到你的应用程序中,并确保处理可能的错误和异常情况。

如果你有任何问题或需要进一步的帮助,请联系我们的技术支持团队。

感谢使用我们的服务!