33import com .paypal .api .payments .Agreement ;
44import com .paypal .api .payments .Plan ;
55import com .paypal .base .rest .PayPalRESTException ;
6+ import org .json .JSONObject ;
67import org .springframework .beans .factory .annotation .Autowired ;
7- import org .springframework .web .bind .annotation .GetMapping ;
8- import org .springframework .web .bind .annotation .RequestParam ;
9- import org .springframework .web .bind .annotation .RestController ;
8+ import org .springframework .http .HttpHeaders ;
9+ import org .springframework .http .HttpStatus ;
10+ import org .springframework .http .MediaType ;
11+ import org .springframework .http .ResponseEntity ;
12+ import org .springframework .web .bind .annotation .*;
1013
1114import java .io .UnsupportedEncodingException ;
1215import java .net .MalformedURLException ;
1316
1417@ RestController
18+ @ RequestMapping ("paypal" )
1519public class PayPalController {
1620
1721 private PayPalBillingPlan payPalBillingPlan ;
@@ -25,20 +29,27 @@ public PayPalController(PayPalBillingPlan payPalBillingPlan, PayPalBillingAgreem
2529
2630 /**
2731 * Define and create billing plan and billing agreement.
28- * Redirects response to successful or unsuccessful URL
32+ * Redirects response to returned URL
2933 */
3034 @ GetMapping ("/subscribe" )
31- public void subscribe () {
35+ public @ ResponseBody ResponseEntity subscribe () {
36+ HttpHeaders headers = new HttpHeaders ();
37+ headers .setContentType (MediaType .APPLICATION_JSON );
38+
3239 try {
3340 Plan plan = payPalBillingPlan .create ();
3441 Agreement agreement = payPalBillingAgreement .define (plan .getId ());
35- String redirect = payPalBillingAgreement .create (agreement );
36- // TODO: 02/06/2019 REDIRECT to redirect url
42+
43+ JSONObject body = new JSONObject ();
44+ body .put ("url" , payPalBillingAgreement .create (agreement ));
45+ return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (200 ));
3746 } catch (PayPalRESTException e ) {
3847 System .err .println (e .getDetails ());
3948 } catch (MalformedURLException | UnsupportedEncodingException e ) {
4049 e .printStackTrace ();
4150 }
51+
52+ return new ResponseEntity <>("" , headers , HttpStatus .BAD_REQUEST );
4253 }
4354
4455 /**
0 commit comments