PaymentResponse.h (5774B)
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 #ifndef mozilla_dom_PaymentResponse_h 8 #define mozilla_dom_PaymentResponse_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/dom/PaymentResponseBinding.h" // PaymentComplete 12 #include "nsITimer.h" 13 #include "nsPIDOMWindow.h" 14 15 namespace mozilla::dom { 16 17 class PaymentAddress; 18 class PaymentRequest; 19 struct PaymentValidationErrors; 20 class Promise; 21 22 class GeneralData final { 23 public: 24 GeneralData() = default; 25 ~GeneralData() = default; 26 nsString data; 27 }; 28 29 class BasicCardData final { 30 public: 31 struct Address { 32 nsString country; 33 CopyableTArray<nsString> addressLine; 34 nsString region; 35 nsString regionCode; 36 nsString city; 37 nsString dependentLocality; 38 nsString postalCode; 39 nsString sortingCode; 40 nsString organization; 41 nsString recipient; 42 nsString phone; 43 }; 44 BasicCardData() = default; 45 ~BasicCardData() = default; 46 47 nsString cardholderName; 48 nsString cardNumber; 49 nsString expiryMonth; 50 nsString expiryYear; 51 nsString cardSecurityCode; 52 Address billingAddress; 53 }; 54 55 class ResponseData final { 56 public: 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(); 67 return *this; 68 } 69 ResponseData& operator=(const BasicCardData& aBasicCardData) { 70 mType = BasicCardResponse; 71 mGeneralData = GeneralData(); 72 mBasicCardData = aBasicCardData; 73 return *this; 74 } 75 ~ResponseData() = default; 76 77 const Type& type() const { return mType; } 78 const GeneralData& generalData() const { return mGeneralData; } 79 const BasicCardData& basicCardData() const { return mBasicCardData; } 80 81 private: 82 Type mType; 83 GeneralData mGeneralData; 84 BasicCardData mBasicCardData; 85 }; 86 87 class PaymentResponse final : public DOMEventTargetHelper, 88 public nsITimerCallback { 89 public: 90 NS_DECL_ISUPPORTS_INHERITED 91 92 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentResponse, 93 DOMEventTargetHelper) 94 95 NS_IMETHOD Notify(nsITimer* aTimer) override; 96 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); 103 104 virtual JSObject* WrapObject(JSContext* aCx, 105 JS::Handle<JSObject*> aGivenProto) override; 106 107 void GetRequestId(nsString& aRetVal) const; 108 109 void GetMethodName(nsString& aRetVal) const; 110 111 void GetDetails(JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const; 112 113 already_AddRefed<PaymentAddress> GetShippingAddress() const; 114 115 void GetShippingOption(nsString& aRetVal) const; 116 117 void GetPayerName(nsString& aRetVal) const; 118 119 void GetPayerEmail(nsString& aRetVal) const; 120 121 void GetPayerPhone(nsString& aRetVal) const; 122 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); 126 127 void RespondComplete(); 128 129 IMPL_EVENT_HANDLER(payerdetailchange); 130 131 nsresult UpdatePayerDetail(const nsAString& aPayerName, 132 const nsAString& aPayerEmail, 133 const nsAString& aPayerPhone); 134 135 already_AddRefed<Promise> Retry(JSContext* aCx, 136 const PaymentValidationErrors& errorField, 137 ErrorResult& aRv); 138 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); 145 146 protected: 147 ~PaymentResponse(); 148 149 void ValidatePaymentValidationErrors(const PaymentValidationErrors& aErrors, 150 ErrorResult& aRv); 151 152 void ConvertPaymentMethodErrors(JSContext* aCx, 153 const PaymentValidationErrors& aErrors, 154 ErrorResult& aRv) const; 155 156 nsresult DispatchUpdateEvent(const nsAString& aType); 157 158 private: 159 bool mCompleteCalled; 160 PaymentRequest* mRequest; 161 nsString mRequestId; 162 nsString mMethodName; 163 ResponseData mDetails; 164 nsString mShippingOption; 165 nsString mPayerName; 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 172 // complete() 173 nsCOMPtr<nsITimer> mTimer; 174 // Promise for "PaymentResponse::Retry" 175 RefPtr<Promise> mRetryPromise; 176 }; 177 178 } // namespace mozilla::dom 179 180 #endif // mozilla_dom_PaymentResponse_h