test.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. $test = '{"app_id":"800989422602","sign_type":"MD5","out_order_no":"tmp81202006223292636412","merchant_id":"1900098942","timestamp":"1592816350","product_code":"pay","payment_type":"direct","total_amount":1,"trade_type":"H5","uid":"800989422602","version":"2.0","currency":"CNY","subject":"千鸟云商-线上商城","body":"千鸟云商-线上商城","trade_time":"1592816350","valid_time":"3600","notify_url":"https:\/\/tp-pay.snssdk.com\/paycallback","wx_url":"https:\/\/wx.tenpay.com\/cgi-bin\/mmpayweb-bin\/checkmweb?prepay_id=wx22165910705139979d7a35b91097735600&package=1423074529","alipay_url":"","risk_info":"{\"ip\":\"1.80.120.55\"}","wx_type":"MWEB","sign":"57dd80fef94c2750a60eca50a2031235"}';
  3. $test = json_decode($test, true);
  4. $test = getSignContent($test);
  5. var_dump($test);exit();
  6. /**
  7. * 生成签名方法
  8. * 官方接口地址: https://microapp.bytedance.com/dev/cn/mini-app/develop/api/open-interface/payment/mini-app-pay-plugin-reference/server-sign
  9. * 官方签名测试地址: https://pay-doc.toutiao.com/tools/sign
  10. * @param $parm
  11. * @return string
  12. */
  13. function getSign($parm)
  14. {
  15. // 剔除risk_info
  16. unset($parm['risk_info']);
  17. unset($parm['sign']);
  18. //非空参数值的参数按照参数名ASCII码从小到大排序(字典序)
  19. if(ksort($parm)){
  20. $stringA = '';
  21. //使用URL键值对的格式拼接成字符串stringA
  22. $i = 1;
  23. foreach($parm as $key => $value){
  24. if($value === 0 || !empty($value)){
  25. $stringA .=$key.'='.$value;
  26. if($i != count($parm)){
  27. $stringA .= '&';
  28. }
  29. }
  30. $i++;
  31. }
  32. }else{
  33. return ResultWrapper::fail('对参数排序出错', ErrorCode::$paramError);
  34. }
  35. echo $stringA.PHP_EOL;
  36. //在stringA最后拼接上key=商户支付密钥
  37. $stringSignTemp = $stringA.'45c87Fa0352211e78D40d4977a9Ea871';
  38. $signValue = md5($stringSignTemp);
  39. return $signValue;
  40. }
  41. function getSignContent($params , $app_secret='', $charset='UTF-8') {
  42. unset($params['risk_info']);
  43. unset($params['sign']);
  44. ksort($params);
  45. $stringToBeSigned = "";
  46. $i = 0;
  47. foreach ($params as $k => $v) {
  48. if(!isset($v) || $v === null || trim($v) === "" || "@" == substr($v, 0, 1)){
  49. continue;
  50. }
  51. // 转换成目标字符集
  52. if (!empty($v)) {
  53. $fileType = "UTF-8";
  54. if (strcasecmp($fileType, $charset) != 0) {
  55. $v = mb_convert_encoding($v, $charset, $fileType);
  56. }
  57. }
  58. if ($i == 0) {
  59. $stringToBeSigned .= "$k" . "=" . "$v";
  60. } else {
  61. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  62. }
  63. $i++;
  64. }
  65. unset ($k, $v);
  66. var_dump($stringToBeSigned);
  67. return md5($stringToBeSigned.'45c87Fa0352211e78D40d4977a9Ea871');
  68. }