标签:服务 port 3.1.1 char 二维码 too 调用 mat 编码
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
request.setBizContent(" {" +
" \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
" \"primary_industry_code\":\"10001/20102\"," +
" \"secondary_industry_code\":\"10001/20102\"," +
" \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
" }");
AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request);
//调用成功,则处理业务逻辑
if(response.isSuccess()){
//.....
}
#应用id,要求应用上线,且其下功能签约状态为生效
appId=20110906...78327
#应用私钥(appId所属应用的应用私钥)RSA2加密方式
privateKey=MIIEvgI...oC6/I2xPqZnvhbntS5qi1NwYg2
#支付宝公钥
alipayPublicKey=MIIBI...wIDAQAB
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 支付宝API类
*/
public class AlipayAPI {
/** 应用ID */
private static String appId;
/** 应用私钥 */
private static String privateKey;
/** 支付宝公钥 */
private static String alipayPublicKey;
/** 支付调用接口 */
private static final String url_pay = "https://openapi.alipay.com/gateway.do";
static {
InputStream is = AlipayAPI.class.getResourceAsStream("/alipay.properties");
Properties prop = new Properties();
try {
prop.load(is);
setAppId(prop.getProperty("appId"));
setPrivateKey(prop.getProperty("privateKey"));
setAlipayPublicKey(prop.getProperty("alipayPublicKey"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getAppId() {
return appId;
}
public static void setAppId(String appId) {
AlipayAPI.appId = appId;
}
public static String getPrivateKey() {
return privateKey;
}
public static void setPrivateKey(String privateKey) {
AlipayAPI.privateKey = privateKey;
}
public static String getAlipayPublicKey() {
return alipayPublicKey;
}
public static void setAlipayPublicKey(String alipayPublicKey) {
AlipayAPI.alipayPublicKey = alipayPublicKey;
}
public static String getUrl_pay() {
return url_pay;
}
}
/**
* 统一收单交易支付的参数封装类
*/
public class PayParam {
/** 商户订单 */
private String out_trade_no;
/** 支付场景 */
private String scene;
/** 支付授权码,即用户的付款码 */
private String auth_code;
/** 订单标题 */
private String subject;
/** 订单金额 */
private double total_amount;
public PayParam(String out_trade_no, String subject, double total_amount, String auth_code) {
this.out_trade_no = out_trade_no;
this.subject = subject;
this.total_amount = total_amount;
this.auth_code = auth_code;
}
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getScene() {
return scene;
}
public void setScene(String scene) {
this.scene = scene;
}
public String getAuth_code() {
return auth_code;
}
public void setAuth_code(String auth_code) {
this.auth_code = auth_code;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public double getTotal_amount() {
return total_amount;
}
public void setTotal_amount(double total_amount) {
this.total_amount = total_amount;
}
}
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import java.util.Date;
/**
* 支付宝的工具类
*/
public class AlipayUtil {
/** UTF8编码格式 */
private static final String CHARSET_UTF8 = "UTF-8";
/** 加密方式 */
private static final String SIGN_TYPE = "RSA2";
/** 数据格式 */
private static final String FORMAT = "JSON";
/** 支付场景,条码支付 */
private static final String SCENE_BARCODE = "bar_code";
/**
* 当面付(条码支付)
* @param content
* @return
*/
public static AlipayTradePayResponse barCodePay(PayParam content){
AlipayClient client = new DefaultAlipayClient(AlipayAPI.getUrl_pay(), AlipayAPI.getAppId(),
AlipayAPI.getPrivateKey(), FORMAT, CHARSET_UTF8, AlipayAPI.getAlipayPublicKey(), SIGN_TYPE);
AlipayTradePayRequest request = new AlipayTradePayRequest();
content.setScene(SCENE_BARCODE);
String bizContent = JSON.toJSONString(content);
request.setBizContent(bizContent);
AlipayTradePayResponse response = null;
try {
response = client.execute(request);
} catch (AlipayApiException e) {
e.printStackTrace();
}
return response;
}
/**
* 支付完成后的业务处理
* @param response
*/
public static void afterBarCodePay(AlipayTradePayResponse response) {
if (response.isSuccess()) {
//处理业务逻辑
System.out.println("start handle business");
}
}
/**
* 获取商户订单
* @return
*/
public static String getTradeNo() {
return "PAY" + new Date().getTime();
}
//测试
public static void main(String[] args) {
String tradeNo = getTradeNo();
String subject = "测试用订单";
double totalAmount = 0.01;
String authCode = "274726044141298157";
PayParam param = new PayParam(tradeNo, subject, totalAmount, authCode);
AlipayTradePayResponse response = barCodePay(param);
System.out.println(response.getBody());
afterBarCodePay(response);
}
}
标签:服务 port 3.1.1 char 二维码 too 调用 mat 编码
原文地址:http://www.cnblogs.com/deng-cc/p/7495264.html