-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathCleengCheckoutService.ts
192 lines (153 loc) · 6.53 KB
/
CleengCheckoutService.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { inject, injectable } from 'inversify';
import type {
AccessMethod,
AddAdyenPaymentDetails,
ChooseOffer,
CreateOrder,
CreateOrderPayload,
DeletePaymentMethod,
FinalizeAdyenPaymentDetails,
GetAdyenPaymentSession,
GetEntitlements,
GetFinalizeAdyenPayment,
GetInitialAdyenPayment,
GetOffer,
GetOffers,
GetOrder,
GetPaymentMethods,
GetSubscriptionSwitch,
GetSubscriptionSwitches,
PaymentWithoutDetails,
PaymentWithPayPal,
SwitchSubscription,
UpdateOrder,
UpdateOrderResponse,
UpdatePaymentWithPayPal,
} from '../../../../types/checkout';
import CheckoutService from '../CheckoutService';
import { GET_CUSTOMER_IP } from '../../../modules/types';
import type { GetCustomerIP } from '../../../../types/get-customer-ip';
import type { ServiceResponse } from '../../../../types/service';
import CleengService from './CleengService';
@injectable()
export default class CleengCheckoutService extends CheckoutService {
private readonly cleengService: CleengService;
private readonly getCustomerIP: GetCustomerIP;
accessMethod: AccessMethod = 'offer';
constructor(cleengService: CleengService, @inject(GET_CUSTOMER_IP) getCustomerIP: GetCustomerIP) {
super();
this.cleengService = cleengService;
this.getCustomerIP = getCustomerIP;
}
getOffers: GetOffers = async (payload) => {
return await Promise.all(
payload.offerIds.map(async (offerId) => {
const response = await this.getOffer({ offerId: String(offerId) });
if (response.errors.length > 0) {
throw new Error(response.errors[0]);
}
return response.responseData;
}),
);
};
chooseOffer: ChooseOffer = async () => {};
getOffer: GetOffer = async (payload) => {
const customerIP = await this.getCustomerIP();
return this.cleengService.get(`/offers/${payload.offerId}${customerIP ? '?customerIP=' + customerIP : ''}`);
};
createOrder: CreateOrder = async (payload) => {
const locales = await this.cleengService.getLocales();
if (locales.errors.length > 0) throw new Error(locales.errors[0]);
const customerIP = locales.responseData.ipAddress;
const createOrderPayload: CreateOrderPayload = {
offerId: payload.offer.offerId,
customerId: payload.customerId,
country: payload.country,
currency: locales?.responseData?.currency || 'EUR',
customerIP,
paymentMethodId: payload.paymentMethodId,
};
return this.cleengService.post('/orders', JSON.stringify(createOrderPayload), { authenticate: true });
};
getOrder: GetOrder = async ({ orderId }) => {
return this.cleengService.get(`/orders/${orderId}`, { authenticate: true });
};
updateOrder: UpdateOrder = async ({ order, ...payload }) => {
const response = await this.cleengService.patch<ServiceResponse<UpdateOrderResponse>>(`/orders/${order.id}`, JSON.stringify(payload), {
authenticate: true,
});
if (response.errors.length) {
if (response.errors[0].includes(`Order with ${order.id} not found`)) {
throw new Error('Order not found');
}
if (response.errors[0].includes(`Coupon ${payload.couponCode} not found`)) {
throw new Error('Invalid coupon code');
}
if (response.errors[0].includes(`Coupon ${payload.couponCode} cannot be applied on this offer`)) {
throw new Error('Invalid coupon code for this offer');
}
}
return response;
};
getPaymentMethods: GetPaymentMethods = async () => {
return this.cleengService.get('/payment-methods', { authenticate: true });
};
paymentWithoutDetails: PaymentWithoutDetails = async (payload) => {
return this.cleengService.post('/payments', JSON.stringify(payload), { authenticate: true });
};
paymentWithPayPal: PaymentWithPayPal = async (payload) => {
const { order, successUrl, cancelUrl, errorUrl } = payload;
const paypalPayload = {
orderId: order.id,
successUrl,
cancelUrl,
errorUrl,
};
return this.cleengService.post('/connectors/paypal/v1/tokens', JSON.stringify(paypalPayload), { authenticate: true });
};
getSubscriptionSwitches: GetSubscriptionSwitches = async (payload) => {
return this.cleengService.get(`/customers/${payload.customerId}/subscription_switches/${payload.offerId}/availability`, { authenticate: true });
};
getSubscriptionSwitch: GetSubscriptionSwitch = async (payload) => {
return this.cleengService.get(`/subscription_switches/${payload.switchId}`, { authenticate: true });
};
switchSubscription: SwitchSubscription = async (payload) => {
return this.cleengService.post(
`/customers/${payload.customerId}/subscription_switches/${payload.offerId}`,
JSON.stringify({ toOfferId: payload.toOfferId, switchDirection: payload.switchDirection }),
{ authenticate: true },
);
};
getEntitlements: GetEntitlements = async (payload) => {
return this.cleengService.get(`/entitlements/${payload.offerId}`, { authenticate: true });
};
createAdyenPaymentSession: GetAdyenPaymentSession = async (payload) => {
return await this.cleengService.post('/connectors/adyen/sessions', JSON.stringify(payload), { authenticate: true });
};
initialAdyenPayment: GetInitialAdyenPayment = async (payload) =>
this.cleengService.post(
'/connectors/adyen/initial-payment',
JSON.stringify({ ...payload, attemptAuthentication: this.cleengService.sandbox ? 'always' : undefined }),
{ authenticate: true },
);
finalizeAdyenPayment: GetFinalizeAdyenPayment = async (payload) =>
this.cleengService.post('/connectors/adyen/initial-payment/finalize', JSON.stringify(payload), { authenticate: true });
updatePaymentMethodWithPayPal: UpdatePaymentWithPayPal = async (payload) => {
return this.cleengService.post('/connectors/paypal/v1/payment_details/tokens', JSON.stringify(payload), { authenticate: true });
};
deletePaymentMethod: DeletePaymentMethod = async (payload) => {
return this.cleengService.remove(`/payment_details/${payload.paymentDetailsId}`, { authenticate: true });
};
addAdyenPaymentDetails: AddAdyenPaymentDetails = async (payload) =>
this.cleengService.post(
'/connectors/adyen/payment-details',
JSON.stringify({
...payload,
attemptAuthentication: this.cleengService.sandbox ? 'always' : undefined,
}),
{ authenticate: true },
);
finalizeAdyenPaymentDetails: FinalizeAdyenPaymentDetails = async (payload) =>
this.cleengService.post('/connectors/adyen/payment-details/finalize', JSON.stringify(payload), { authenticate: true });
directPostCardPayment = async () => false;
}