go语言 服务器接入小米支付SDK

服务器需要接入小米支付, 但是官网只有java和php版本,因此签名解析需要自己写


实行步骤:

1. 用eclipse打开java版本支付用例,加入mainClass文件,代码如下:

package com.xiaomi.migc.server.sdk;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class mainClass {
	private static String getSign(Map
    
    
     
      params) throws Exception{
		String signString = ParamUtil.getSortQueryString(params);
		return HmacSHA1Encryption.hmacSHA1Encrypt(signString, "QFehjb0X8qEhbkpi380GTQ=1");
	}
	
	
	 public static void main(String args[]){
		 Map
     
     
      
       mapInfo = new HashMap
      
      
       
       ();
		 mapInfo.put("appId", "2882303761517239138");
		 mapInfo.put("cpOrderId", "9786bffc-996d-4553-aa33-f7e92c0b29d5");
		 mapInfo.put("orderConsumeType", "10");
		 mapInfo.put("orderId", "21140990160359583390");
		 mapInfo.put("orderStatus", "TRADE_SUCCESS");
		 mapInfo.put("payFee", "1");
		 mapInfo.put("payTime", "2014-09-05%2015:20:27");
		 mapInfo.put("productCode", "com.demo_1");
		 mapInfo.put("productCount", "1");
		 mapInfo.put("productName", "银子1两");
		 mapInfo.put("uid", "100010");
		 try {
			System.out.println(mainClass.getSign(mapInfo));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}
      
      
     
     
    
    

生成测试用例:121f401e6e92fa8d8bb45392b8102891a058c1be

2. 编写golang代码,并测试,代码如下:
package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"fmt"
	"net/url"
	"sort"
)

func CheckXiaoMiPay(urlInfo url.Values) bool {
	mapKeyInfo := map[string]string{}
	var slKeys sort.StringSlice
	for key, _ := range urlInfo {
		if key == `signature` {
			continue
		}
		mapKeyInfo[key] = urlInfo.Get(key)
		slKeys = append(slKeys, key)
	}
	//check success
	slKeys.Sort()
	var sText string
	for _, key := range slKeys {
		sText = sText + key + `=` + mapKeyInfo[key] + `&`
	}
	if len(sText) != 0 {
		sText = sText[:len(sText)-1]
	}

	sEncrypt := getEncryptKey(sText, "QFehjb0X8qEhbkpi380GTQ=1")
	fmt.Println(sEncrypt)
	return sEncrypt == urlInfo.Get(`signature`)
}

func getEncryptKey(sText string, appSecret string) string {
	mac := hmac.New(sha1.New, []byte(appSecret))
	mac.Write([]byte(sText))
	slBytes := mac.Sum(nil)
	if slBytes == nil {
		return ``
	}
	var strRtn string
	for _, aByte := range slBytes {
		strRtn = strRtn + fmt.Sprintf("%02x", aByte)
	}
	return strRtn
}

func main() {
	urlData := url.Values{
		"appId":            []string{"2882303761517239138"},
		"cpOrderId":        []string{"9786bffc-996d-4553-aa33-f7e92c0b29d5"},
		"orderConsumeType": []string{"10"},
		"orderId":          []string{"21140990160359583390"},
		"orderStatus":      []string{"TRADE_SUCCESS"},
		"payFee":           []string{"1"},
		"payTime":          []string{"2014-09-05%2015:20:27"},
		"productCode":      []string{"com.demo_1"},
		"productCount":     []string{"1"},
		"productName":      []string{"银子1两"},
		"uid":              []string{"100010"},
		"signature":        []string{"121f401e6e92fa8d8bb45392b8102891a058c1be"},
	}
	fmt.Println(CheckXiaoMiPay(urlData))
}
经过测试打印为true,服务器程序只需要复制上面俩个函数,即可对签名进行确认。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值