You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP集成Clover电商沙箱支付API获取信用卡token报加密PAN错误如何解决

问题原因及解决方法

核心错误点

  1. 公钥解析错误
    Clover返回的modulus和exponent都是十六进制字符串,你初始化Math_BigInteger时没有指定基数为16,默认按十进制解析,生成的公钥完全错误,加密结果无法被Clover识别。
  2. RSA加密模式不匹配
    phpseclib 1.0.x默认使用OAEP填充加密,而Clover要求使用PKCS#1 v1.5填充,加密模式不匹配导致密文无效。
  3. 字段名错误
    Clover令牌化接口要求的加密卡号字段名为encrypted_pan,你使用的cardEncrypted是错误字段名,接口无法识别到加密的PAN信息,这也是你删除cardEncrypted字段后报错完全一致的原因。
  4. 测试卡信息无效
    你使用的测试卡号仅10位(正常Discover卡为16位)、过期时间为2021年(已过期),无效的卡信息也会导致校验失败。

修复代码片段

把你RSA加密及参数构造部分的代码替换为以下内容:

$rsa = new Crypt_RSA();
$prefix = $pay_key_data->prefix;
$modulus = $pay_key_data->modulus;
$exponent = $pay_key_data->exponent;

// 使用Clover官方沙箱有效Discover测试卡,过期时间替换为未来时间
$user_cc_no = '6011361000006668';
$user_cc_mo = '12';
$user_cc_yr = '2029';
$user_cc_cvv = '123';
$stingToEnc = $prefix . $user_cc_no; 

// 重点:指定十六进制基数解析公钥参数
$modulus = new Math_BigInteger($modulus, 16);
$exponent = new Math_BigInteger($exponent, 16);
$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setPublicKey();
// 重点:设置加密模式为PKCS1填充
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($stingToEnc);
$stingBase64Encpted = base64_encode($ciphertext);

$orderData = array();
// 重点:使用接口要求的正确字段名encrypted_pan
$orderData['encrypted_pan'] = $stingBase64Encpted;
$orderData['first6'] = substr($user_cc_no, 0, 6);
$orderData['last4'] = substr($user_cc_no,-4);
$orderData['exp_month'] = $user_cc_mo;
// 接口要求年份为两位,截取后两位
$orderData['exp_year'] = substr($user_cc_yr, -2);
$orderData['cvv'] = $user_cc_cvv;
$orderData['brand'] = "DISCOVER";
$cardData['card'] = $orderData;
$jsonCardData = json_encode($cardData);

其他校验点

  • 确认你的商户IDB0T4DYVTYBTZ1和你使用的authToken归属同一个沙箱账号
  • 提交请求前可以打印jsonCardData确认字段没有拼写错误、值格式符合要求

内容的提问来源于stack exchange,提问作者Rakesh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:39:02