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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "PaymentActionResponse.h"
#include "BasicCardPayment.h"
#include "PaymentRequestUtils.h"
namespace mozilla::dom {
/* PaymentResponseData */
NS_IMPL_ISUPPORTS(PaymentResponseData, nsIPaymentResponseData)
NS_IMETHODIMP
PaymentResponseData::GetType(uint32_t* aType) {
NS_ENSURE_ARG_POINTER(aType);
*aType = mType;
return NS_OK;
}
NS_IMETHODIMP
PaymentResponseData::Init(const uint32_t aType) {
if (aType != nsIPaymentResponseData::GENERAL_RESPONSE &&
aType != nsIPaymentResponseData::BASICCARD_RESPONSE) {
return NS_ERROR_FAILURE;
}
mType = aType;
return NS_OK;
}
/* GeneralResponseData */
NS_IMPL_ISUPPORTS_INHERITED(GeneralResponseData, PaymentResponseData,
nsIGeneralResponseData)
GeneralResponseData::GeneralResponseData() : mData(u"{}"_ns) {
Init(nsIPaymentResponseData::GENERAL_RESPONSE);
}
NS_IMETHODIMP
GeneralResponseData::GetData(nsAString& aData) {
aData = mData;
return NS_OK;
}
NS_IMETHODIMP
GeneralResponseData::InitData(JS::HandleValue aValue, JSContext* aCx) {
if (aValue.isNullOrUndefined()) {
return NS_ERROR_FAILURE;
}
nsresult rv = SerializeFromJSVal(aCx, aValue, mData);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/* BasicCardResponseData */
NS_IMPL_ISUPPORTS_INHERITED(BasicCardResponseData, PaymentResponseData,
nsIBasicCardResponseData)
BasicCardResponseData::BasicCardResponseData() {
Init(nsIPaymentResponseData::BASICCARD_RESPONSE);
}
NS_IMETHODIMP
BasicCardResponseData::GetCardholderName(nsAString& aCardholderName) {
aCardholderName = mCardholderName;
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::GetCardNumber(nsAString& aCardNumber) {
aCardNumber = mCardNumber;
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::GetExpiryMonth(nsAString& aExpiryMonth) {
aExpiryMonth = mExpiryMonth;
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::GetExpiryYear(nsAString& aExpiryYear) {
aExpiryYear = mExpiryYear;
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::GetCardSecurityCode(nsAString& aCardSecurityCode) {
aCardSecurityCode = mCardSecurityCode;
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::GetBillingAddress(nsIPaymentAddress** aBillingAddress) {
NS_ENSURE_ARG_POINTER(aBillingAddress);
nsCOMPtr<nsIPaymentAddress> address;
address = mBillingAddress;
address.forget(aBillingAddress);
return NS_OK;
}
NS_IMETHODIMP
BasicCardResponseData::InitData(const nsAString& aCardholderName,
const nsAString& aCardNumber,
const nsAString& aExpiryMonth,
const nsAString& aExpiryYear,
const nsAString& aCardSecurityCode,
nsIPaymentAddress* aBillingAddress) {
// cardNumber is a required attribute, cannot be empty;
if (aCardNumber.IsEmpty()) {
return NS_ERROR_FAILURE;
}
RefPtr<BasicCardService> service = BasicCardService::GetService();
MOZ_ASSERT(service);
if (!service->IsValidExpiryMonth(aExpiryMonth)) {
return NS_ERROR_FAILURE;
}
if (!service->IsValidExpiryYear(aExpiryYear)) {
return NS_ERROR_FAILURE;
}
mCardholderName = aCardholderName;
mCardNumber = aCardNumber;
mExpiryMonth = aExpiryMonth;
mExpiryYear = aExpiryYear;
mCardSecurityCode = aCardSecurityCode;
mBillingAddress = aBillingAddress;
return NS_OK;
}
/* PaymentActionResponse */
NS_IMPL_ISUPPORTS(PaymentActionResponse, nsIPaymentActionResponse)
PaymentActionResponse::PaymentActionResponse()
: mRequestId(u""_ns), mType(nsIPaymentActionResponse::NO_TYPE) {}
NS_IMETHODIMP
PaymentActionResponse::GetRequestId(nsAString& aRequestId) {
aRequestId = mRequestId;
return NS_OK;
}
NS_IMETHODIMP
PaymentActionResponse::GetType(uint32_t* aType) {
NS_ENSURE_ARG_POINTER(aType);
*aType = mType;
return NS_OK;
}
/* PaymentCanMakeActionResponse */
NS_IMPL_ISUPPORTS_INHERITED(PaymentCanMakeActionResponse, PaymentActionResponse,
nsIPaymentCanMakeActionResponse)
PaymentCanMakeActionResponse::PaymentCanMakeActionResponse() : mResult(false) {
mType = nsIPaymentActionResponse::CANMAKE_ACTION;
}
NS_IMETHODIMP
PaymentCanMakeActionResponse::GetResult(bool* aResult) {
NS_ENSURE_ARG_POINTER(aResult);
*aResult = mResult;
return NS_OK;
}
NS_IMETHODIMP
PaymentCanMakeActionResponse::Init(const nsAString& aRequestId,
const bool aResult) {
mRequestId = aRequestId;
mResult = aResult;
return NS_OK;
}
/* PaymentShowActionResponse */
NS_IMPL_ISUPPORTS_INHERITED(PaymentShowActionResponse, PaymentActionResponse,
nsIPaymentShowActionResponse)
PaymentShowActionResponse::PaymentShowActionResponse()
: mAcceptStatus(nsIPaymentActionResponse::PAYMENT_REJECTED) {
mType = nsIPaymentActionResponse::SHOW_ACTION;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetAcceptStatus(uint32_t* aAcceptStatus) {
NS_ENSURE_ARG_POINTER(aAcceptStatus);
*aAcceptStatus = mAcceptStatus;
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetMethodName(nsAString& aMethodName) {
aMethodName = mMethodName;
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetData(nsIPaymentResponseData** aData) {
NS_ENSURE_ARG_POINTER(aData);
nsCOMPtr<nsIPaymentResponseData> data = mData;
data.forget(aData);
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetPayerName(nsAString& aPayerName) {
aPayerName = mPayerName;
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetPayerEmail(nsAString& aPayerEmail) {
aPayerEmail = mPayerEmail;
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::GetPayerPhone(nsAString& aPayerPhone) {
aPayerPhone = mPayerPhone;
return NS_OK;
}
NS_IMETHODIMP
PaymentShowActionResponse::Init(const nsAString& aRequestId,
const uint32_t aAcceptStatus,
const nsAString& aMethodName,
nsIPaymentResponseData* aData,
const nsAString& aPayerName,
const nsAString& aPayerEmail,
const nsAString& aPayerPhone) {
if (aAcceptStatus == nsIPaymentActionResponse::PAYMENT_ACCEPTED) {
NS_ENSURE_ARG_POINTER(aData);
}
mRequestId = aRequestId;
mAcceptStatus = aAcceptStatus;
mMethodName = aMethodName;
RefPtr<BasicCardService> service = BasicCardService::GetService();
MOZ_ASSERT(service);
bool isBasicCardPayment = service->IsBasicCardPayment(mMethodName);
if (aAcceptStatus == nsIPaymentActionResponse::PAYMENT_ACCEPTED) {
uint32_t responseType;
NS_ENSURE_SUCCESS(aData->GetType(&responseType), NS_ERROR_FAILURE);
switch (responseType) {
case nsIPaymentResponseData::GENERAL_RESPONSE: {
if (isBasicCardPayment) {
return NS_ERROR_FAILURE;
}
break;
}
case nsIPaymentResponseData::BASICCARD_RESPONSE: {
if (!isBasicCardPayment) {
return NS_ERROR_FAILURE;
}
break;
}
default: {
return NS_ERROR_FAILURE;
}
}
}
mData = aData;
mPayerName = aPayerName;
mPayerEmail = aPayerEmail;
mPayerPhone = aPayerPhone;
return NS_OK;
}
/* PaymentAbortActionResponse */
NS_IMPL_ISUPPORTS_INHERITED(PaymentAbortActionResponse, PaymentActionResponse,
nsIPaymentAbortActionResponse)
PaymentAbortActionResponse::PaymentAbortActionResponse()
: mAbortStatus(nsIPaymentActionResponse::ABORT_FAILED) {
mType = nsIPaymentActionResponse::ABORT_ACTION;
}
NS_IMETHODIMP
PaymentAbortActionResponse::GetAbortStatus(uint32_t* aAbortStatus) {
NS_ENSURE_ARG_POINTER(aAbortStatus);
*aAbortStatus = mAbortStatus;
return NS_OK;
}
NS_IMETHODIMP
PaymentAbortActionResponse::Init(const nsAString& aRequestId,
const uint32_t aAbortStatus) {
mRequestId = aRequestId;
mAbortStatus = aAbortStatus;
return NS_OK;
}
NS_IMETHODIMP
PaymentAbortActionResponse::IsSucceeded(bool* aIsSucceeded) {
NS_ENSURE_ARG_POINTER(aIsSucceeded);
*aIsSucceeded = (mAbortStatus == nsIPaymentActionResponse::ABORT_SUCCEEDED);
return NS_OK;
}
/* PaymentCompleteActionResponse */
NS_IMPL_ISUPPORTS_INHERITED(PaymentCompleteActionResponse,
PaymentActionResponse,
nsIPaymentCompleteActionResponse)
PaymentCompleteActionResponse::PaymentCompleteActionResponse()
: mCompleteStatus(nsIPaymentActionResponse::COMPLETE_FAILED) {
mType = nsIPaymentActionResponse::COMPLETE_ACTION;
}
nsresult PaymentCompleteActionResponse::Init(const nsAString& aRequestId,
const uint32_t aCompleteStatus) {
mRequestId = aRequestId;
mCompleteStatus = aCompleteStatus;
return NS_OK;
}
nsresult PaymentCompleteActionResponse::GetCompleteStatus(
uint32_t* aCompleteStatus) {
NS_ENSURE_ARG_POINTER(aCompleteStatus);
*aCompleteStatus = mCompleteStatus;
return NS_OK;
}
nsresult PaymentCompleteActionResponse::IsCompleted(bool* aIsCompleted) {
NS_ENSURE_ARG_POINTER(aIsCompleted);
*aIsCompleted =
(mCompleteStatus == nsIPaymentActionResponse::COMPLETE_SUCCEEDED);
return NS_OK;
}
/* PaymentChangeDetails */
NS_IMPL_ISUPPORTS(MethodChangeDetails, nsIMethodChangeDetails)
NS_IMETHODIMP
MethodChangeDetails::GetType(uint32_t* aType) {
NS_ENSURE_ARG_POINTER(aType);
*aType = mType;
return NS_OK;
}
NS_IMETHODIMP
MethodChangeDetails::Init(const uint32_t aType) {
if (aType != nsIMethodChangeDetails::GENERAL_DETAILS &&
aType != nsIMethodChangeDetails::BASICCARD_DETAILS) {
return NS_ERROR_FAILURE;
}
mType = aType;
return NS_OK;
}
/* GeneralMethodChangeDetails */
NS_IMPL_ISUPPORTS_INHERITED(GeneralMethodChangeDetails, MethodChangeDetails,
nsIGeneralChangeDetails)
GeneralMethodChangeDetails::GeneralMethodChangeDetails() : mDetails(u"{}"_ns) {
Init(nsIMethodChangeDetails::GENERAL_DETAILS);
}
NS_IMETHODIMP
GeneralMethodChangeDetails::GetDetails(nsAString& aDetails) {
aDetails = mDetails;
return NS_OK;
}
NS_IMETHODIMP
GeneralMethodChangeDetails::InitData(JS::HandleValue aDetails, JSContext* aCx) {
if (aDetails.isNullOrUndefined()) {
return NS_ERROR_FAILURE;
}
nsresult rv = SerializeFromJSVal(aCx, aDetails, mDetails);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/* BasicCardMethodChangeDetails */
NS_IMPL_ISUPPORTS_INHERITED(BasicCardMethodChangeDetails, MethodChangeDetails,
nsIBasicCardChangeDetails)
BasicCardMethodChangeDetails::BasicCardMethodChangeDetails() {
Init(nsIMethodChangeDetails::BASICCARD_DETAILS);
}
NS_IMETHODIMP
BasicCardMethodChangeDetails::GetBillingAddress(
nsIPaymentAddress** aBillingAddress) {
NS_ENSURE_ARG_POINTER(aBillingAddress);
nsCOMPtr<nsIPaymentAddress> address;
address = mBillingAddress;
address.forget(aBillingAddress);
return NS_OK;
}
NS_IMETHODIMP
BasicCardMethodChangeDetails::InitData(nsIPaymentAddress* aBillingAddress) {
mBillingAddress = aBillingAddress;
return NS_OK;
}
} // namespace mozilla::dom
|