当前位置 : 主页 > 编程语言 > c++ >

JAVA生成客户端需要的支付宝参数

来源:互联网 收集:自由互联 发布时间:2021-07-03
AlipayUtils package com.pay.alipay;import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.domain.AlipayTradeAppPayModel;import com.alipay.api.internal.util
AlipayUtils
package com.pay.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.kunze.taigan.api.notify.PayNotifyHelper;

import java.io.PrintStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import utils.AppConfig;

public class AlipayUtils {
	private static final String APPID = "appid";
	private static final String PRIVATE_KEY = "";
	private static final String PUBLIC_KEY = "";
	private static final String CHARSET = "utf-8";
	private static final String SERVER_URI = "https://openapi.alipay.com/gateway.do";
	private static final String NOTIFY_URL = "http://www.notifyurl.com/notify_url_alipay.do";
	private static final String SUBJECT = "充值";
	private static final String BODY = "余额支付";
	private static final String PAY_TYPE = "支付宝";

	public static String getPayParams(String price, String out_trade_no) {
		AlipayClient alipayClient = new DefaultAlipayClient(SERVER_URI, APPID,
				PRIVATE_KEY, "json", "utf-8", PUBLIC_KEY, "RSA2");
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		request.setNotifyUrl(NOTIFY_URL);
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(BODY);
		model.setSubject(SUBJECT);
		model.setOutTradeNo(out_trade_no);
		model.setTimeoutExpress("5m");
		model.setTotalAmount(price);
		model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		try {
			AlipayTradeAppPayResponse response = (AlipayTradeAppPayResponse) alipayClient
					.sdkExecute(request);
			return URLEncoder.encode(response.getBody());
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String payNofityCallback(Map requestParams) {
		System.out.println("支付宝支付结果通知" + requestParams.toString());
		Map
 
   params = new HashMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			params.put(name, values[0]);
		}
		try {
			if ((!APPID.equals(params.get("auth_app_id")))
					&& (!APPID.equals(params.get("app_id")))) {
				return "fail";
			}
			if (AlipaySignature.rsaCheckV1(params, PUBLIC_KEY,
					AppConfig.DefaultEncoding, "RSA2")) {
				if ("TRADE_SUCCESS".equals(params.get("trade_status"))) {
					double amount = Double.parseDouble("0"
							+ (String) params.get("total_amount"));

					String out_trade_no = (String) params.get("out_trade_no");

					String trade_no = (String) params.get("trade_no");
					String[] order_params = out_trade_no.split("_");
					String userid = order_params[0];
					if (PayNotifyHelper.shouldDo(trade_no, PAY_TYPE)) {
						String orderTime_str = new SimpleDateFormat(
								"yyyy-MM-MM HH:mm:ss.SSS")
								.format(new SimpleDateFormat(
										"yyyyMMddHHmmssSSS")
										.parse(order_params[1]));
						if (PayNotifyHelper.addOrder(userid, amount,
								out_trade_no, trade_no,PAY_TYPE,
								orderTime_str,
								(String) params.get("gmt_payment"))) {
							return "success";
						}
						return "fail";
					}
					return "success";
				}
				if ("TRADE_CLOSE".equals(params.get("trade_status"))) {
					return "success";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}
}
 
notify_url
response.setHeader("Content-type", "text/html;charset="  
                + AppConfig.DefaultEncoding);  
        response.setCharacterEncoding(AppConfig.DefaultEncoding);  
        request.setCharacterEncoding(AppConfig.DefaultEncoding);  
        String result=AlipayUtils.payNofityCallback(request.getParameterMap());  
        response.getOutputStream().write(result.getBytes(AppConfig.DefaultEncoding));
网友评论