2025-10-27 17:12:05 +08:00

76 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace addons\shopro\library\ccblife;
/**
* 建行生活MD5签名类
* 处理API消息签名和支付字符串签名
*/
class CcbMD5
{
/**
* 生成API消息签名
*
* ⚠️ 注意建行API接口签名使用大写MD5
* 签名规则MD5(JSON报文 + 服务方私钥) → 转大写
*
* 示例:
* message = '{"CLD_HEADER":{...},"CLD_BODY":{...}}'
* privateKey = 'MIICeAIBA...'
* sign = strtoupper(md5(message + privateKey))
*
* @param string $message JSON格式的源报文
* @param string $privateKey 服务方私钥BASE64格式
* @return string 大写的32位MD5签名
*/
public static function signApiMessage($message, $privateKey)
{
// 移除私钥中的空格和换行
$privateKey = preg_replace('/\s+/', '', $privateKey);
// 计算MD5并转大写建行要求
return strtoupper(md5($message . $privateKey));
}
/**
* 验证API消息签名
*
* @param string $message JSON格式的源报文
* @param string $signature 待验证的签名
* @param string $privateKey 商户私钥BASE64格式
* @return bool 签名是否有效
*/
public static function verifyApiSignature($message, $signature, $privateKey)
{
$expectedSignature = self::signApiMessage($message, $privateKey);
return $expectedSignature === strtoupper($signature);
}
/**
* 生成交易流水号
* 格式YYYYMMDDHHMMSS + 6位随机数
*
* @return string 20位交易流水号
*/
public static function generateTransactionSeq()
{
$timestamp = date('YmdHis');
$random = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
return $timestamp . $random;
}
/**
* 生成订单号
* 格式:前缀 + YYYYMMDDHHMMSS + 4位随机数
*
* @param string $prefix 订单号前缀,默认'CCB'
* @return string 订单号
*/
public static function generateOrderId($prefix = 'CCB')
{
$timestamp = date('YmdHis');
$random = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
return $prefix . $timestamp . $random;
}
}