[Payment Request] It should be possible to require a phonetic name for shipping contacts
[WebKit-https.git] / Source / WebCore / testing / MockPaymentCoordinator.cpp
1 /*
2  * Copyright (C) 2017-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 #include "config.h"
27 #include "MockPaymentCoordinator.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #include "ApplePaySessionPaymentRequest.h"
32 #include "MockPayment.h"
33 #include "MockPaymentContact.h"
34 #include "MockPaymentMethod.h"
35 #include "Page.h"
36 #include "PaymentCoordinator.h"
37 #include <wtf/RunLoop.h>
38 #include <wtf/URL.h>
39
40 namespace WebCore {
41
42 MockPaymentCoordinator::MockPaymentCoordinator(Page& page)
43     : m_page { page }
44 {
45     m_availablePaymentNetworks.add("amex");
46     m_availablePaymentNetworks.add("carteBancaire");
47     m_availablePaymentNetworks.add("chinaUnionPay");
48     m_availablePaymentNetworks.add("discover");
49     m_availablePaymentNetworks.add("interac");
50     m_availablePaymentNetworks.add("jcb");
51     m_availablePaymentNetworks.add("masterCard");
52     m_availablePaymentNetworks.add("privateLabel");
53     m_availablePaymentNetworks.add("visa");
54 }
55
56 Optional<String> MockPaymentCoordinator::validatedPaymentNetwork(const String& paymentNetwork)
57 {
58     auto result = m_availablePaymentNetworks.find(paymentNetwork);
59     if (result == m_availablePaymentNetworks.end())
60         return WTF::nullopt;
61     return *result;
62 }
63
64 bool MockPaymentCoordinator::canMakePayments()
65 {
66     return m_canMakePayments;
67 }
68
69 void MockPaymentCoordinator::canMakePaymentsWithActiveCard(const String&, const String&, Function<void(bool)>&& completionHandler)
70 {
71     RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), canMakePaymentsWithActiveCard = m_canMakePaymentsWithActiveCard] {
72         completionHandler(canMakePaymentsWithActiveCard);
73     });
74 }
75
76 void MockPaymentCoordinator::openPaymentSetup(const String&, const String&, Function<void(bool)>&& completionHandler)
77 {
78     RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)] {
79         completionHandler(true);
80     });
81 }
82
83 static uint64_t showCount;
84 static uint64_t hideCount;
85
86 static void dispatchIfShowing(Function<void()>&& function)
87 {
88     ASSERT(showCount > hideCount);
89     RunLoop::main().dispatch([currentShowCount = showCount, function = WTFMove(function)]() {
90         if (showCount > hideCount && showCount == currentShowCount)
91             function();
92     });
93 }
94
95 static Vector<ApplePayShippingMethod> convert(const Vector<ApplePaySessionPaymentRequest::ShippingMethod>& shippingMethods)
96 {
97     return WTF::map(shippingMethods, [] (auto& shippingMethod) {
98         return ApplePayShippingMethod { shippingMethod.label, shippingMethod.detail, shippingMethod.amount, shippingMethod.identifier };
99     });
100 }
101
102 bool MockPaymentCoordinator::showPaymentUI(const URL&, const Vector<URL>&, const ApplePaySessionPaymentRequest& request)
103 {
104     if (request.shippingContact().pkContact())
105         m_shippingAddress = request.shippingContact().toApplePayPaymentContact(request.version());
106     m_shippingMethods = convert(request.shippingMethods());
107     m_requiredBillingContactFields = request.requiredBillingContactFields();
108     m_requiredShippingContactFields = request.requiredShippingContactFields();
109
110     ASSERT(showCount == hideCount);
111     ++showCount;
112     dispatchIfShowing([page = &m_page]() {
113         page->paymentCoordinator().validateMerchant({ URL(), "https://webkit.org/"_s });
114     });
115     return true;
116 }
117
118 void MockPaymentCoordinator::completeMerchantValidation(const PaymentMerchantSession&)
119 {
120     dispatchIfShowing([page = &m_page, shippingAddress = m_shippingAddress]() mutable {
121         page->paymentCoordinator().didSelectShippingContact(MockPaymentContact { WTFMove(shippingAddress) });
122     });
123 }
124
125 static ApplePayLineItem convert(const ApplePaySessionPaymentRequest::LineItem& lineItem)
126 {
127     ApplePayLineItem result;
128     result.type = lineItem.type;
129     result.label = lineItem.label;
130     result.amount = lineItem.amount;
131     return result;
132 }
133
134 void MockPaymentCoordinator::updateTotalAndLineItems(const ApplePaySessionPaymentRequest::TotalAndLineItems& totalAndLineItems)
135 {
136     m_total = convert(totalAndLineItems.total);
137     m_lineItems.clear();
138     for (auto& lineItem : totalAndLineItems.lineItems)
139         m_lineItems.append(convert(lineItem));
140 }
141
142 void MockPaymentCoordinator::completeShippingMethodSelection(Optional<ShippingMethodUpdate>&& shippingMethodUpdate)
143 {
144     if (shippingMethodUpdate)
145         updateTotalAndLineItems(shippingMethodUpdate->newTotalAndLineItems);
146 }
147
148 void MockPaymentCoordinator::completeShippingContactSelection(Optional<ShippingContactUpdate>&& shippingContactUpdate)
149 {
150     if (!shippingContactUpdate)
151         return;
152
153     m_shippingMethods = convert(shippingContactUpdate->newShippingMethods);
154     updateTotalAndLineItems(shippingContactUpdate->newTotalAndLineItems);
155     m_errors = WTFMove(shippingContactUpdate->errors);
156 }
157
158 void MockPaymentCoordinator::completePaymentMethodSelection(Optional<PaymentMethodUpdate>&& paymentMethodUpdate)
159 {
160     if (paymentMethodUpdate)
161         updateTotalAndLineItems(paymentMethodUpdate->newTotalAndLineItems);
162 }
163
164 void MockPaymentCoordinator::changeShippingOption(String&& shippingOption)
165 {
166     dispatchIfShowing([page = &m_page, shippingOption = WTFMove(shippingOption)]() mutable {
167         ApplePaySessionPaymentRequest::ShippingMethod shippingMethod;
168         shippingMethod.identifier = WTFMove(shippingOption);
169         page->paymentCoordinator().didSelectShippingMethod(shippingMethod);
170     });
171 }
172
173 void MockPaymentCoordinator::changePaymentMethod(ApplePayPaymentMethod&& paymentMethod)
174 {
175     dispatchIfShowing([page = &m_page, paymentMethod = WTFMove(paymentMethod)]() mutable {
176         page->paymentCoordinator().didSelectPaymentMethod(MockPaymentMethod { WTFMove(paymentMethod) });
177     });
178 }
179
180 void MockPaymentCoordinator::acceptPayment()
181 {
182     dispatchIfShowing([page = &m_page, shippingAddress = m_shippingAddress]() mutable {
183         ApplePayPayment payment;
184         payment.shippingContact = WTFMove(shippingAddress);
185         page->paymentCoordinator().didAuthorizePayment(MockPayment { WTFMove(payment) });
186     });
187 }
188
189 void MockPaymentCoordinator::cancelPayment()
190 {
191     dispatchIfShowing([page = &m_page] {
192         page->paymentCoordinator().didCancelPaymentSession();
193         ++hideCount;
194         ASSERT(showCount == hideCount);
195     });
196 }
197
198 void MockPaymentCoordinator::completePaymentSession(Optional<PaymentAuthorizationResult>&& result)
199 {
200     auto isFinalState = isFinalStateResult(result);
201     m_errors = WTFMove(result->errors);
202
203     if (!isFinalState)
204         return;
205
206     ++hideCount;
207     ASSERT(showCount == hideCount);
208 }
209
210 void MockPaymentCoordinator::abortPaymentSession()
211 {
212     ++hideCount;
213     ASSERT(showCount == hideCount);
214 }
215
216 void MockPaymentCoordinator::cancelPaymentSession()
217 {
218     ++hideCount;
219     ASSERT(showCount == hideCount);
220 }
221
222 void MockPaymentCoordinator::paymentCoordinatorDestroyed()
223 {
224     ASSERT(showCount == hideCount);
225     delete this;
226 }
227
228 } // namespace WebCore
229
230 #endif // ENABLE(APPLE_PAY)