30
30
public class PaypalService {
31
31
private final PayPalHttpClient payPalHttpClient ;
32
32
private final PaymentService paymentService ;
33
+ private final OrderService orderService ;
33
34
private final BigDecimal maxPay = BigDecimal .valueOf (1000 );
34
35
@ Value ("${yas.public.url}/capture" )
35
36
private String returnUrl ;
@@ -51,12 +52,12 @@ public PaypalRequestPayment createPayment(RequestPayment requestPayment) {
51
52
PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest ().amountWithBreakdown (amountWithBreakdown );
52
53
orderRequest .purchaseUnits (List .of (purchaseUnitRequest ));
53
54
ApplicationContext applicationContext = new ApplicationContext ()
54
- .returnUrl (returnUrl )
55
- .cancelUrl (cancelUrl )
56
- .brandName (Constants .Yas .BRAND_NAME )
57
- .landingPage ("BILLING" )
58
- .userAction ("PAY_NOW" )
59
- .shippingPreference ("NO_SHIPPING" );
55
+ .returnUrl (returnUrl )
56
+ .cancelUrl (cancelUrl )
57
+ .brandName (Constants .Yas .BRAND_NAME )
58
+ .landingPage ("BILLING" )
59
+ .userAction ("PAY_NOW" )
60
+ .shippingPreference ("NO_SHIPPING" );
60
61
61
62
orderRequest .applicationContext (applicationContext );
62
63
OrdersCreateRequest ordersCreateRequest = new OrdersCreateRequest ().requestBody (orderRequest );
@@ -65,10 +66,10 @@ public PaypalRequestPayment createPayment(RequestPayment requestPayment) {
65
66
HttpResponse <Order > orderHttpResponse = payPalHttpClient .execute (ordersCreateRequest );
66
67
Order order = orderHttpResponse .result ();
67
68
String redirectUrl = order .links ().stream ()
68
- .filter (link -> "approve" .equals (link .rel ()))
69
- .findFirst ()
70
- .orElseThrow (NoSuchElementException ::new )
71
- .href ();
69
+ .filter (link -> "approve" .equals (link .rel ()))
70
+ .findFirst ()
71
+ .orElseThrow (NoSuchElementException ::new )
72
+ .href ();
72
73
73
74
CheckoutIdHelper .setCheckoutId (requestPayment .checkoutId ());
74
75
return new PaypalRequestPayment ("success" , order .id (), redirectUrl );
@@ -91,15 +92,17 @@ public CapturedPaymentVm capturePayment(String token) {
91
92
BigDecimal paymentFee = new BigDecimal (paypalFee );
92
93
BigDecimal amount = new BigDecimal (capture .amount ().value ());
93
94
95
+ var orderVm = orderService .getOrderByCheckoutId (CheckoutIdHelper .getCheckoutId ());
94
96
95
97
CapturedPaymentVm capturedPayment = CapturedPaymentVm .builder ()
96
- .paymentFee (paymentFee )
97
- .gatewayTransactionId (order .id ())
98
- .amount (amount )
99
- .paymentStatus (order .status ())
100
- .paymentMethod ("PAYPAL" )
101
- .checkoutId (CheckoutIdHelper .getCheckoutId ())
102
- .build ();
98
+ .orderId (orderVm .id ())
99
+ .paymentFee (paymentFee )
100
+ .gatewayTransactionId (order .id ())
101
+ .amount (amount )
102
+ .paymentStatus (order .status ())
103
+ .paymentMethod ("PAYPAL" )
104
+ .checkoutId (CheckoutIdHelper .getCheckoutId ())
105
+ .build ();
103
106
104
107
paymentService .capturePayment (capturedPayment );
105
108
return capturedPayment ;
@@ -110,4 +113,4 @@ public CapturedPaymentVm capturePayment(String token) {
110
113
}
111
114
return CapturedPaymentVm .builder ().failureMessage ("Something Wrong!" ).build ();
112
115
}
113
- }
116
+ }
0 commit comments