Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / Modules / applepay / PaymentCoordinator.cpp
1 /*
2  * Copyright (C) 2015-2018 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 #include "config.h"
27 #include "PaymentCoordinator.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #include "PaymentAuthorizationStatus.h"
32 #include "PaymentCoordinatorClient.h"
33 #include "PaymentSession.h"
34 #include <wtf/URL.h>
35
36 namespace WebCore {
37
38 PaymentCoordinator::PaymentCoordinator(PaymentCoordinatorClient& client)
39     : m_client { client }
40 {
41 }
42
43 PaymentCoordinator::~PaymentCoordinator()
44 {
45     m_client.paymentCoordinatorDestroyed();
46 }
47
48 bool PaymentCoordinator::supportsVersion(unsigned version) const
49 {
50     return m_client.supportsVersion(version);
51 }
52
53 bool PaymentCoordinator::canMakePayments()
54 {
55     return m_client.canMakePayments();
56 }
57
58 void PaymentCoordinator::canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler)
59 {
60     m_client.canMakePaymentsWithActiveCard(merchantIdentifier, domainName, WTFMove(completionHandler));
61 }
62
63 void PaymentCoordinator::openPaymentSetup(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler)
64 {
65     m_client.openPaymentSetup(merchantIdentifier, domainName, WTFMove(completionHandler));
66 }
67
68 bool PaymentCoordinator::beginPaymentSession(PaymentSession& paymentSession, const URL& originatingURL, const Vector<URL>& linkIconURLs, const ApplePaySessionPaymentRequest& paymentRequest)
69 {
70     ASSERT(!m_activeSession);
71
72     if (!m_client.showPaymentUI(originatingURL, linkIconURLs, paymentRequest))
73         return false;
74
75     m_activeSession = &paymentSession;
76     return true;
77 }
78
79 void PaymentCoordinator::completeMerchantValidation(const PaymentMerchantSession& paymentMerchantSession)
80 {
81     ASSERT(m_activeSession);
82
83     m_client.completeMerchantValidation(paymentMerchantSession);
84 }
85
86 void PaymentCoordinator::completeShippingMethodSelection(std::optional<ShippingMethodUpdate>&& update)
87 {
88     ASSERT(m_activeSession);
89
90     m_client.completeShippingMethodSelection(WTFMove(update));
91 }
92
93 void PaymentCoordinator::completeShippingContactSelection(std::optional<ShippingContactUpdate>&& update)
94 {
95     ASSERT(m_activeSession);
96
97     m_client.completeShippingContactSelection(WTFMove(update));
98 }
99
100 void PaymentCoordinator::completePaymentMethodSelection(std::optional<PaymentMethodUpdate>&& update)
101 {
102     ASSERT(m_activeSession);
103
104     m_client.completePaymentMethodSelection(WTFMove(update));
105 }
106
107 void PaymentCoordinator::completePaymentSession(std::optional<PaymentAuthorizationResult>&& result)
108 {
109     ASSERT(m_activeSession);
110
111     bool isFinalState = isFinalStateResult(result);
112
113     m_client.completePaymentSession(WTFMove(result));
114
115     if (!isFinalState)
116         return;
117
118     m_activeSession = nullptr;
119 }
120
121 void PaymentCoordinator::abortPaymentSession()
122 {
123     ASSERT(m_activeSession);
124
125     m_client.abortPaymentSession();
126     m_activeSession = nullptr;
127 }
128
129 void PaymentCoordinator::cancelPaymentSession()
130 {
131     ASSERT(m_activeSession);
132
133     m_client.cancelPaymentSession();
134 }
135
136 void PaymentCoordinator::validateMerchant(URL&& validationURL)
137 {
138     if (!m_activeSession) {
139         // It's possible that the payment has been aborted already.
140         return;
141     }
142
143     m_activeSession->validateMerchant(WTFMove(validationURL));
144 }
145
146 void PaymentCoordinator::didAuthorizePayment(const Payment& payment)
147 {
148     if (!m_activeSession) {
149         // It's possible that the payment has been aborted already.
150         return;
151     }
152
153     m_activeSession->didAuthorizePayment(payment);
154 }
155
156 void PaymentCoordinator::didSelectPaymentMethod(const PaymentMethod& paymentMethod)
157 {
158     if (!m_activeSession) {
159         // It's possible that the payment has been aborted already.
160         return;
161     }
162
163     m_activeSession->didSelectPaymentMethod(paymentMethod);
164 }
165
166 void PaymentCoordinator::didSelectShippingMethod(const ApplePaySessionPaymentRequest::ShippingMethod& shippingMethod)
167 {
168     if (!m_activeSession) {
169         // It's possible that the payment has been aborted already.
170         return;
171     }
172
173     m_activeSession->didSelectShippingMethod(shippingMethod);
174 }
175
176 void PaymentCoordinator::didSelectShippingContact(const PaymentContact& shippingContact)
177 {
178     if (!m_activeSession) {
179         // It's possible that the payment has been aborted already.
180         return;
181     }
182
183     m_activeSession->didSelectShippingContact(shippingContact);
184 }
185
186 void PaymentCoordinator::didCancelPaymentSession()
187 {
188     if (!m_activeSession) {
189         // It's possible that the payment has been aborted already.
190         return;
191     }
192
193     m_activeSession->didCancelPaymentSession();
194     m_activeSession = nullptr;
195 }
196
197 std::optional<String> PaymentCoordinator::validatedPaymentNetwork(unsigned version, const String& paymentNetwork) const
198 {
199     if (version < 2 && equalIgnoringASCIICase(paymentNetwork, "jcb"))
200         return std::nullopt;
201
202     if (version < 3 && equalIgnoringASCIICase(paymentNetwork, "carteBancaire"))
203         return std::nullopt;
204
205     return m_client.validatedPaymentNetwork(paymentNetwork);
206 }
207
208 } // namespace WebCore
209
210 #endif // ENABLE(APPLE_PAY)