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"
|
|
|
|
#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;
|
2017-05-25 20:50:32 +03:00
|
|
|
class PaymentResponse;
|
|
|
|
|
|
|
|
class PaymentRequest final : public DOMEventTargetHelper
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2017-05-25 20:50:32 +03:00
|
|
|
already_AddRefed<Promise> Show(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();
|
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);
|
|
|
|
|
2017-09-06 09:36:24 +03:00
|
|
|
nsresult UpdatePayment(JSContext* aCx, const PaymentDetailsUpdate& aDetails);
|
2017-06-23 12:15:51 +03:00
|
|
|
void AbortUpdate(nsresult aRv);
|
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;
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(shippingaddresschange);
|
|
|
|
IMPL_EVENT_HANDLER(shippingoptionchange);
|
|
|
|
|
|
|
|
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;
|
|
|
|
// It is populated when the user provides a shipping address.
|
|
|
|
RefPtr<PaymentAddress> mShippingAddress;
|
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;
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PaymentRequest_h
|