创建订单
请求地址:http://orderapi.liangtouyang.com/api/orders/pay
请求名 | 类型 | 参考值 | 备注 | |
appid | String | 100010000 | 后台应用中心获取 | |
type | int | 0 | 0:微信支付 1:支付宝支付 | |
out_trade_no | string | Y1234567989 | 用户订单号 | |
money | string | 1.00 | 付款金额,注意必须是标准的金额格式如1.00 1.01 | |
notify_url | string | 可填可不填 不填则用后台应用内设置的为准 | ||
device | int | 1 | 1:电脑 2:手机 填写的类型会利于接口的返回类型 | |
sign | string | 32位的MD5加密字符串 |
|
计算签名
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <openssl/md5.h>
// Function to calculate MD5 hash
std::string md5_hash(const std::string& input) {
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, input.c_str(), input.length());
MD5_Final(digest, &md5);
char buffer[MD5_DIGEST_LENGTH * 2 + 1];
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
snprintf(buffer + (i * 2), 3, "%02x", digest[i]);
}
return std::string(buffer);
}
// Function to generate sign
std::string generate_sign(const std::map<std::string, std::string>& params, const std::string& secret_key) {
// Create a concatenated string from the sorted parameters
std::string param_string;
for (const auto& pair : params) {
param_string += pair.first + "=" + pair.second + "&";
}
// Remove the last '&'
if (!param_string.empty()) {
param_string.pop_back();
}
// Append the secret key
param_string += secret_key;
// Calculate MD5 hash
return md5_hash(param_string);
}
int main() {
std::map<std::string, std::string> params = {
{"appid", "100010000"},
{"type", "0"},
{"out_trade_no", "Y1234567989"},
{"money", "1.00"},
{"notify_url", ""},
{"device", "1"}
};
std::string secret_key = "your_secret_key_here";
std::string sign = generate_sign(params, secret_key);
std::cout << "Generated sign: " << sign << std::endl;
return 0;
}
查询订单
请求地址:http://orderapi.liangtouyang.com/api/orders/query
请求名 | 类型 | 参考值 | 备注 | |
appid | string | 100010000 | 后台应用处获取 | |
out_trade_no | string | Y123456789 | 用户自己订单号 (二选一) | |
trade_no | string | R201400000000000000 | 平台订单号(二选一) |
查询订单 无需sign计算
验证签名
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <openssl/md5.h>
// Function to calculate MD5 hash
std::string md5_hash(const std::string& input) {
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, input.c_str(), input.length());
MD5_Final(digest, &md5);
char buffer[MD5_DIGEST_LENGTH * 2 + 1];
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
snprintf(buffer + (i * 2), 3, "%02x", digest[i]);
}
return std::string(buffer);
}
// Function to verify sign
bool verify_sign(const std::map<std::string, std::string>& params, const std::string& received_sign, const std::string& secret_key) {
// Create a concatenated string from the sorted parameters
std::string param_string;
for (const auto& pair : params) {
param_string += pair.first + "=" + pair.second + "&";
}
// Remove the last '&'
if (!param_string.empty()) {
param_string.pop_back();
}
// Append the secret key
param_string += secret_key;
// Calculate MD5 hash and compare
std::string computed_sign = md5_hash(param_string);
return computed_sign == received_sign;
}
int main() {
std::map<std::string, std::string> params = {
{"appid", "100100001"},
{"time", "1713760397"},
{"amount", "1.100"},
{"status", "1"},
{"out_trade_no", "y1713760297"},
{"trade_no", "R240422123135450090"}
};
std::string received_sign = "a7762b869a349409616ed4cbc5018dae";
std::string secret_key = "your_secret_key_here";
bool is_valid = verify_sign(params, received_sign, secret_key);
if (is_valid) {
std::cout << "签名验证通过" << std::endl;
} else {
std::cout << "签名验证失败" << std::endl;
}
return 0;
}
进件地址:两头羊支付