支付寶線上支付介面
index.jsp
<%@ page contentType=”text/html;charset=gb2312″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
</head>
<body >
<br>
<a href=”<%=gateway.payment.payment.CreateUrl()%>”><img src=”images/alipay_bwrx.gif” border=”0″></a>
</body>
</html>
alipay_notify.jsp
<%@ page contentType=”text/html;charset=gb2312″%>
<%@ page import=”java.util.*”%>
<%request.setCharacterEncoding(“gb2312”);
String partner = “”; //partner合作伙伴id(必須填寫)
String privateKey = “”; //partner 的對應交易安全校驗碼(必須填寫)
String alipayNotifyURL = “https://www.alipay.com/cooperate/gateway.do?service=notify_verify“
+ “&partner=”
+ partner
+ “¬ify_id=”
+ request.getParameter(“notify_id”);
//獲取支付寶ATN返回結果,true是正確的訂單資訊,false 是無效的
//如果你的伺服器不支援https訪問的話,需要用老的介面查詢地址了,論壇上會公佈
String responseTxt = gateway.checkURL.checkURL
.check(alipayNotifyURL);
Map params = new HashMap();
//獲得POST 過來引數設定到新的params中
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter
.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = “”;
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length – 1) ? valueStr + values[i]
: valueStr + values[i] + “,”;
//valueStr = valueStr + values[i];
}
params.put(name, valueStr);
}
String mysign = com.alipay.util.SignatureHelper.sign(params, privateKey);
//out.println(mysign+”—–“+request.getParameter(“sign”));
if (mysign.equals(request.getParameter(“sign”)) || responseTxt.equals(“true”) ){
out.println(“success”);
}
%>
retrun_url.jsp
<%@ page contentType=”text/html;charset=gb2312″%>
<%@ page import=”java.util.*”%>
<%request.setCharacterEncoding(“gb2312”);
//String partner = “”; //partner合作伙伴id(必須填寫)
String privateKey = “”; //partner 的對應交易安全校驗碼(必須填寫)
// String alipayNotifyURL = “http://www.alipay.com/cooperate/gateway.do?service=notify_verify“
// + “&partner=”
// + partner
// + “¬ify_id=”
// + request.getParameter(“notify_id”);
//獲取支付寶ATN返回結果,true是正確的訂單資訊,false 是無效的
//String responseTxt = gateway.checkURL.checkURL
// .check(alipayNotifyURL);
//由於return url的返回通知,notifyid為空,無法到伺服器查詢真假,所以只要比對簽名就好
Map params = new HashMap();
//獲得POST 過來引數設定到新的params中
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter
.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = “”;
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length – 1) ? valueStr + values[i]
: valueStr + values[i] + “,”;
//valueStr = valueStr + values[i];
}
params.put(name, valueStr);
}
String mysign = com.alipay.util.SignatureHelper_return.sign(params, privateKey);
//out.println(mysign+”—–“+request.getParameter(“sign”));
if (mysign.equals(request.getParameter(“sign”)) ){
if (request.getParameter(“trade_status”).equalsIgnoreCase(“TRADE_FINISHED”)){
// 可以做重定向,也可以用來虛擬物品發貨
}
}
%>
SignatureHelper.java
package com.alipay.util;
import gateway.md5.Md5Encrypt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class SignatureHelper {
public static String sign(Map params, String privateKey) {
Properties properties = new Properties();
for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
Object value = params.get(name);
if (value == null) {
continue;
}
if (name == null || name.equalsIgnoreCase(“sign”)
|| name.equalsIgnoreCase(“sign_type”)) {
continue;
}
properties.setProperty(name, value.toString());
}
String content = getSignatureContent(properties);
return sign(content, privateKey);
}
@SuppressWarnings(“unchecked”)
public static String getSignatureContent(Properties properties) {
StringBuffer content = new StringBuffer();
List keys = new ArrayList(properties.keySet());
Collections.sort(keys);
for (int i = 0; i < keys.size(); i++) {
String key = (String) keys.get(i);
String value = properties.getProperty(key);
content.append((i == 0 ? “” : “&”) + key + “=” + value);
}
return content.toString();
}
public static String sign(String content, String privateKey) {
if (privateKey == null) {
return null;
}
String signBefore = content + privateKey;
// System.out.print(Md5Encrypt.md5(signBefore));
return Md5Encrypt.md5(signBefore);
}
}
SignatureHelper_return.java
package com.alipay.util;
import gateway.md5.Md5Encrypt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class SignatureHelper_return {
public static String sign(Map params, String privateKey) {
Properties properties = new Properties();
for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
Object value = params.get(name);
if (value == null) {
continue;
}
if ( name.equalsIgnoreCase(“sign”)
|| name.equalsIgnoreCase(“sign_type”)) {
continue;
}
properties.setProperty(name, value.toString());
}
String content = getSignatureContent(properties);
return sign(content, privateKey);
}
@SuppressWarnings(“unchecked”)
public static String getSignatureContent(Properties properties) {
StringBuffer content = new StringBuffer();
List keys = new ArrayList(properties.keySet());
Collections.sort(keys);
for (int i = 0; i < keys.size(); i++) {
String key = (String) keys.get(i);
String value = properties.getProperty(key);
content.append((i == 0 ? “” : “&”) + key + “=” + value);
}
return content.toString();
}
public static String sign(String content, String privateKey) {
if (privateKey == null) {
return null;
}
String signBefore = content + privateKey;
// System.out.print(Md5Encrypt.md5(signBefore));
return Md5Encrypt.md5(signBefore);
}
}
checkURL.java
package gateway.checkURL;
import java.net.*;
import java.io.*;
public class checkURL {
/**
* 對字串進行MD5加密
* @param myUrl
*
* @param url
*
* @return 獲取url內容
*/
public static String check(String urlvalue ) {
String inputLine = “”;
try
{
URL url = new URL(urlvalue);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String stTemp = “”;
while((stTemp = in.readLine()) != null)
{
System.out.println(stTemp);
inputLine = inputLine + stTemp;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return inputLine;
}
}
Md5Encrypt.java
/**
* Alipay.com Inc. Copyright (c) 2004-2005 All Rights Reserved.
*
* <p>
* Created on 2005-7-9
* </p>
*/
package gateway.md5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密演算法
*/
public class Md5Encrypt {
/**
* 對字串進行MD5加密
*
* @param text 明文
*
* @return 密文
*/
public static String md5(String text) {
MessageDigest msgDigest = null;
try {
msgDigest = MessageDigest.getInstance(“MD5”);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(“System doesn`t support MD5 algorithm.”);
}
msgDigest.update(text.getBytes());
byte[] bytes = msgDigest.digest();
byte tb;
char low;
char high;
char tmpChar;
String md5Str = new String();
for (int i = 0; i < bytes.length; i++) {
tb = bytes[i];
tmpChar = (char) ((tb >>> 4) & 0x000f);
if (tmpChar >= 10) {
high = (char) ((`a` + tmpChar) – 10);
} else {
high = (char) (`0` + tmpChar);
}
md5Str += high;
tmpChar = (char) (tb & 0x000f);
if (tmpChar >= 10) {
low = (char) ((`a` + tmpChar) – 10);
} else {
low = (char) (`0` + tmpChar);
}
md5Str += low;
}
return md5Str;
}
}
payment.java
package gateway.payment;
import java.util.*;
public class payment {
public static String CreateUrl()
{
Date out_trade_no=new Date();
String paygateway = “https://www.alipay.com/cooperate/gateway.do?”; //`支付介面
String service = “create_direct_pay_by_user”;
String partner = “”; //partner合作伙伴ID(必填)
String sign_type = “MD5”;
String subject = “訂單號:” + out_trade_no; //subject 商品名稱
String body = “交易地點總價包含郵費”; //body 商品描述
String price = “100”; //price 商品單價 0.01~50000.00
String show_url = “www.alipay.com“;
String quantity = “1”;
String payment_type = “1”;
String logistics_type = “EXPRESS”;//logistic為物流資訊
String logistics_fee = “2”;
String logistics_payment = “SELLER_PAY”;
String logistics_type_1 = “EMS”;
String logistics_fee_1 = “4”;
String logistics_payment_1 = “SELLER_PAY”;
String seller_email = “”; //賣家賬號(必填)
String key = “”; //partner賬戶的支付寶安全校驗碼(必填)
String notify_url = “http://10.2.17.136:8081/new_jsp/alipay_notify.jsp”;//”alipay_notify.jsp“檔案的地址
String[] Oristr ={ “service=”+service, “partner=” + partner, “subject=” + subject, “body=” + body, “out_trade_no=” + out_trade_no, “price=” + price, “show_url=” + show_url, “quantity=” + quantity, “payment_type=” + payment_type, “logistics_type=” + logistics_type,
“logistics_fee=” + logistics_fee, “logistics_payment=” + logistics_payment, “logistics_type_1=” + logistics_type_1, “logistics_fee_1=” + logistics_fee_1, “logistics_payment_1=” + logistics_payment_1, “seller_email=” + seller_email, “notify_url=” + notify_url
};
Arrays.sort(Oristr);//Sorts the specified array of objects into ascending order
//according to the natural ordering of its elements.
String prestr=””;
for (int i = 0; i < Oristr.length; i++)
{
if (i==Oristr.length-1)
{
prestr = prestr + Oristr[i] ;
}
else
{
prestr = prestr + Oristr[i] + “&”;
}
}
prestr = prestr + key;
//生成Md5摘要;
String sign = gateway.md5.Md5Encrypt.md5(prestr);
//構造支付Url;
String parameter = “”;
parameter = parameter + paygateway;
for (int i = 0; i < Oristr.length; i++)
{
parameter = parameter + Oristr[i] + “&”;
}
parameter = parameter + “sign=” + sign + “&sign_type=” + sign_type;
//返回支付Url;
return parameter;
}
}
注意:
包的位置,
引數排序是以字母的排序排的
相關文章
- 支付寶線上支付介面開發教程與總結
- Django呼叫支付寶支付介面Django
- 支付寶介面開發
- Django對接支付寶Alipay支付介面Django
- 網站如何整合支付寶支付介面網站
- 支付寶支付產品介面文件地址和支付寶代金券理解
- 支付寶介面呼叫 -- JAVA版Java
- 支付寶新版介面曝光:支付寶小程式或迎來重磅入口
- SpringBoot 配置支付寶介面Spring Boot
- XorPay 個人支付平臺【支援個人微信支付和支付寶支付介面】
- 支付寶介面的數字簽名
- 支付寶介面對接開發過程
- 支付寶-API介面解析-轉賬到銀行API
- 支付寶介面代簽約失敗排查指南
- Java 支付寶支付,退款,單筆轉賬到支付寶賬戶(支付寶訂單退款)Java
- [後端開發]支付寶支付介面除錯 (Python v3.6)後端除錯Python
- 支付寶alipay移動支付
- Java接入支付寶支付教程Java
- vue-仿支付寶支付Vue
- 支付寶App支付全解析APP
- python呼叫支付寶支付介面詳細示例—附帶Django demo程式碼PythonDjango
- 線上支付介面申請和開發(一)
- PHP-Laravel支付寶支付和微信支付PHPLaravel
- 支付寶的快捷支付實現“支付”了
- C# 專案中【支付寶】介面開發整合案例C#
- pc端實現支付寶支付
- PHP支付寶支付開發流程PHP
- iOS支付寶支付主要程式碼iOS
- Android整合支付寶支付功能Android
- Laravel 搞定支付寶和微信掃碼支付Laravel
- 手機APP如何接入支付寶支付APP
- flutter 支付寶APP支付 (包含後臺)FlutterAPP
- Android 接入微信支付寶支付Android
- iOS使用Stripe整合支付寶Alipay支付iOS
- go-zero之App支付寶支付GoAPP
- Android 支付寶支付開發流程Android
- java支付寶支付(即時到賬)Java
- 微信支付,支付寶支付,銀聯支付——三大支付總結