Skip to content

Instantly share code, notes, and snippets.

@charleehu
Created November 10, 2014 07:41
Show Gist options
  • Save charleehu/777d72ca479e4531f8e1 to your computer and use it in GitHub Desktop.
Save charleehu/777d72ca479e4531f8e1 to your computer and use it in GitHub Desktop.
MPAY支付DEMO
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
*
* @author <a href="mailto:huxiaowei@corp.netease.com">Xiaowei Hu</a>
* @version 1.0 Jul 21, 2014 3:41:17 PM
*/
public class TestPay {
public static void main(String[] args) throws ClientProtocolException, IOException {
String host = "https://service.mkey.163.com";
String path = "/mpay_sandbox/games/********/orders";
String apiKey = "********";
long timestamp = System.currentTimeMillis() / 1000L;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(host + path);
//post
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("uid", "******"));
nvps.add(new BasicNameValuePair("goods_name", "goods_name"));
nvps.add(new BasicNameValuePair("price", "1"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
//signature
String stringToSign = String.format("%s\n%d\n%s\n%s", "POST", timestamp, EntityUtils.toString(httpPost.getEntity()), path);
String signature = base64(hmacSha1(apiKey, stringToSign));
System.out.println("signature: " + signature);
//header
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
httpPost.setHeader("X-Mpay-Timestamp", String.valueOf(timestamp));
httpPost.setHeader("Authorization", "MPAY :" + signature);
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
System.out.println(EntityUtils.toString(entity2));
} finally {
response2.close();
}
}
public static byte[] hmacSha1(String key, String value) {
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(value.getBytes());
return rawHmac;
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e) {
throw new RuntimeException("hmacSha1 error", e);
}
}
public static String base64(byte[] binaryData) {
return Base64.encodeBase64String(binaryData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment