11package com .egzosn .pay .demo .controller ;
22
33
4- import java .io .IOException ;
5- import java .io .InputStream ;
64import java .math .BigDecimal ;
75import java .util .Map ;
86
97import javax .annotation .PostConstruct ;
108import javax .servlet .http .HttpServletRequest ;
9+ import javax .servlet .http .HttpServletResponse ;
1110
12- import org .springframework .web .bind .annotation .GetMapping ;
11+ import org .springframework .web .bind .annotation .PostMapping ;
1312import org .springframework .web .bind .annotation .RequestMapping ;
1413import org .springframework .web .bind .annotation .RestController ;
1514
@@ -117,49 +116,15 @@ public Map<String, Object> refundquery() {
117116 order .setRefundNo ("退款成功之后返回的退款单号" );
118117 return service .refundquery (order );
119118 }
120- /**
121- * 注意:这里不是异步回调的通知 IPN 地址设置的路径:https://developer.paypal.com/developer/ipnSimulator/
122- * PayPal确认付款调用的接口
123- * 用户确认付款后,paypal调用的这个方法执行付款
124- *
125- * @param request 请求
126- * @return 付款成功信息
127- * @throws IOException IOException
128- */
129- @ GetMapping (value = "payBackBefore.json" )
130- public String payBackBefore (HttpServletRequest request ) throws IOException {
131- try (InputStream is = request .getInputStream ()) {
132- // 参数解析与校验 https://developer.paypal.com/docs/api-basics/notifications/ipn/IPNIntro/#id08CKFJ00JYK
133- if (service .verify (service .getParameter2Map (request .getParameterMap (), is ))) {
134- // TODO 这里进行成功后的订单业务处理
135- // TODO 返回成功付款页面,这个到时候再做一个漂亮的页面显示,并使用前后端分离的模式
136- return service .successPayOutMessage (null ).toMessage ();
137- }
138- }
139- return "failure" ;
140- }
119+
141120
142121 /**
143122 * 支付回调地址
123+ * 请求方式必须为post, 回调详情文档:https://developer.paypal.com/api/rest/webhooks
124+ * 回调成功之后必须返回http状态码 200 才能行,不然一直会重复回调,注意:如果您的应用程序以任何其他状态码响应,贝宝会在三天内重试25次通知消息
144125 *
145- * @param request 请求
146- *
147- * @return 是否成功
148- *
149- * 业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看{@link com.egzosn.pay.common.api.PayService#setPayMessageHandler(com.egzosn.pay.common.api.PayMessageHandler)}
150- *
151- * 如果未设置 {@link com.egzosn.pay.common.api.PayMessageHandler} 那么会使用默认的 {@link com.egzosn.pay.common.api.DefaultPayMessageHandler}
152- * @throws IOException IOException
153- */
154- @ RequestMapping (value = "payBackOld.json" )
155- public String payBackOld (HttpServletRequest request ) throws IOException {
156- //业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看com.egzosn.pay.common.api.PayService.setPayMessageHandler()
157- return service .payBack (request .getParameterMap (), request .getInputStream ()).toMessage ();
158- }
159- /**
160- * 支付回调地址
161- *
162- * @param request 请求
126+ * @param request 请求
127+ * @param response 响应
163128 * @return 是否成功
164129 * <p>
165130 * 业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看{@link com.egzosn.pay.common.api.PayService#setPayMessageHandler(com.egzosn.pay.common.api.PayMessageHandler)}
@@ -171,12 +136,15 @@ public String payBackOld(HttpServletRequest request) throws IOException {
171136 * <b>注意:此方法一个订单只能调用一次, 建议在支付回调时进行调用</b>
172137 * 这里主要用来获取captureId使用,后续退款,查订单等等使用,用来替换下单返回的id
173138 * 详情: https://developer.paypal.com/docs/api/orders/v2/#orders_capture
174- *
175139 */
176- @ RequestMapping (value = "payBack.json" )
177- public String payBack (HttpServletRequest request ) {
140+ @ PostMapping (value = "payBack.json" )
141+ public String payBack (HttpServletRequest request , HttpServletResponse response ) {
178142 //业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看com.egzosn.pay.common.api.PayService.setPayMessageHandler()
179- return service .payBack (new HttpRequestNoticeParams (request )).toMessage ();
143+ final String message = service .payBack (new HttpRequestNoticeParams (request )).toMessage ();
144+ if (!"200" .equals (message )) {
145+ response .setStatus (400 );
146+ }
147+ return message ;
180148 }
181149
182150
0 commit comments