Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit 2b8c713

Browse files
committed
Payment urls fixed, plan ids adding to database
1 parent 2d9cd70 commit 2b8c713

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/main/java/pl/simplemethod/codebin/paypal/PayPalBillingPlan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public PayPalBillingPlan(APIContext apiContext) {
2121
this.apiContext = apiContext;
2222
}
2323

24-
private static final String CANCEL_URL = "http://127.0.0.1/dashboard/dashboard-payment-error.html";
25-
private static final String PROCESS_URL = "http://127.0.0.1/dashboard/dashboard-payment-accept.html";
24+
private static final String CANCEL_URL = "http://127.0.0.1/dashboard.html#!/payment-error";
25+
private static final String PROCESS_URL = "http://127.0.0.1/paypal/payment-success";
2626

2727
/**
2828
* Creates a plan to billing plan

src/main/java/pl/simplemethod/codebin/paypal/PayPalController.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,77 @@
33
import com.paypal.api.payments.Agreement;
44
import com.paypal.api.payments.Plan;
55
import com.paypal.base.rest.PayPalRESTException;
6-
import org.json.JSONObject;
76
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;
127
import org.springframework.web.bind.annotation.*;
8+
import pl.simplemethod.codebin.model.Users;
9+
import pl.simplemethod.codebin.repository.UsersRepository;
1310

11+
import javax.servlet.http.HttpServletResponse;
12+
import java.io.IOException;
1413
import java.io.UnsupportedEncodingException;
1514
import java.net.MalformedURLException;
1615

1716
@RestController
1817
@RequestMapping("paypal")
1918
public class PayPalController {
2019

20+
private UsersRepository usersRepository;
21+
2122
private PayPalBillingPlan payPalBillingPlan;
2223
private PayPalBillingAgreement payPalBillingAgreement;
2324

25+
private static String planId;
26+
private static final String PAYMENT_ACCEPT_URL = "http://127.0.0.1/dashboard.html#!/payment-accept";
27+
2428
@Autowired
25-
public PayPalController(PayPalBillingPlan payPalBillingPlan, PayPalBillingAgreement payPalBillingAgreement) {
29+
public PayPalController(PayPalBillingPlan payPalBillingPlan, PayPalBillingAgreement payPalBillingAgreement,
30+
UsersRepository usersRepository) {
2631
this.payPalBillingPlan = payPalBillingPlan;
2732
this.payPalBillingAgreement = payPalBillingAgreement;
33+
this.usersRepository = usersRepository;
2834
}
2935

3036
/**
3137
* Define and create billing plan and billing agreement.
3238
* Redirects response to returned URL
3339
*/
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) {
3942
try {
4043
Plan plan = payPalBillingPlan.create();
4144
Agreement agreement = payPalBillingAgreement.define(plan.getId());
45+
planId = plan.getId();
4246

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));
4648
} catch (PayPalRESTException e) {
4749
System.err.println(e.getDetails());
4850
} catch (MalformedURLException | UnsupportedEncodingException e) {
4951
e.printStackTrace();
52+
} catch (IOException e) {
53+
e.printStackTrace();
5054
}
51-
52-
return new ResponseEntity<>("", headers, HttpStatus.BAD_REQUEST);
5355
}
5456

5557
/**
5658
* Executes successfully created billing agreement
5759
* @param token Token which is automatically assigned to billing agreement
5860
*/
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) {
6163
try {
6264
payPalBillingAgreement.execute(token);
65+
66+
Users users = usersRepository.getFirstById(id);
67+
users.setSubscription(planId);
68+
usersRepository.save(users);
6369
} catch (PayPalRESTException e) {
6470
System.err.println(e.getDetails());
6571
}
72+
73+
try {
74+
response.sendRedirect(PAYMENT_ACCEPT_URL);
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
6678
}
6779
}

0 commit comments

Comments
 (0)