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"
20 struct PaymentValidationErrors
;
23 class GeneralData final
{
25 GeneralData() = default;
26 ~GeneralData() = default;
30 class BasicCardData final
{
34 CopyableTArray
<nsString
> addressLine
;
38 nsString dependentLocality
;
41 nsString organization
;
45 BasicCardData() = default;
46 ~BasicCardData() = default;
48 nsString cardholderName
;
52 nsString cardSecurityCode
;
53 Address billingAddress
;
56 class ResponseData final
{
58 enum Type
{ Unknown
= 0, GeneralResponse
= 1, BasicCardResponse
};
59 ResponseData() : mType(ResponseData::Unknown
) {}
60 explicit ResponseData(const GeneralData
& aGeneralData
)
61 : mType(GeneralResponse
), mGeneralData(aGeneralData
) {}
62 explicit ResponseData(const BasicCardData
& aBasicCardData
)
63 : mType(BasicCardResponse
), mBasicCardData(aBasicCardData
) {}
64 ResponseData
& operator=(const GeneralData
& aGeneralData
) {
65 mType
= GeneralResponse
;
66 mGeneralData
= aGeneralData
;
67 mBasicCardData
= BasicCardData();
70 ResponseData
& operator=(const BasicCardData
& aBasicCardData
) {
71 mType
= BasicCardResponse
;
72 mGeneralData
= GeneralData();
73 mBasicCardData
= aBasicCardData
;
76 ~ResponseData() = default;
78 const Type
& type() const { return mType
; }
79 const GeneralData
& generalData() const { return mGeneralData
; }
80 const BasicCardData
& basicCardData() const { return mBasicCardData
; }
84 GeneralData mGeneralData
;
85 BasicCardData mBasicCardData
;
88 class PaymentResponse final
: public DOMEventTargetHelper
,
89 public nsITimerCallback
{
91 NS_DECL_ISUPPORTS_INHERITED
93 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentResponse
,
96 NS_IMETHOD
Notify(nsITimer
* aTimer
) override
;
98 PaymentResponse(nsPIDOMWindowInner
* aWindow
, PaymentRequest
* aRequest
,
99 const nsAString
& aRequestId
, const nsAString
& aMethodName
,
100 const nsAString
& aShippingOption
,
101 PaymentAddress
* aShippingAddress
,
102 const ResponseData
& aDetails
, const nsAString
& aPayerName
,
103 const nsAString
& aPayerEmail
, const nsAString
& aPayerPhone
);
105 virtual JSObject
* WrapObject(JSContext
* aCx
,
106 JS::Handle
<JSObject
*> aGivenProto
) override
;
108 void GetRequestId(nsString
& aRetVal
) const;
110 void GetMethodName(nsString
& aRetVal
) const;
112 void GetDetails(JSContext
* cx
, JS::MutableHandle
<JSObject
*> aRetVal
) const;
114 already_AddRefed
<PaymentAddress
> GetShippingAddress() const;
116 void GetShippingOption(nsString
& aRetVal
) const;
118 void GetPayerName(nsString
& aRetVal
) const;
120 void GetPayerEmail(nsString
& aRetVal
) const;
122 void GetPayerPhone(nsString
& aRetVal
) const;
124 // Return a raw pointer here to avoid refcounting, but make sure it's safe
125 // (the object should be kept alive by the callee).
126 already_AddRefed
<Promise
> Complete(PaymentComplete result
, ErrorResult
& aRv
);
128 void RespondComplete();
130 IMPL_EVENT_HANDLER(payerdetailchange
);
132 nsresult
UpdatePayerDetail(const nsAString
& aPayerName
,
133 const nsAString
& aPayerEmail
,
134 const nsAString
& aPayerPhone
);
136 already_AddRefed
<Promise
> Retry(JSContext
* aCx
,
137 const PaymentValidationErrors
& errorField
,
140 void RespondRetry(const nsAString
& aMethodName
,
141 const nsAString
& aShippingOption
,
142 PaymentAddress
* aShippingAddress
,
143 const ResponseData
& aDetails
, const nsAString
& aPayerName
,
144 const nsAString
& aPayerEmail
, const nsAString
& aPayerPhone
);
145 void RejectRetry(ErrorResult
&& aRejectReason
);
150 void ValidatePaymentValidationErrors(const PaymentValidationErrors
& aErrors
,
153 void ConvertPaymentMethodErrors(JSContext
* aCx
,
154 const PaymentValidationErrors
& aErrors
,
155 ErrorResult
& aRv
) const;
157 nsresult
DispatchUpdateEvent(const nsAString
& aType
);
160 bool mCompleteCalled
;
161 PaymentRequest
* mRequest
;
163 nsString mMethodName
;
164 ResponseData mDetails
;
165 nsString mShippingOption
;
167 nsString mPayerEmail
;
168 nsString mPayerPhone
;
169 RefPtr
<PaymentAddress
> mShippingAddress
;
170 // Promise for "PaymentResponse::Complete"
171 RefPtr
<Promise
> mPromise
;
172 // Timer for timing out if the page doesn't call
174 nsCOMPtr
<nsITimer
> mTimer
;
175 // Promise for "PaymentResponse::Retry"
176 RefPtr
<Promise
> mRetryPromise
;
180 } // namespace mozilla
182 #endif // mozilla_dom_PaymentResponse_h