2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2017-05-25 20:50:32 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_PaymentRequest_h
|
|
|
|
#define mozilla_dom_PaymentRequest_h
|
|
|
|
|
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "mozilla/dom/PaymentRequestBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2018-04-02 18:12:21 +03:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2017-05-25 20:50:32 +03:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsWrapperCache.h"
|
2017-06-23 12:15:51 +03:00
|
|
|
#include "PaymentRequestUpdateEvent.h"
|
2017-05-25 20:50:32 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class EventHandlerNonNull;
|
2017-06-14 10:59:00 +03:00
|
|
|
class PaymentAddress;
|
2018-05-31 23:30:29 +03:00
|
|
|
class PaymentRequestChild;
|
2017-05-25 20:50:32 +03:00
|
|
|
class PaymentResponse;
|
|
|
|
|
|
|
|
class PaymentRequest final : public DOMEventTargetHelper
|
2018-04-02 18:12:21 +03:00
|
|
|
, public PromiseNativeHandler
|
2017-05-25 20:50:32 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentRequest, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
|
|
|
static already_AddRefed<PaymentRequest>
|
|
|
|
CreatePaymentRequest(nsPIDOMWindowInner* aWindow, nsresult& aRv);
|
|
|
|
|
|
|
|
static bool PrefEnabled(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
2017-09-11 04:52:24 +03:00
|
|
|
static nsresult IsValidStandardizedPMI(const nsAString& aIdentifier,
|
|
|
|
nsAString& aErrorMsg);
|
|
|
|
|
|
|
|
static nsresult IsValidPaymentMethodIdentifier(const nsAString& aIdentifier,
|
|
|
|
nsAString& aErrorMsg);
|
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult IsValidMethodData(JSContext* aCx,
|
|
|
|
const Sequence<PaymentMethodData>& aMethodData,
|
|
|
|
nsAString& aErrorMsg);
|
2017-06-14 10:59:00 +03:00
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult
|
2017-05-25 20:50:32 +03:00
|
|
|
IsValidNumber(const nsAString& aItem,
|
|
|
|
const nsAString& aStr,
|
|
|
|
nsAString& aErrorMsg);
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult
|
2017-06-21 08:23:24 +03:00
|
|
|
IsNonNegativeNumber(const nsAString& aItem,
|
|
|
|
const nsAString& aStr,
|
|
|
|
nsAString& aErrorMsg);
|
2017-05-25 20:50:32 +03:00
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult
|
|
|
|
IsValidCurrencyAmount(const nsAString& aItem,
|
|
|
|
const PaymentCurrencyAmount& aAmount,
|
|
|
|
const bool aIsTotalItem,
|
|
|
|
nsAString& aErrorMsg);
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
IsValidCurrency(const nsAString& aItem,
|
|
|
|
const nsAString& aCurrency,
|
|
|
|
nsAString& aErrorMsg);
|
|
|
|
|
|
|
|
static nsresult
|
2017-05-25 20:50:32 +03:00
|
|
|
IsValidDetailsInit(const PaymentDetailsInit& aDetails,
|
2017-09-28 10:20:19 +03:00
|
|
|
const bool aRequestShipping,
|
2017-05-25 20:50:32 +03:00
|
|
|
nsAString& aErrorMsg);
|
2017-06-23 12:15:51 +03:00
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult
|
2017-09-28 10:20:19 +03:00
|
|
|
IsValidDetailsUpdate(const PaymentDetailsUpdate& aDetails,
|
|
|
|
const bool aRequestShipping);
|
2017-06-23 12:15:51 +03:00
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
static nsresult
|
2017-05-25 20:50:32 +03:00
|
|
|
IsValidDetailsBase(const PaymentDetailsBase& aDetails,
|
2017-09-28 10:20:19 +03:00
|
|
|
const bool aRequestShipping,
|
2017-05-25 20:50:32 +03:00
|
|
|
nsAString& aErrorMsg);
|
|
|
|
|
|
|
|
static already_AddRefed<PaymentRequest>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<PaymentMethodData>& aMethodData,
|
|
|
|
const PaymentDetailsInit& aDetails,
|
|
|
|
const PaymentOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2017-06-14 10:59:00 +03:00
|
|
|
already_AddRefed<Promise> CanMakePayment(ErrorResult& aRv);
|
|
|
|
void RespondCanMakePayment(bool aResult);
|
|
|
|
|
2018-04-02 18:12:21 +03:00
|
|
|
already_AddRefed<Promise> Show(const Optional<OwningNonNull<Promise>>& detailsPromise,
|
|
|
|
ErrorResult& aRv);
|
2017-08-28 06:55:59 +03:00
|
|
|
void RespondShowPayment(const nsAString& aMethodName,
|
2017-06-14 10:59:00 +03:00
|
|
|
const nsAString& aDetails,
|
|
|
|
const nsAString& aPayerName,
|
|
|
|
const nsAString& aPayerEmail,
|
|
|
|
const nsAString& aPayerPhone,
|
2017-08-28 06:55:59 +03:00
|
|
|
nsresult aRv);
|
2017-06-14 10:59:00 +03:00
|
|
|
void RejectShowPayment(nsresult aRejectReason);
|
|
|
|
void RespondComplete();
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
already_AddRefed<Promise> Abort(ErrorResult& aRv);
|
2017-06-14 10:59:00 +03:00
|
|
|
void RespondAbortPayment(bool aResult);
|
2017-05-25 20:50:32 +03:00
|
|
|
|
|
|
|
void GetId(nsAString& aRetVal) const;
|
|
|
|
void GetInternalId(nsAString& aRetVal);
|
|
|
|
void SetId(const nsAString& aId);
|
|
|
|
|
|
|
|
bool Equals(const nsAString& aInternalId) const;
|
|
|
|
|
2017-06-14 10:59:00 +03:00
|
|
|
bool ReadyForUpdate();
|
2018-04-02 18:12:21 +03:00
|
|
|
bool IsUpdating() const { return mUpdating; }
|
2017-05-25 20:50:32 +03:00
|
|
|
void SetUpdating(bool aUpdating);
|
|
|
|
|
2017-06-14 10:59:00 +03:00
|
|
|
already_AddRefed<PaymentAddress> GetShippingAddress() const;
|
2017-06-23 12:15:51 +03:00
|
|
|
// Update mShippingAddress and fire shippingaddresschange event
|
|
|
|
nsresult UpdateShippingAddress(const nsAString& aCountry,
|
|
|
|
const nsTArray<nsString>& aAddressLine,
|
|
|
|
const nsAString& aRegion,
|
|
|
|
const nsAString& aCity,
|
|
|
|
const nsAString& aDependentLocality,
|
|
|
|
const nsAString& aPostalCode,
|
|
|
|
const nsAString& aSortingCode,
|
|
|
|
const nsAString& aLanguageCode,
|
|
|
|
const nsAString& aOrganization,
|
|
|
|
const nsAString& aRecipient,
|
|
|
|
const nsAString& aPhone);
|
|
|
|
|
2017-06-15 21:11:19 +03:00
|
|
|
|
|
|
|
void SetShippingOption(const nsAString& aShippingOption);
|
2017-05-25 20:50:32 +03:00
|
|
|
void GetShippingOption(nsAString& aRetVal) const;
|
2017-06-23 12:15:51 +03:00
|
|
|
nsresult UpdateShippingOption(const nsAString& aShippingOption);
|
|
|
|
|
2018-05-31 23:30:29 +03:00
|
|
|
nsresult UpdatePayment(JSContext* aCx, const PaymentDetailsUpdate& aDetails,
|
|
|
|
bool aDeferredShow);
|
|
|
|
void AbortUpdate(nsresult aRv, bool aDeferredShow);
|
2017-05-25 20:50:32 +03:00
|
|
|
|
2017-06-15 21:11:19 +03:00
|
|
|
void SetShippingType(const Nullable<PaymentShippingType>& aShippingType);
|
2017-05-25 20:50:32 +03:00
|
|
|
Nullable<PaymentShippingType> GetShippingType() const;
|
|
|
|
|
2018-03-07 14:16:46 +03:00
|
|
|
inline void ShippingWasRequested()
|
|
|
|
{
|
|
|
|
mRequestShipping = true;
|
|
|
|
}
|
|
|
|
|
2018-04-02 18:12:21 +03:00
|
|
|
void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
IMPL_EVENT_HANDLER(shippingaddresschange);
|
|
|
|
IMPL_EVENT_HANDLER(shippingoptionchange);
|
2018-06-30 10:11:00 +03:00
|
|
|
IMPL_EVENT_HANDLER(paymentmethodchange);
|
2017-05-25 20:50:32 +03:00
|
|
|
|
2018-05-31 23:30:29 +03:00
|
|
|
void SetIPC(PaymentRequestChild* aChild)
|
|
|
|
{
|
|
|
|
mIPC = aChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
PaymentRequestChild* GetIPC()
|
|
|
|
{
|
|
|
|
return mIPC;
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
protected:
|
|
|
|
~PaymentRequest();
|
|
|
|
|
2017-06-23 12:15:51 +03:00
|
|
|
nsresult DispatchUpdateEvent(const nsAString& aType);
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
PaymentRequest(nsPIDOMWindowInner* aWindow, const nsAString& aInternalId);
|
|
|
|
|
|
|
|
// Id for internal identification
|
|
|
|
nsString mInternalId;
|
|
|
|
// Id for communicating with merchant side
|
|
|
|
// mId is initialized to details.id if it exists
|
|
|
|
// otherwise, mId has the same value as mInternalId.
|
|
|
|
nsString mId;
|
2017-06-14 10:59:00 +03:00
|
|
|
// Promise for "PaymentRequest::CanMakePayment"
|
|
|
|
RefPtr<Promise> mResultPromise;
|
|
|
|
// Promise for "PaymentRequest::Show"
|
|
|
|
RefPtr<Promise> mAcceptPromise;
|
|
|
|
// Promise for "PaymentRequest::Abort"
|
|
|
|
RefPtr<Promise> mAbortPromise;
|
|
|
|
// Resolve mAcceptPromise with mResponse if user accepts the request.
|
|
|
|
RefPtr<PaymentResponse> mResponse;
|
2018-03-07 10:39:08 +03:00
|
|
|
// The redacted shipping address.
|
2017-06-14 10:59:00 +03:00
|
|
|
RefPtr<PaymentAddress> mShippingAddress;
|
2018-03-07 10:39:08 +03:00
|
|
|
// The full shipping address to be used in the response upon payment.
|
|
|
|
RefPtr<PaymentAddress> mFullShippingAddress;
|
2017-05-25 20:50:32 +03:00
|
|
|
// It is populated when the user chooses a shipping option.
|
|
|
|
nsString mShippingOption;
|
|
|
|
|
2017-06-15 21:11:19 +03:00
|
|
|
Nullable<PaymentShippingType> mShippingType;
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
// "true" when there is a pending updateWith() call to update the payment request
|
|
|
|
// and "false" otherwise.
|
|
|
|
bool mUpdating;
|
2018-03-07 14:16:46 +03:00
|
|
|
|
|
|
|
// Whether shipping was requested. This models [[options]].requestShipping,
|
|
|
|
// but we don't actually store the full [[options]] internal slot.
|
|
|
|
bool mRequestShipping;
|
2018-05-31 23:30:29 +03:00
|
|
|
|
|
|
|
// True if the user passed a promise to show, causing us to defer telling the
|
|
|
|
// front end about it.
|
|
|
|
bool mDeferredShow;
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
// The error is set in AbortUpdate(). The value is NS_OK by default.
|
2017-06-23 12:15:51 +03:00
|
|
|
nsresult mUpdateError;
|
2017-05-25 20:50:32 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
eUnknown,
|
|
|
|
eCreated,
|
|
|
|
eInteractive,
|
|
|
|
eClosed
|
|
|
|
} mState;
|
2018-05-31 23:30:29 +03:00
|
|
|
|
|
|
|
PaymentRequestChild* mIPC;
|
2017-05-25 20:50:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PaymentRequest_h
|