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 #ifndef mozilla_dom_PaymentResponse_h
8 #define mozilla_dom_PaymentResponse_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/PaymentResponseBinding.h" // PaymentComplete
12 #include "nsPIDOMWindow.h"
15 namespace mozilla::dom
{
19 struct PaymentValidationErrors
;
22 class GeneralData final
{
24 GeneralData() = default;
25 ~GeneralData() = default;
29 class BasicCardData final
{
33 CopyableTArray
<nsString
> addressLine
;
37 nsString dependentLocality
;
40 nsString organization
;
44 BasicCardData() = default;
45 ~BasicCardData() = default;
47 nsString cardholderName
;
51 nsString cardSecurityCode
;
52 Address billingAddress
;
55 class ResponseData final
{
57 enum Type
{ Unknown
= 0, GeneralResponse
= 1, BasicCardResponse
};
58 ResponseData() : mType(ResponseData::Unknown
) {}
59 explicit ResponseData(const GeneralData
& aGeneralData
)
60 : mType(GeneralResponse
), mGeneralData(aGeneralData
) {}
61 explicit ResponseData(const BasicCardData
& aBasicCardData
)
62 : mType(BasicCardResponse
), mBasicCardData(aBasicCardData
) {}
63 ResponseData
& operator=(const GeneralData
& aGeneralData
) {
64 mType
= GeneralResponse
;
65 mGeneralData
= aGeneralData
;
66 mBasicCardData
= BasicCardData();
69 ResponseData
& operator=(const BasicCardData
& aBasicCardData
) {
70 mType
= BasicCardResponse
;
71 mGeneralData
= GeneralData();
72 mBasicCardData
= aBasicCardData
;
75 ~ResponseData() = default;
77 const Type
& type() const { return mType
; }
78 const GeneralData
& generalData() const { return mGeneralData
; }
79 const BasicCardData
& basicCardData() const { return mBasicCardData
; }
83 GeneralData mGeneralData
;
84 BasicCardData mBasicCardData
;
87 class PaymentResponse final
: public DOMEventTargetHelper
,
88 public nsITimerCallback
{
90 NS_DECL_ISUPPORTS_INHERITED
92 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentResponse
,
95 NS_IMETHOD
Notify(nsITimer
* aTimer
) override
;
97 PaymentResponse(nsPIDOMWindowInner
* aWindow
, PaymentRequest
* aRequest
,
98 const nsAString
& aRequestId
, const nsAString
& aMethodName
,
99 const nsAString
& aShippingOption
,
100 PaymentAddress
* aShippingAddress
,
101 const ResponseData
& aDetails
, const nsAString
& aPayerName
,
102 const nsAString
& aPayerEmail
, const nsAString
& aPayerPhone
);
104 virtual JSObject
* WrapObject(JSContext
* aCx
,
105 JS::Handle
<JSObject
*> aGivenProto
) override
;
107 void GetRequestId(nsString
& aRetVal
) const;
109 void GetMethodName(nsString
& aRetVal
) const;
111 void GetDetails(JSContext
* cx
, JS::MutableHandle
<JSObject
*> aRetVal
) const;
113 already_AddRefed
<PaymentAddress
> GetShippingAddress() const;
115 void GetShippingOption(nsString
& aRetVal
) const;
117 void GetPayerName(nsString
& aRetVal
) const;
119 void GetPayerEmail(nsString
& aRetVal
) const;
121 void GetPayerPhone(nsString
& aRetVal
) const;
123 // Return a raw pointer here to avoid refcounting, but make sure it's safe
124 // (the object should be kept alive by the callee).
125 already_AddRefed
<Promise
> Complete(PaymentComplete result
, ErrorResult
& aRv
);
127 void RespondComplete();
129 IMPL_EVENT_HANDLER(payerdetailchange
);
131 nsresult
UpdatePayerDetail(const nsAString
& aPayerName
,
132 const nsAString
& aPayerEmail
,
133 const nsAString
& aPayerPhone
);
135 already_AddRefed
<Promise
> Retry(JSContext
* aCx
,
136 const PaymentValidationErrors
& errorField
,
139 void RespondRetry(const nsAString
& aMethodName
,
140 const nsAString
& aShippingOption
,
141 PaymentAddress
* aShippingAddress
,
142 const ResponseData
& aDetails
, const nsAString
& aPayerName
,
143 const nsAString
& aPayerEmail
, const nsAString
& aPayerPhone
);
144 void RejectRetry(ErrorResult
&& aRejectReason
);
149 void ValidatePaymentValidationErrors(const PaymentValidationErrors
& aErrors
,
152 void ConvertPaymentMethodErrors(JSContext
* aCx
,
153 const PaymentValidationErrors
& aErrors
,
154 ErrorResult
& aRv
) const;
156 nsresult
DispatchUpdateEvent(const nsAString
& aType
);
159 bool mCompleteCalled
;
160 PaymentRequest
* mRequest
;
162 nsString mMethodName
;
163 ResponseData mDetails
;
164 nsString mShippingOption
;
166 nsString mPayerEmail
;
167 nsString mPayerPhone
;
168 RefPtr
<PaymentAddress
> mShippingAddress
;
169 // Promise for "PaymentResponse::Complete"
170 RefPtr
<Promise
> mPromise
;
171 // Timer for timing out if the page doesn't call
173 nsCOMPtr
<nsITimer
> mTimer
;
174 // Promise for "PaymentResponse::Retry"
175 RefPtr
<Promise
> mRetryPromise
;
178 } // namespace mozilla::dom
180 #endif // mozilla_dom_PaymentResponse_h