3a92507cfe7eb37f233ca0662f81d49734a81f07
[WebKit-https.git] / Source / WebCore / Modules / applepay / ApplePaySession.h
1 /*
2  * Copyright (C) 2015-2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(APPLE_PAY)
29
30 #include "ActiveDOMObject.h"
31 #include "ApplePayPaymentRequest.h"
32 #include "EventTarget.h"
33 #include "ExceptionOr.h"
34 #include "PaymentSession.h"
35 #include <wtf/Ref.h>
36 #include <wtf/RefCounted.h>
37
38 namespace JSC {
39 class CallFrame;
40 class JSValue;
41 using ExecState = CallFrame;
42 }
43
44 namespace WebCore {
45
46 class DeferredPromise;
47 class Document;
48 class Payment;
49 class PaymentContact;
50 class PaymentCoordinator;
51 class PaymentMethod;
52 enum class PaymentAuthorizationStatus;
53 struct ApplePayLineItem;
54 struct ApplePayPaymentRequest;
55 struct ApplePayShippingMethod;
56 struct ApplePayPaymentAuthorizationResult;
57 struct ApplePayPaymentMethodUpdate;
58 struct ApplePayShippingContactUpdate;
59 struct ApplePayShippingMethodUpdate;
60
61 class ApplePaySession final : public PaymentSession, public ActiveDOMObject, public EventTargetWithInlineData {
62     WTF_MAKE_ISO_ALLOCATED(ApplePaySession);
63 public:
64     static ExceptionOr<Ref<ApplePaySession>> create(Document&, unsigned version, ApplePayPaymentRequest&&);
65     virtual ~ApplePaySession();
66
67     static const unsigned short STATUS_SUCCESS = 0;
68     static const unsigned short STATUS_FAILURE = 1;
69     static const unsigned short STATUS_INVALID_BILLING_POSTAL_ADDRESS = 2;
70     static const unsigned short STATUS_INVALID_SHIPPING_POSTAL_ADDRESS = 3;
71     static const unsigned short STATUS_INVALID_SHIPPING_CONTACT = 4;
72     static const unsigned short STATUS_PIN_REQUIRED = 5;
73     static const unsigned short STATUS_PIN_INCORRECT = 6;
74     static const unsigned short STATUS_PIN_LOCKOUT = 7;
75
76     static ExceptionOr<bool> supportsVersion(Document&, unsigned version);
77     static ExceptionOr<bool> canMakePayments(Document&);
78     static ExceptionOr<void> canMakePaymentsWithActiveCard(Document&, const String& merchantIdentifier, Ref<DeferredPromise>&&);
79     static ExceptionOr<void> openPaymentSetup(Document&, const String& merchantIdentifier, Ref<DeferredPromise>&&);
80
81     ExceptionOr<void> begin(Document&);
82     ExceptionOr<void> abort();
83     ExceptionOr<void> completeMerchantValidation(JSC::ExecState&, JSC::JSValue merchantSession);
84     ExceptionOr<void> completeShippingMethodSelection(ApplePayShippingMethodUpdate&&);
85     ExceptionOr<void> completeShippingContactSelection(ApplePayShippingContactUpdate&&);
86     ExceptionOr<void> completePaymentMethodSelection(ApplePayPaymentMethodUpdate&&);
87     ExceptionOr<void> completePayment(ApplePayPaymentAuthorizationResult&&);
88
89     // Old functions.
90     ExceptionOr<void> completeShippingMethodSelection(unsigned short status, ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
91     ExceptionOr<void> completeShippingContactSelection(unsigned short status, Vector<ApplePayShippingMethod>&& newShippingMethods, ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
92     ExceptionOr<void> completePaymentMethodSelection(ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
93     ExceptionOr<void> completePayment(unsigned short status);
94
95     const ApplePaySessionPaymentRequest& paymentRequest() const { return m_paymentRequest; }
96
97     using PaymentSession::ref;
98     using PaymentSession::deref;
99
100 private:
101     ApplePaySession(Document&, unsigned version, ApplePaySessionPaymentRequest&&);
102
103     // ActiveDOMObject.
104     const char* activeDOMObjectName() const override;
105     bool canSuspendForDocumentSuspension() const override;
106     void stop() override;
107
108     // EventTargetWithInlineData.
109     EventTargetInterface eventTargetInterface() const override { return ApplePaySessionEventTargetInterfaceType; }
110     ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
111     void refEventTarget() override { ref(); }
112     void derefEventTarget() override { deref(); }
113
114     // PaymentSession
115     unsigned version() const override;
116     void validateMerchant(URL&&) override;
117     void didAuthorizePayment(const Payment&) override;
118     void didSelectShippingMethod(const ApplePaySessionPaymentRequest::ShippingMethod&) override;
119     void didSelectShippingContact(const PaymentContact&) override;
120     void didSelectPaymentMethod(const PaymentMethod&) override;
121     void didCancelPaymentSession(PaymentSessionError&&) override;
122
123     PaymentCoordinator& paymentCoordinator() const;
124
125     bool canBegin() const;
126     bool canAbort() const;
127     bool canCancel() const;
128     bool canCompleteMerchantValidation() const;
129     bool canCompleteShippingMethodSelection() const;
130     bool canCompleteShippingContactSelection() const;
131     bool canCompletePaymentMethodSelection() const;
132     bool canCompletePayment() const;
133
134     bool isFinalState() const;
135     void didReachFinalState();
136
137     enum class State {
138         Idle,
139
140         Active,
141         ShippingMethodSelected,
142         ShippingContactSelected,
143         PaymentMethodSelected,
144         CancelRequested,
145         Authorized,
146         Completed,
147
148         Aborted,
149         Canceled,
150     } m_state { State::Idle };
151
152     enum class MerchantValidationState {
153         Idle,
154         ValidatingMerchant,
155         ValidationComplete,
156     } m_merchantValidationState { MerchantValidationState::Idle };
157
158     const ApplePaySessionPaymentRequest m_paymentRequest;
159     unsigned m_version;
160 };
161
162 }
163
164 #endif