/src/mozilla-central/dom/payments/PaymentActionResponse.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "PaymentActionResponse.h" |
8 | | #include "PaymentRequestUtils.h" |
9 | | #include "BasicCardPayment.h" |
10 | | |
11 | | namespace mozilla { |
12 | | namespace dom { |
13 | | |
14 | | /* PaymentResponseData */ |
15 | | |
16 | | NS_IMPL_ISUPPORTS(PaymentResponseData, nsIPaymentResponseData) |
17 | | |
18 | | NS_IMETHODIMP |
19 | | PaymentResponseData::GetType(uint32_t* aType) |
20 | 0 | { |
21 | 0 | NS_ENSURE_ARG_POINTER(aType); |
22 | 0 | *aType = mType; |
23 | 0 | return NS_OK; |
24 | 0 | } |
25 | | |
26 | | NS_IMETHODIMP |
27 | | PaymentResponseData::Init(const uint32_t aType) |
28 | 0 | { |
29 | 0 | if (aType != nsIPaymentResponseData::GENERAL_RESPONSE && |
30 | 0 | aType != nsIPaymentResponseData::BASICCARD_RESPONSE) { |
31 | 0 | return NS_ERROR_FAILURE; |
32 | 0 | } |
33 | 0 | mType = aType; |
34 | 0 | return NS_OK; |
35 | 0 | } |
36 | | |
37 | | /* GeneralResponseData */ |
38 | | |
39 | | NS_IMPL_ISUPPORTS_INHERITED(GeneralResponseData, |
40 | | PaymentResponseData, |
41 | | nsIGeneralResponseData) |
42 | | |
43 | | GeneralResponseData::GeneralResponseData() |
44 | | : mData(NS_LITERAL_STRING("{}")) |
45 | 0 | { |
46 | 0 | Init(nsIPaymentResponseData::GENERAL_RESPONSE); |
47 | 0 | } |
48 | | |
49 | | NS_IMETHODIMP |
50 | | GeneralResponseData::GetData(nsAString& aData) |
51 | 0 | { |
52 | 0 | aData = mData; |
53 | 0 | return NS_OK; |
54 | 0 | } |
55 | | |
56 | | NS_IMETHODIMP |
57 | | GeneralResponseData::InitData(JS::HandleValue aValue, JSContext* aCx) |
58 | 0 | { |
59 | 0 | if (aValue.isNullOrUndefined()) { |
60 | 0 | return NS_ERROR_FAILURE; |
61 | 0 | } |
62 | 0 | nsresult rv = SerializeFromJSVal(aCx, aValue, mData); |
63 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
64 | 0 | return rv; |
65 | 0 | } |
66 | 0 | return NS_OK; |
67 | 0 | } |
68 | | |
69 | | /* BasicCardResponseData */ |
70 | | |
71 | | NS_IMPL_ISUPPORTS_INHERITED(BasicCardResponseData, |
72 | | PaymentResponseData, |
73 | | nsIBasicCardResponseData) |
74 | | |
75 | | BasicCardResponseData::BasicCardResponseData() |
76 | 0 | { |
77 | 0 | Init(nsIPaymentResponseData::BASICCARD_RESPONSE); |
78 | 0 | } |
79 | | |
80 | | NS_IMETHODIMP |
81 | | BasicCardResponseData::GetData(nsAString& aData) |
82 | 0 | { |
83 | 0 | aData = mData; |
84 | 0 | return NS_OK; |
85 | 0 | } |
86 | | |
87 | | NS_IMETHODIMP |
88 | | BasicCardResponseData::InitData(const nsAString& aCardholderName, |
89 | | const nsAString& aCardNumber, |
90 | | const nsAString& aExpiryMonth, |
91 | | const nsAString& aExpiryYear, |
92 | | const nsAString& aCardSecurityCode, |
93 | | nsIPaymentAddress* aBillingAddress) |
94 | 0 | { |
95 | 0 | // cardNumber is a required attribute, cannot be empty; |
96 | 0 | if (aCardNumber.IsEmpty()) { |
97 | 0 | return NS_ERROR_FAILURE; |
98 | 0 | } |
99 | 0 | |
100 | 0 | RefPtr<BasicCardService> service = BasicCardService::GetService(); |
101 | 0 | MOZ_ASSERT(service); |
102 | 0 |
|
103 | 0 | if (!service->IsValidExpiryMonth(aExpiryMonth)) { |
104 | 0 | return NS_ERROR_FAILURE; |
105 | 0 | } |
106 | 0 | |
107 | 0 | if (!service->IsValidExpiryYear(aExpiryYear)) { |
108 | 0 | return NS_ERROR_FAILURE; |
109 | 0 | } |
110 | 0 | nsresult rv = service->EncodeBasicCardData(aCardholderName, |
111 | 0 | aCardNumber, |
112 | 0 | aExpiryMonth, |
113 | 0 | aExpiryYear, |
114 | 0 | aCardSecurityCode, |
115 | 0 | aBillingAddress, |
116 | 0 | mData); |
117 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
118 | 0 | return rv; |
119 | 0 | } |
120 | 0 | return NS_OK; |
121 | 0 | } |
122 | | |
123 | | /* PaymentActionResponse */ |
124 | | |
125 | | NS_IMPL_ISUPPORTS(PaymentActionResponse, |
126 | | nsIPaymentActionResponse) |
127 | | |
128 | | PaymentActionResponse::PaymentActionResponse() |
129 | | : mRequestId(EmptyString()) |
130 | | , mType(nsIPaymentActionResponse::NO_TYPE) |
131 | 0 | { |
132 | 0 | } |
133 | | |
134 | | NS_IMETHODIMP |
135 | | PaymentActionResponse::GetRequestId(nsAString& aRequestId) |
136 | 0 | { |
137 | 0 | aRequestId = mRequestId; |
138 | 0 | return NS_OK; |
139 | 0 | } |
140 | | |
141 | | NS_IMETHODIMP |
142 | | PaymentActionResponse::GetType(uint32_t* aType) |
143 | 0 | { |
144 | 0 | NS_ENSURE_ARG_POINTER(aType); |
145 | 0 | *aType = mType; |
146 | 0 | return NS_OK; |
147 | 0 | } |
148 | | |
149 | | /* PaymentCanMakeActionResponse */ |
150 | | |
151 | | NS_IMPL_ISUPPORTS_INHERITED(PaymentCanMakeActionResponse, |
152 | | PaymentActionResponse, |
153 | | nsIPaymentCanMakeActionResponse) |
154 | | |
155 | | PaymentCanMakeActionResponse::PaymentCanMakeActionResponse() |
156 | | : mResult(false) |
157 | 0 | { |
158 | 0 | mType = nsIPaymentActionResponse::CANMAKE_ACTION; |
159 | 0 | } |
160 | | |
161 | | NS_IMETHODIMP |
162 | | PaymentCanMakeActionResponse::GetResult(bool* aResult) |
163 | 0 | { |
164 | 0 | NS_ENSURE_ARG_POINTER(aResult); |
165 | 0 | *aResult = mResult; |
166 | 0 | return NS_OK; |
167 | 0 | } |
168 | | |
169 | | NS_IMETHODIMP |
170 | | PaymentCanMakeActionResponse::Init(const nsAString& aRequestId, const bool aResult) |
171 | 0 | { |
172 | 0 | mRequestId = aRequestId; |
173 | 0 | mResult = aResult; |
174 | 0 | return NS_OK; |
175 | 0 | } |
176 | | |
177 | | /* PaymentShowActionResponse */ |
178 | | |
179 | | NS_IMPL_ISUPPORTS_INHERITED(PaymentShowActionResponse, |
180 | | PaymentActionResponse, |
181 | | nsIPaymentShowActionResponse) |
182 | | |
183 | | PaymentShowActionResponse::PaymentShowActionResponse() |
184 | | : mAcceptStatus(nsIPaymentActionResponse::PAYMENT_REJECTED) |
185 | 0 | { |
186 | 0 | mType = nsIPaymentActionResponse::SHOW_ACTION; |
187 | 0 | } |
188 | | |
189 | | NS_IMETHODIMP |
190 | | PaymentShowActionResponse::GetAcceptStatus(uint32_t* aAcceptStatus) |
191 | 0 | { |
192 | 0 | NS_ENSURE_ARG_POINTER(aAcceptStatus); |
193 | 0 | *aAcceptStatus = mAcceptStatus; |
194 | 0 | return NS_OK; |
195 | 0 | } |
196 | | |
197 | | NS_IMETHODIMP |
198 | | PaymentShowActionResponse::GetMethodName(nsAString& aMethodName) |
199 | 0 | { |
200 | 0 | aMethodName = mMethodName; |
201 | 0 | return NS_OK; |
202 | 0 | } |
203 | | |
204 | | NS_IMETHODIMP |
205 | | PaymentShowActionResponse::GetData(nsAString& aData) |
206 | 0 | { |
207 | 0 | aData = mData; |
208 | 0 | return NS_OK; |
209 | 0 | } |
210 | | |
211 | | NS_IMETHODIMP |
212 | | PaymentShowActionResponse::GetPayerName(nsAString& aPayerName) |
213 | 0 | { |
214 | 0 | aPayerName = mPayerName; |
215 | 0 | return NS_OK; |
216 | 0 | } |
217 | | |
218 | | NS_IMETHODIMP |
219 | | PaymentShowActionResponse::GetPayerEmail(nsAString& aPayerEmail) |
220 | 0 | { |
221 | 0 | aPayerEmail = mPayerEmail; |
222 | 0 | return NS_OK; |
223 | 0 | } |
224 | | |
225 | | NS_IMETHODIMP |
226 | | PaymentShowActionResponse::GetPayerPhone(nsAString& aPayerPhone) |
227 | 0 | { |
228 | 0 | aPayerPhone = mPayerPhone; |
229 | 0 | return NS_OK; |
230 | 0 | } |
231 | | |
232 | | NS_IMETHODIMP |
233 | | PaymentShowActionResponse::Init(const nsAString& aRequestId, |
234 | | const uint32_t aAcceptStatus, |
235 | | const nsAString& aMethodName, |
236 | | nsIPaymentResponseData* aData, |
237 | | const nsAString& aPayerName, |
238 | | const nsAString& aPayerEmail, |
239 | | const nsAString& aPayerPhone) |
240 | 0 | { |
241 | 0 | if (aAcceptStatus == nsIPaymentActionResponse::PAYMENT_ACCEPTED) { |
242 | 0 | NS_ENSURE_ARG_POINTER(aData); |
243 | 0 | } |
244 | 0 |
|
245 | 0 | mRequestId = aRequestId; |
246 | 0 | mAcceptStatus = aAcceptStatus; |
247 | 0 | mMethodName = aMethodName; |
248 | 0 |
|
249 | 0 | RefPtr<BasicCardService> service = BasicCardService::GetService(); |
250 | 0 | MOZ_ASSERT(service); |
251 | 0 | bool isBasicCardPayment = service->IsBasicCardPayment(mMethodName); |
252 | 0 |
|
253 | 0 | if (aAcceptStatus == nsIPaymentActionResponse::PAYMENT_ACCEPTED) { |
254 | 0 | uint32_t responseType; |
255 | 0 | NS_ENSURE_SUCCESS(aData->GetType(&responseType), NS_ERROR_FAILURE); |
256 | 0 | switch (responseType) { |
257 | 0 | case nsIPaymentResponseData::GENERAL_RESPONSE: { |
258 | 0 | if (isBasicCardPayment) { |
259 | 0 | return NS_ERROR_FAILURE; |
260 | 0 | } |
261 | 0 | nsCOMPtr<nsIGeneralResponseData> data = do_QueryInterface(aData); |
262 | 0 | MOZ_ASSERT(data); |
263 | 0 | NS_ENSURE_SUCCESS(data->GetData(mData), NS_ERROR_FAILURE); |
264 | 0 | break; |
265 | 0 | } |
266 | 0 | case nsIPaymentResponseData::BASICCARD_RESPONSE: { |
267 | 0 | if (!isBasicCardPayment) { |
268 | 0 | return NS_ERROR_FAILURE; |
269 | 0 | } |
270 | 0 | nsCOMPtr<nsIBasicCardResponseData> data = do_QueryInterface(aData); |
271 | 0 | MOZ_ASSERT(data); |
272 | 0 | NS_ENSURE_SUCCESS(data->GetData(mData), NS_ERROR_FAILURE); |
273 | 0 | break; |
274 | 0 | } |
275 | 0 | default: { |
276 | 0 | return NS_ERROR_FAILURE; |
277 | 0 | } |
278 | 0 | } |
279 | 0 | if (mData.IsEmpty()) { |
280 | 0 | return NS_ERROR_FAILURE; |
281 | 0 | } |
282 | 0 | } |
283 | 0 | mPayerName = aPayerName; |
284 | 0 | mPayerEmail = aPayerEmail; |
285 | 0 | mPayerPhone = aPayerPhone; |
286 | 0 | return NS_OK; |
287 | 0 | } |
288 | | |
289 | | /* PaymentAbortActionResponse */ |
290 | | |
291 | | NS_IMPL_ISUPPORTS_INHERITED(PaymentAbortActionResponse, |
292 | | PaymentActionResponse, |
293 | | nsIPaymentAbortActionResponse) |
294 | | |
295 | | PaymentAbortActionResponse::PaymentAbortActionResponse() |
296 | | : mAbortStatus(nsIPaymentActionResponse::ABORT_FAILED) |
297 | 0 | { |
298 | 0 | mType = nsIPaymentActionResponse::ABORT_ACTION; |
299 | 0 | } |
300 | | |
301 | | NS_IMETHODIMP |
302 | | PaymentAbortActionResponse::GetAbortStatus(uint32_t* aAbortStatus) |
303 | 0 | { |
304 | 0 | NS_ENSURE_ARG_POINTER(aAbortStatus); |
305 | 0 | *aAbortStatus = mAbortStatus; |
306 | 0 | return NS_OK; |
307 | 0 | } |
308 | | |
309 | | NS_IMETHODIMP |
310 | | PaymentAbortActionResponse::Init(const nsAString& aRequestId, |
311 | | const uint32_t aAbortStatus) |
312 | 0 | { |
313 | 0 | mRequestId = aRequestId; |
314 | 0 | mAbortStatus = aAbortStatus; |
315 | 0 | return NS_OK; |
316 | 0 | } |
317 | | |
318 | | NS_IMETHODIMP |
319 | | PaymentAbortActionResponse::IsSucceeded(bool* aIsSucceeded) |
320 | 0 | { |
321 | 0 | NS_ENSURE_ARG_POINTER(aIsSucceeded); |
322 | 0 | *aIsSucceeded = (mAbortStatus == nsIPaymentActionResponse::ABORT_SUCCEEDED); |
323 | 0 | return NS_OK; |
324 | 0 | } |
325 | | |
326 | | /* PaymentCompleteActionResponse */ |
327 | | |
328 | | NS_IMPL_ISUPPORTS_INHERITED(PaymentCompleteActionResponse, |
329 | | PaymentActionResponse, |
330 | | nsIPaymentCompleteActionResponse) |
331 | | |
332 | | PaymentCompleteActionResponse::PaymentCompleteActionResponse() |
333 | | : mCompleteStatus(nsIPaymentActionResponse::COMPLETE_FAILED) |
334 | 0 | { |
335 | 0 | mType = nsIPaymentActionResponse::COMPLETE_ACTION; |
336 | 0 | } |
337 | | |
338 | | nsresult |
339 | | PaymentCompleteActionResponse::Init(const nsAString& aRequestId, |
340 | | const uint32_t aCompleteStatus) |
341 | 0 | { |
342 | 0 | mRequestId = aRequestId; |
343 | 0 | mCompleteStatus = aCompleteStatus; |
344 | 0 | return NS_OK; |
345 | 0 | } |
346 | | |
347 | | nsresult |
348 | | PaymentCompleteActionResponse::GetCompleteStatus(uint32_t* aCompleteStatus) |
349 | 0 | { |
350 | 0 | NS_ENSURE_ARG_POINTER(aCompleteStatus); |
351 | 0 | *aCompleteStatus = mCompleteStatus; |
352 | 0 | return NS_OK; |
353 | 0 | } |
354 | | |
355 | | nsresult |
356 | | PaymentCompleteActionResponse::IsCompleted(bool* aIsCompleted) |
357 | 0 | { |
358 | 0 | NS_ENSURE_ARG_POINTER(aIsCompleted); |
359 | 0 | *aIsCompleted = (mCompleteStatus == nsIPaymentActionResponse::COMPLETE_SUCCEEDED); |
360 | 0 | return NS_OK; |
361 | 0 | } |
362 | | |
363 | | } // end of namespace dom |
364 | | } // end of namespace mozilla |