目录
1.进入支付宝开放者平台2.导入pom依赖3.controller4.访问
1.进入支付宝开放者平台
https://openhome.alipay.com/platform/home.htm 注册并实名 下载并注册花生壳,将内网映射为外网可以访问的的工具
2.导入pom依赖
<dependency>
<groupId>com
.alipay
.sdk
</groupId
>
<artifactId>alipay
-sdk
-java
</artifactId
>
<version>3.7.110.ALL
</version
>
</dependency
>
3.controller
@RestController
public class PayController {
@RequestMapping("/pay")
public void pay(HttpServletRequest httpRequest
,
HttpServletResponse httpResponse
) throws IOException
{
AlipayClient alipayClient
= new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , "AppID", "自己的私钥", "json", "utf-8", "自己的公钥", "RSA2");
AlipayTradePagePayRequest alipayRequest
= new AlipayTradePagePayRequest();
alipayRequest
.setReturnUrl( "回调网址(花生壳)" );
alipayRequest
.setNotifyUrl( "http://domain.com/CallBack/notify_url.jsp" );
alipayRequest
.setBizContent("{" +
" \"out_trade_no\":\"12345678909875\"," +
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
" \"total_amount\":88.88," +
" \"subject\":\"Iphone6 16G\"," +
" \"body\":\"Iphone6 16G\"," +
" \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
" \"extend_params\":{" +
" \"sys_service_provider_id\":\"2088511833207846\"" +
" }" +
" }" );
String form
= "" ;
try {
form
= alipayClient
.pageExecute(alipayRequest
).getBody();
} catch (AlipayApiException e
) {
e
.printStackTrace();
}
httpResponse
.setContentType( "text/html;charset=" + "UTF-8");
httpResponse
.getWriter().write(form
);
httpResponse
.getWriter().flush();
httpResponse
.getWriter().close();
}
@RequestMapping("/returnUrl")
public void returnUrl(HttpServletRequest request
, HttpResponse httpResp
) throws AlipayApiException
{
Map
<String, String> stringStringMap
= convertRequestParamsToMap(request
);
boolean signVerified
= AlipaySignature
.rsaCheckV1(stringStringMap
, "公钥", "utf-8", "RSA2");
if (signVerified
){
System
.out
.println(stringStringMap
);
} else {
}
}
private static Map
<String, String> convertRequestParamsToMap(HttpServletRequest request
) {
Map
<String, String> retMap
= new HashMap<String, String>();
Set
<Map
.Entry
<String
, String
[]>> entrySet
= request
.getParameterMap().entrySet();
for (Map
.Entry
<String
, String
[]> entry
: entrySet
) {
String name
= entry
.getKey();
String
[] values
= entry
.getValue();
int valLen
= values
.length
;
if (valLen
== 1) {
retMap
.put(name
, values
[0]);
} else if (valLen
> 1) {
StringBuilder sb
= new StringBuilder();
for (String val
: values
) {
sb
.append(",").append(val
);
}
retMap
.put(name
, sb
.toString().substring(1));
} else {
retMap
.put(name
, "");
}
}
return retMap
;
}
}
4.访问
http://localhost:8088/pay