|
3 | 3 | import com.paypal.api.payments.Agreement; |
4 | 4 | import com.paypal.api.payments.Plan; |
5 | 5 | import com.paypal.base.rest.PayPalRESTException; |
6 | | -import org.json.JSONObject; |
7 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
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 | 7 | import org.springframework.web.bind.annotation.*; |
| 8 | +import pl.simplemethod.codebin.model.Users; |
| 9 | +import pl.simplemethod.codebin.repository.UsersRepository; |
13 | 10 |
|
| 11 | +import javax.servlet.http.HttpServletResponse; |
| 12 | +import java.io.IOException; |
14 | 13 | import java.io.UnsupportedEncodingException; |
15 | 14 | import java.net.MalformedURLException; |
16 | 15 |
|
17 | 16 | @RestController |
18 | 17 | @RequestMapping("paypal") |
19 | 18 | public class PayPalController { |
20 | 19 |
|
| 20 | + private UsersRepository usersRepository; |
| 21 | + |
21 | 22 | private PayPalBillingPlan payPalBillingPlan; |
22 | 23 | private PayPalBillingAgreement payPalBillingAgreement; |
23 | 24 |
|
| 25 | + private static String planId; |
| 26 | + private static final String PAYMENT_ACCEPT_URL = "http://127.0.0.1/dashboard.html#!/payment-accept"; |
| 27 | + |
24 | 28 | @Autowired |
25 | | - public PayPalController(PayPalBillingPlan payPalBillingPlan, PayPalBillingAgreement payPalBillingAgreement) { |
| 29 | + public PayPalController(PayPalBillingPlan payPalBillingPlan, PayPalBillingAgreement payPalBillingAgreement, |
| 30 | + UsersRepository usersRepository) { |
26 | 31 | this.payPalBillingPlan = payPalBillingPlan; |
27 | 32 | this.payPalBillingAgreement = payPalBillingAgreement; |
| 33 | + this.usersRepository = usersRepository; |
28 | 34 | } |
29 | 35 |
|
30 | 36 | /** |
31 | 37 | * Define and create billing plan and billing agreement. |
32 | 38 | * Redirects response to returned URL |
33 | 39 | */ |
34 | | - @GetMapping("/subscribe") |
35 | | - public @ResponseBody ResponseEntity subscribe() { |
36 | | - HttpHeaders headers = new HttpHeaders(); |
37 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
38 | | - |
| 40 | + @GetMapping(path = "/subscribe") |
| 41 | + public void subscribe(HttpServletResponse response) { |
39 | 42 | try { |
40 | 43 | Plan plan = payPalBillingPlan.create(); |
41 | 44 | Agreement agreement = payPalBillingAgreement.define(plan.getId()); |
| 45 | + planId = plan.getId(); |
42 | 46 |
|
43 | | - JSONObject body = new JSONObject(); |
44 | | - body.put("url", payPalBillingAgreement.create(agreement)); |
45 | | - return new ResponseEntity<>(body.toString(), headers, HttpStatus.valueOf(200)); |
| 47 | + response.sendRedirect(payPalBillingAgreement.create(agreement)); |
46 | 48 | } catch (PayPalRESTException e) { |
47 | 49 | System.err.println(e.getDetails()); |
48 | 50 | } catch (MalformedURLException | UnsupportedEncodingException e) { |
49 | 51 | e.printStackTrace(); |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
50 | 54 | } |
51 | | - |
52 | | - return new ResponseEntity<>("", headers, HttpStatus.BAD_REQUEST); |
53 | 55 | } |
54 | 56 |
|
55 | 57 | /** |
56 | 58 | * Executes successfully created billing agreement |
57 | 59 | * @param token Token which is automatically assigned to billing agreement |
58 | 60 | */ |
59 | | - @GetMapping("/subscribe-success") |
60 | | - public void success(@RequestParam String token) { |
| 61 | + @GetMapping("/payment-success") |
| 62 | + public void success(HttpServletResponse response, @RequestParam String token, @CookieValue("id") Integer id) { |
61 | 63 | try { |
62 | 64 | payPalBillingAgreement.execute(token); |
| 65 | + |
| 66 | + Users users = usersRepository.getFirstById(id); |
| 67 | + users.setSubscription(planId); |
| 68 | + usersRepository.save(users); |
63 | 69 | } catch (PayPalRESTException e) { |
64 | 70 | System.err.println(e.getDetails()); |
65 | 71 | } |
| 72 | + |
| 73 | + try { |
| 74 | + response.sendRedirect(PAYMENT_ACCEPT_URL); |
| 75 | + } catch (IOException e) { |
| 76 | + e.printStackTrace(); |
| 77 | + } |
66 | 78 | } |
67 | 79 | } |
0 commit comments