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/. */
7 #include "PaymentActionResponse.h"
8 #include "BasicCardPayment.h"
9 #include "PaymentRequestUtils.h"
11 namespace mozilla::dom
{
13 /* PaymentResponseData */
15 NS_IMPL_ISUPPORTS(PaymentResponseData
, nsIPaymentResponseData
)
18 PaymentResponseData::GetType(uint32_t* aType
) {
19 NS_ENSURE_ARG_POINTER(aType
);
25 PaymentResponseData::Init(const uint32_t aType
) {
26 if (aType
!= nsIPaymentResponseData::GENERAL_RESPONSE
&&
27 aType
!= nsIPaymentResponseData::BASICCARD_RESPONSE
) {
28 return NS_ERROR_FAILURE
;
34 /* GeneralResponseData */
36 NS_IMPL_ISUPPORTS_INHERITED(GeneralResponseData
, PaymentResponseData
,
37 nsIGeneralResponseData
)
39 GeneralResponseData::GeneralResponseData() : mData(u
"{}"_ns
) {
40 Init(nsIPaymentResponseData::GENERAL_RESPONSE
);
44 GeneralResponseData::GetData(nsAString
& aData
) {
50 GeneralResponseData::InitData(JS::HandleValue aValue
, JSContext
* aCx
) {
51 if (aValue
.isNullOrUndefined()) {
52 return NS_ERROR_FAILURE
;
54 nsresult rv
= SerializeFromJSVal(aCx
, aValue
, mData
);
55 if (NS_WARN_IF(NS_FAILED(rv
))) {
61 /* BasicCardResponseData */
63 NS_IMPL_ISUPPORTS_INHERITED(BasicCardResponseData
, PaymentResponseData
,
64 nsIBasicCardResponseData
)
66 BasicCardResponseData::BasicCardResponseData() {
67 Init(nsIPaymentResponseData::BASICCARD_RESPONSE
);
71 BasicCardResponseData::GetCardholderName(nsAString
& aCardholderName
) {
72 aCardholderName
= mCardholderName
;
77 BasicCardResponseData::GetCardNumber(nsAString
& aCardNumber
) {
78 aCardNumber
= mCardNumber
;
83 BasicCardResponseData::GetExpiryMonth(nsAString
& aExpiryMonth
) {
84 aExpiryMonth
= mExpiryMonth
;
89 BasicCardResponseData::GetExpiryYear(nsAString
& aExpiryYear
) {
90 aExpiryYear
= mExpiryYear
;
95 BasicCardResponseData::GetCardSecurityCode(nsAString
& aCardSecurityCode
) {
96 aCardSecurityCode
= mCardSecurityCode
;
101 BasicCardResponseData::GetBillingAddress(nsIPaymentAddress
** aBillingAddress
) {
102 NS_ENSURE_ARG_POINTER(aBillingAddress
);
103 nsCOMPtr
<nsIPaymentAddress
> address
;
104 address
= mBillingAddress
;
105 address
.forget(aBillingAddress
);
110 BasicCardResponseData::InitData(const nsAString
& aCardholderName
,
111 const nsAString
& aCardNumber
,
112 const nsAString
& aExpiryMonth
,
113 const nsAString
& aExpiryYear
,
114 const nsAString
& aCardSecurityCode
,
115 nsIPaymentAddress
* aBillingAddress
) {
116 // cardNumber is a required attribute, cannot be empty;
117 if (aCardNumber
.IsEmpty()) {
118 return NS_ERROR_FAILURE
;
121 RefPtr
<BasicCardService
> service
= BasicCardService::GetService();
124 if (!service
->IsValidExpiryMonth(aExpiryMonth
)) {
125 return NS_ERROR_FAILURE
;
128 if (!service
->IsValidExpiryYear(aExpiryYear
)) {
129 return NS_ERROR_FAILURE
;
132 mCardholderName
= aCardholderName
;
133 mCardNumber
= aCardNumber
;
134 mExpiryMonth
= aExpiryMonth
;
135 mExpiryYear
= aExpiryYear
;
136 mCardSecurityCode
= aCardSecurityCode
;
137 mBillingAddress
= aBillingAddress
;
142 /* PaymentActionResponse */
144 NS_IMPL_ISUPPORTS(PaymentActionResponse
, nsIPaymentActionResponse
)
146 PaymentActionResponse::PaymentActionResponse()
147 : mRequestId(u
""_ns
), mType(nsIPaymentActionResponse::NO_TYPE
) {}
150 PaymentActionResponse::GetRequestId(nsAString
& aRequestId
) {
151 aRequestId
= mRequestId
;
156 PaymentActionResponse::GetType(uint32_t* aType
) {
157 NS_ENSURE_ARG_POINTER(aType
);
162 /* PaymentCanMakeActionResponse */
164 NS_IMPL_ISUPPORTS_INHERITED(PaymentCanMakeActionResponse
, PaymentActionResponse
,
165 nsIPaymentCanMakeActionResponse
)
167 PaymentCanMakeActionResponse::PaymentCanMakeActionResponse() : mResult(false) {
168 mType
= nsIPaymentActionResponse::CANMAKE_ACTION
;
172 PaymentCanMakeActionResponse::GetResult(bool* aResult
) {
173 NS_ENSURE_ARG_POINTER(aResult
);
179 PaymentCanMakeActionResponse::Init(const nsAString
& aRequestId
,
180 const bool aResult
) {
181 mRequestId
= aRequestId
;
186 /* PaymentShowActionResponse */
188 NS_IMPL_ISUPPORTS_INHERITED(PaymentShowActionResponse
, PaymentActionResponse
,
189 nsIPaymentShowActionResponse
)
191 PaymentShowActionResponse::PaymentShowActionResponse()
192 : mAcceptStatus(nsIPaymentActionResponse::PAYMENT_REJECTED
) {
193 mType
= nsIPaymentActionResponse::SHOW_ACTION
;
197 PaymentShowActionResponse::GetAcceptStatus(uint32_t* aAcceptStatus
) {
198 NS_ENSURE_ARG_POINTER(aAcceptStatus
);
199 *aAcceptStatus
= mAcceptStatus
;
204 PaymentShowActionResponse::GetMethodName(nsAString
& aMethodName
) {
205 aMethodName
= mMethodName
;
210 PaymentShowActionResponse::GetData(nsIPaymentResponseData
** aData
) {
211 NS_ENSURE_ARG_POINTER(aData
);
212 nsCOMPtr
<nsIPaymentResponseData
> data
= mData
;
218 PaymentShowActionResponse::GetPayerName(nsAString
& aPayerName
) {
219 aPayerName
= mPayerName
;
224 PaymentShowActionResponse::GetPayerEmail(nsAString
& aPayerEmail
) {
225 aPayerEmail
= mPayerEmail
;
230 PaymentShowActionResponse::GetPayerPhone(nsAString
& aPayerPhone
) {
231 aPayerPhone
= mPayerPhone
;
236 PaymentShowActionResponse::Init(const nsAString
& aRequestId
,
237 const uint32_t aAcceptStatus
,
238 const nsAString
& aMethodName
,
239 nsIPaymentResponseData
* aData
,
240 const nsAString
& aPayerName
,
241 const nsAString
& aPayerEmail
,
242 const nsAString
& aPayerPhone
) {
243 if (aAcceptStatus
== nsIPaymentActionResponse::PAYMENT_ACCEPTED
) {
244 NS_ENSURE_ARG_POINTER(aData
);
247 mRequestId
= aRequestId
;
248 mAcceptStatus
= aAcceptStatus
;
249 mMethodName
= aMethodName
;
251 RefPtr
<BasicCardService
> service
= BasicCardService::GetService();
253 bool isBasicCardPayment
= service
->IsBasicCardPayment(mMethodName
);
255 if (aAcceptStatus
== nsIPaymentActionResponse::PAYMENT_ACCEPTED
) {
256 uint32_t responseType
;
257 NS_ENSURE_SUCCESS(aData
->GetType(&responseType
), NS_ERROR_FAILURE
);
258 switch (responseType
) {
259 case nsIPaymentResponseData::GENERAL_RESPONSE
: {
260 if (isBasicCardPayment
) {
261 return NS_ERROR_FAILURE
;
265 case nsIPaymentResponseData::BASICCARD_RESPONSE
: {
266 if (!isBasicCardPayment
) {
267 return NS_ERROR_FAILURE
;
272 return NS_ERROR_FAILURE
;
277 mPayerName
= aPayerName
;
278 mPayerEmail
= aPayerEmail
;
279 mPayerPhone
= aPayerPhone
;
283 /* PaymentAbortActionResponse */
285 NS_IMPL_ISUPPORTS_INHERITED(PaymentAbortActionResponse
, PaymentActionResponse
,
286 nsIPaymentAbortActionResponse
)
288 PaymentAbortActionResponse::PaymentAbortActionResponse()
289 : mAbortStatus(nsIPaymentActionResponse::ABORT_FAILED
) {
290 mType
= nsIPaymentActionResponse::ABORT_ACTION
;
294 PaymentAbortActionResponse::GetAbortStatus(uint32_t* aAbortStatus
) {
295 NS_ENSURE_ARG_POINTER(aAbortStatus
);
296 *aAbortStatus
= mAbortStatus
;
301 PaymentAbortActionResponse::Init(const nsAString
& aRequestId
,
302 const uint32_t aAbortStatus
) {
303 mRequestId
= aRequestId
;
304 mAbortStatus
= aAbortStatus
;
309 PaymentAbortActionResponse::IsSucceeded(bool* aIsSucceeded
) {
310 NS_ENSURE_ARG_POINTER(aIsSucceeded
);
311 *aIsSucceeded
= (mAbortStatus
== nsIPaymentActionResponse::ABORT_SUCCEEDED
);
315 /* PaymentCompleteActionResponse */
317 NS_IMPL_ISUPPORTS_INHERITED(PaymentCompleteActionResponse
,
318 PaymentActionResponse
,
319 nsIPaymentCompleteActionResponse
)
321 PaymentCompleteActionResponse::PaymentCompleteActionResponse()
322 : mCompleteStatus(nsIPaymentActionResponse::COMPLETE_FAILED
) {
323 mType
= nsIPaymentActionResponse::COMPLETE_ACTION
;
326 nsresult
PaymentCompleteActionResponse::Init(const nsAString
& aRequestId
,
327 const uint32_t aCompleteStatus
) {
328 mRequestId
= aRequestId
;
329 mCompleteStatus
= aCompleteStatus
;
333 nsresult
PaymentCompleteActionResponse::GetCompleteStatus(
334 uint32_t* aCompleteStatus
) {
335 NS_ENSURE_ARG_POINTER(aCompleteStatus
);
336 *aCompleteStatus
= mCompleteStatus
;
340 nsresult
PaymentCompleteActionResponse::IsCompleted(bool* aIsCompleted
) {
341 NS_ENSURE_ARG_POINTER(aIsCompleted
);
343 (mCompleteStatus
== nsIPaymentActionResponse::COMPLETE_SUCCEEDED
);
347 /* PaymentChangeDetails */
349 NS_IMPL_ISUPPORTS(MethodChangeDetails
, nsIMethodChangeDetails
)
352 MethodChangeDetails::GetType(uint32_t* aType
) {
353 NS_ENSURE_ARG_POINTER(aType
);
359 MethodChangeDetails::Init(const uint32_t aType
) {
360 if (aType
!= nsIMethodChangeDetails::GENERAL_DETAILS
&&
361 aType
!= nsIMethodChangeDetails::BASICCARD_DETAILS
) {
362 return NS_ERROR_FAILURE
;
368 /* GeneralMethodChangeDetails */
370 NS_IMPL_ISUPPORTS_INHERITED(GeneralMethodChangeDetails
, MethodChangeDetails
,
371 nsIGeneralChangeDetails
)
373 GeneralMethodChangeDetails::GeneralMethodChangeDetails() : mDetails(u
"{}"_ns
) {
374 Init(nsIMethodChangeDetails::GENERAL_DETAILS
);
378 GeneralMethodChangeDetails::GetDetails(nsAString
& aDetails
) {
384 GeneralMethodChangeDetails::InitData(JS::HandleValue aDetails
, JSContext
* aCx
) {
385 if (aDetails
.isNullOrUndefined()) {
386 return NS_ERROR_FAILURE
;
388 nsresult rv
= SerializeFromJSVal(aCx
, aDetails
, mDetails
);
389 if (NS_WARN_IF(NS_FAILED(rv
))) {
395 /* BasicCardMethodChangeDetails */
397 NS_IMPL_ISUPPORTS_INHERITED(BasicCardMethodChangeDetails
, MethodChangeDetails
,
398 nsIBasicCardChangeDetails
)
400 BasicCardMethodChangeDetails::BasicCardMethodChangeDetails() {
401 Init(nsIMethodChangeDetails::BASICCARD_DETAILS
);
405 BasicCardMethodChangeDetails::GetBillingAddress(
406 nsIPaymentAddress
** aBillingAddress
) {
407 NS_ENSURE_ARG_POINTER(aBillingAddress
);
408 nsCOMPtr
<nsIPaymentAddress
> address
;
409 address
= mBillingAddress
;
410 address
.forget(aBillingAddress
);
415 BasicCardMethodChangeDetails::InitData(nsIPaymentAddress
* aBillingAddress
) {
416 mBillingAddress
= aBillingAddress
;
420 } // namespace mozilla::dom