Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / Modules / applepay / ApplePaySession.h
1 /*
2  * Copyright (C) 2015, 2016 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 ExecState;
40 class JSValue;
41 }
42
43 namespace WebCore {
44
45 class DeferredPromise;
46 class Document;
47 class Payment;
48 class PaymentContact;
49 class PaymentCoordinator;
50 class PaymentMethod;
51 enum class PaymentAuthorizationStatus;
52 struct ApplePayLineItem;
53 struct ApplePayPaymentRequest;
54 struct ApplePayShippingMethod;
55 struct ApplePayPaymentAuthorizationResult;
56 struct ApplePayPaymentMethodUpdate;
57 struct ApplePayShippingContactUpdate;
58 struct ApplePayShippingMethodUpdate;
59
60 class ApplePaySession final : public PaymentSession, public ActiveDOMObject, public EventTargetWithInlineData {
61 public:
62     static ExceptionOr<Ref<ApplePaySession>> create(Document&, unsigned version, ApplePayPaymentRequest&&);
63     virtual ~ApplePaySession();
64
65     static const unsigned short STATUS_SUCCESS = 0;
66     static const unsigned short STATUS_FAILURE = 1;
67     static const unsigned short STATUS_INVALID_BILLING_POSTAL_ADDRESS = 2;
68     static const unsigned short STATUS_INVALID_SHIPPING_POSTAL_ADDRESS = 3;
69     static const unsigned short STATUS_INVALID_SHIPPING_CONTACT = 4;
70     static const unsigned short STATUS_PIN_REQUIRED = 5;
71     static const unsigned short STATUS_PIN_INCORRECT = 6;
72     static const unsigned short STATUS_PIN_LOCKOUT = 7;
73
74     static ExceptionOr<bool> supportsVersion(ScriptExecutionContext&, unsigned version);
75     static ExceptionOr<bool> canMakePayments(ScriptExecutionContext&);
76     static ExceptionOr<void> canMakePaymentsWithActiveCard(ScriptExecutionContext&, const String& merchantIdentifier, Ref<DeferredPromise>&&);
77     static ExceptionOr<void> openPaymentSetup(ScriptExecutionContext&, const String& merchantIdentifier, Ref<DeferredPromise>&&);
78
79     ExceptionOr<void> begin();
80     ExceptionOr<void> abort();
81     ExceptionOr<void> completeMerchantValidation(JSC::ExecState&, JSC::JSValue merchantSession);
82     ExceptionOr<void> completeShippingMethodSelection(ApplePayShippingMethodUpdate&&);
83     ExceptionOr<void> completeShippingContactSelection(ApplePayShippingContactUpdate&&);
84     ExceptionOr<void> completePaymentMethodSelection(ApplePayPaymentMethodUpdate&&);
85     ExceptionOr<void> completePayment(ApplePayPaymentAuthorizationResult&&);
86
87     // Old functions.
88     ExceptionOr<void> completeShippingMethodSelection(unsigned short status, ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
89     ExceptionOr<void> completeShippingContactSelection(unsigned short status, Vector<ApplePayShippingMethod>&& newShippingMethods, ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
90     ExceptionOr<void> completePaymentMethodSelection(ApplePayLineItem&& newTotal, Vector<ApplePayLineItem>&& newLineItems);
91     ExceptionOr<void> completePayment(unsigned short status);
92
93     const ApplePaySessionPaymentRequest& paymentRequest() const { return m_paymentRequest; }
94
95     using PaymentSession::ref;
96     using PaymentSession::deref;
97
98 private:
99     ApplePaySession(Document&, unsigned version, ApplePaySessionPaymentRequest&&);
100
101     // ActiveDOMObject.
102     const char* activeDOMObjectName() const override;
103     bool canSuspendForDocumentSuspension() const override;
104     void stop() override;
105
106     // EventTargetWithInlineData.
107     EventTargetInterface eventTargetInterface() const override { return ApplePaySessionEventTargetInterfaceType; }
108     ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
109     void refEventTarget() override { ref(); }
110     void derefEventTarget() override { deref(); }
111
112     // PaymentSession
113     unsigned version() const override;
114     void validateMerchant(URL&&) override;
115     void didAuthorizePayment(const Payment&) override;
116     void didSelectShippingMethod(const ApplePaySessionPaymentRequest::ShippingMethod&) override;
117     void didSelectShippingContact(const PaymentContact&) override;
118     void didSelectPaymentMethod(const PaymentMethod&) override;
119     void didCancelPaymentSession() override;
120
121     PaymentCoordinator& paymentCoordinator() const;
122
123     bool canBegin() const;
124     bool canAbort() const;
125     bool canCancel() const;
126     bool canCompleteMerchantValidation() const;
127     bool canCompleteShippingMethodSelection() const;
128     bool canCompleteShippingContactSelection() const;
129     bool canCompletePaymentMethodSelection() const;
130     bool canCompletePayment() const;
131
132     bool isFinalState() const;
133     void didReachFinalState();
134
135     enum class State {
136         Idle,
137
138         Active,
139         ShippingMethodSelected,
140         ShippingContactSelected,
141         PaymentMethodSelected,
142         CancelRequested,
143         Authorized,
144         Completed,
145
146         Aborted,
147         Canceled,
148     } m_state { State::Idle };
149
150     enum class MerchantValidationState {
151         Idle,
152         ValidatingMerchant,
153         ValidationComplete,
154     } m_merchantValidationState { MerchantValidationState::Idle };
155
156     const ApplePaySessionPaymentRequest m_paymentRequest;
157     unsigned m_version;
158 };
159
160 }
161
162 #endif