参考
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.hKuHU3&treeId=54&articleId=106370&docType=1#s7
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient( "https://openapi.alipay.com/gateway.do" , APP_ID, APP_PRIVATE_KEY, "json" , CHARSET, ALIPAY_PUBLIC_KEY, "RSA2" );
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody( "我是测试数据" );
model.setSubject( "App支付测试Java" );
model.setOutTradeNo(outtradeno);
model.setTimeoutExpress( "30m" );
model.setTotalAmount( "0.01" );
model.setProductCode( "QUICK_MSECURITY_PAY" );
request.setBizModel(model);
request.setNotifyUrl( "商户外网可以访问的异步地址" );
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
System.out.println(response.getBody()); //就是orderString 可以直接给客户端请求,无需再做处理。
} catch (AlipayApiException e) {
e.printStackTrace();
}
JAVA服务端SDK生成APP支付订单信息示例
JAVA服务端验证异步通知信息参数示例
//获取支付宝POST过来反馈信息
Map<String,String> params = new HashMap<String,String>();
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 = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
boolean flag = AlipaySignature.rsaCheckV1(params, alipaypublicKey, charset, "RSA2" )
PHP服务端SDK生成APP支付订单信息示例
$aop = new AopClient;
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do" ;
$aop->appId = "app_id" ;
$aop->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
$aop->format = "json" ;
$aop->charset = "UTF-8" ;
$aop->signType = "RSA2" ;
$aop->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串' ;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$bizcontent = "{\"body\":\"我是测试数据\","
. "\"subject\": \"App支付测试\","
. "\"out_trade_no\": \"20170125test01\","
. "\"timeout_express\": \"30m\","
. "\"total_amount\": \"0.01\","
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}" ;
$request->setNotifyUrl( "商户外网可以访问的异步地址" );
$request->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
echo htmlspecialchars($response); //就是orderString 可以直接给客户端请求,无需再做处理。
PHP服务端验证异步通知信息参数示例
$aop = new AopClient;
$aop->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串' ;
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA" );
.NET服务端SDK生成APP支付订单信息示例
IAopClient client = new DefaultAopClient( "https://openapi.alipay.com/gateway.do" , APPID, APP_PRIVATE_KEY, "json" , "1.0" , "RSA2" , ALIPAY_PUBLIC_KEY, CHARSET, false );
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.Body = "我是测试数据" ;
model.Subject = "App支付测试DoNet" ;
model.TotalAmount = "0.01" ;
model.ProductCode = "QUICK_MSECURITY_PAY" ;
model.OutTradeNo = "20170216test01" ;
model.TimeoutExpress = "30m" ;
request.SetBizModel(model);
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = client.SdkExecute(request);
//HttpUtility.HtmlEncode是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
Response.Write(HttpUtility.HtmlEncode(response.Body));
//页面输出的response.Body就是orderString 可以直接给客户端请求,无需再做处理。
.NET服务端验证异步通知信息参数示例
/// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
/// request回来的信息组成的数组
public Dictionary<string, string> GetRequestPost()
{
int i = 0 ;
IDictionary<string, string> sArray = new IDictionary<string, string>();
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.Form;
// Get names of all forms into a string array.
String[] requestItem = coll.AllKeys;
for (i = 0 ; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
}
return sArray;
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//bool RSACheckV1(IDictionary<string, string> parameters, string alipaypublicKey, string charset, string signType, bool keyFromFile)
bool flag = AlipaySignature.RSACheckV1(GetRequestPost(), alipaypublicKey, charset, "RSA2" , false );