-
Notifications
You must be signed in to change notification settings - Fork 0
/
expedia_payments_api.h
171 lines (143 loc) · 3.75 KB
/
expedia_payments_api.h
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
/*
* expedia_payments_api.h
*
* Created on: Sep 18, 2020
* Author: moustafa
*/
#ifndef EXPEDIA_PAYMENTS_API_H_
#define EXPEDIA_PAYMENTS_API_H_
using namespace std;
#include "json.hpp"
using namespace json;
// Below: What we did with flights and payments satisfy Dependency inversion principle
// High-level modules (expedia.com) should not depend on low-level modules (AirCandaAirlines/PaypalPayment APIs).
// Both should depend on abstractions (e.g. IFlighsFinder and IPayment).
// That is the power of interfaces BTW ;)
class PayPalCreditCard {
public:
string name;
string address;
string id;
string expiry_date;
int ccv;
};
class PayPalOnlinePaymentAPI {
public:
void SetCardInfo(const PayPalCreditCard* const card) {
}
bool MakePayment(double money) {
return true;
}
};
class StripeUserInfo {
public:
string name;
string address;
};
class StripeCardInfo {
public:
string id;
string expiry_date;
};
class StripePaymentAPI {
public:
bool static WithDrawMoney(StripeUserInfo user, StripeCardInfo card, double money) {
return true;
}
};
class SquarePaymentAPI {
public:
bool static WithDrawMoney(string JsonQuery) {
//cout << JsonQuery << "\n";
json::JSON obj = JSON::Load(JsonQuery);
return true;
}
};
class IPayment {
public:
virtual void SetUserInfo(string name, string address) = 0;
virtual void SetCardInfo(string id, string expiry_date, int ccv) = 0;
virtual bool MakePayment(double money) = 0;
virtual ~IPayment() {}
};
class PayPalPayment : public IPayment {
private:
PayPalOnlinePaymentAPI paypal;
PayPalCreditCard card;
public:
virtual void SetUserInfo(string name, string address) {
card.name = name;
card.address = address;
}
virtual void SetCardInfo(string id, string expiry_date, int ccv){
card.id = id;
card.expiry_date = expiry_date;
card.ccv = ccv;
}
virtual bool MakePayment(double money) {
paypal.SetCardInfo(&card);
return paypal.MakePayment(money);
}
};
class StripePayment : public IPayment {
private:
StripeCardInfo card;
StripeUserInfo user;
public:
// setters
virtual void SetUserInfo(string name, string address) {
user.name = name;
user.address = address;
}
virtual void SetCardInfo(string id, string expiry_date, int ccv) {
card.id = id;
card.expiry_date = expiry_date;
}
virtual bool MakePayment(double money) {
return StripePaymentAPI::WithDrawMoney(user, card, money);
}
};
class SquarePayment : public IPayment {
private:
string name;
string address;
string id;
string expiry_date;
int ccv;
public:
virtual void SetUserInfo(string name, string address) {
this->name = name;
this->address = address;
}
virtual void SetCardInfo(string id, string expiry_date, int ccv) {
this->id = id;
this->expiry_date = expiry_date;
this->ccv = ccv;
}
virtual bool MakePayment(double money) {
// this is similar to adapter pattern...
// we change format of interface to match another interface
json::JSON obj;
obj["user_info"] = json::Array(name, address);
obj["card_info"]["ID"] = id;
obj["card_info"]["DATE"] = expiry_date;
obj["card_info"]["CCV"] = ccv;
obj["money"] = money;
ostringstream oss;
oss << obj;
string json_query = oss.str();
return SquarePaymentAPI::WithDrawMoney(json_query);
}
};
class PaymentFactory {
public:
static IPayment* GetPaymentHelper() {
if (true)
return new SquarePayment();
else if (true)
return new PayPalPayment();
else
return new StripePayment();
}
};
#endif /* EXPEDIA_PAYMENTS_API_H_ */