-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathPaypalHandler.java
66 lines (59 loc) · 3.18 KB
/
PaypalHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.yas.payment.service.provider.handler;
import com.yas.payment.model.CapturedPayment;
import com.yas.payment.model.InitiatedPayment;
import com.yas.payment.model.enumeration.PaymentMethod;
import com.yas.payment.model.enumeration.PaymentStatus;
import com.yas.payment.paypal.service.PaypalService;
import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentRequest;
import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentResponse;
import com.yas.payment.paypal.viewmodel.PaypalCreatePaymentRequest;
import com.yas.payment.paypal.viewmodel.PaypalCreatePaymentResponse;
import com.yas.payment.service.PaymentProviderService;
import com.yas.payment.viewmodel.CapturePaymentRequestVm;
import com.yas.payment.viewmodel.InitPaymentRequestVm;
import org.springframework.stereotype.Component;
@Component
public class PaypalHandler extends AbstractPaymentHandler implements PaymentHandler {
private final PaypalService paypalService;
PaypalHandler(PaymentProviderService paymentProviderService, PaypalService paypalService) {
super(paymentProviderService);
this.paypalService = paypalService;
}
@Override
public String getProviderId() {
return PaymentMethod.PAYPAL.name();
}
@Override
public InitiatedPayment initPayment(InitPaymentRequestVm initPaymentRequestVm) {
PaypalCreatePaymentRequest requestPayment = PaypalCreatePaymentRequest.builder()
.totalPrice(initPaymentRequestVm.totalPrice())
.checkoutId(initPaymentRequestVm.checkoutId())
.paymentMethod(initPaymentRequestVm.paymentMethod())
.paymentSettings(getPaymentSettings(getProviderId()))
.build();
PaypalCreatePaymentResponse paypalCreatePaymentResponse = paypalService.createPayment(requestPayment);
return InitiatedPayment.builder()
.status(paypalCreatePaymentResponse.status())
.paymentId(paypalCreatePaymentResponse.paymentId())
.redirectUrl(paypalCreatePaymentResponse.redirectUrl())
.build();
}
@Override
public CapturedPayment capturePayment(CapturePaymentRequestVm capturePaymentRequestVm) {
PaypalCapturePaymentRequest paypalCapturePaymentRequest = PaypalCapturePaymentRequest.builder()
.token(capturePaymentRequestVm.token())
.paymentSettings(getPaymentSettings(getProviderId()))
.build();
PaypalCapturePaymentResponse paypalCapturePaymentResponse
= paypalService.capturePayment(paypalCapturePaymentRequest);
return CapturedPayment.builder()
.checkoutId(paypalCapturePaymentResponse.checkoutId())
.amount(paypalCapturePaymentResponse.amount())
.paymentFee(paypalCapturePaymentResponse.paymentFee())
.gatewayTransactionId(paypalCapturePaymentResponse.gatewayTransactionId())
.paymentMethod(PaymentMethod.valueOf(paypalCapturePaymentResponse.paymentMethod()))
.paymentStatus(PaymentStatus.valueOf(paypalCapturePaymentResponse.paymentStatus()))
.failureMessage(paypalCapturePaymentResponse.failureMessage())
.build();
}
}