[Payment Request] It should be possible to require a phonetic name for shipping contacts
[WebKit-https.git] / Source / WebCore / testing / MockPaymentCoordinator.h
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 #pragma once
27
28 #if ENABLE(APPLE_PAY)
29
30 #include "ApplePayLineItem.h"
31 #include "ApplePayShippingMethod.h"
32 #include "MockPaymentAddress.h"
33 #include "MockPaymentContactFields.h"
34 #include "MockPaymentError.h"
35 #include "PaymentCoordinatorClient.h"
36 #include <wtf/HashSet.h>
37 #include <wtf/text/StringHash.h>
38
39 namespace WebCore {
40
41 class Page;
42 struct ApplePayPaymentMethod;
43
44 class MockPaymentCoordinator final : public PaymentCoordinatorClient {
45 public:
46     explicit MockPaymentCoordinator(Page&);
47
48     void setCanMakePayments(bool canMakePayments) { m_canMakePayments = canMakePayments; }
49     void setCanMakePaymentsWithActiveCard(bool canMakePaymentsWithActiveCard) { m_canMakePaymentsWithActiveCard = canMakePaymentsWithActiveCard; }
50     void setShippingAddress(MockPaymentAddress&& shippingAddress) { m_shippingAddress = WTFMove(shippingAddress); }
51     void changeShippingOption(String&& shippingOption);
52     void changePaymentMethod(ApplePayPaymentMethod&&);
53     void acceptPayment();
54     void cancelPayment();
55
56     const ApplePayLineItem& total() const { return m_total; }
57     const Vector<ApplePayLineItem>& lineItems() const { return m_lineItems; }
58     const Vector<MockPaymentError>& errors() const { return m_errors; }
59     const Vector<ApplePayShippingMethod>& shippingMethods() const { return m_shippingMethods; }
60     const MockPaymentContactFields& requiredBillingContactFields() const { return m_requiredBillingContactFields; }
61     const MockPaymentContactFields& requiredShippingContactFields() const { return m_requiredShippingContactFields; }
62
63     void ref() const { }
64     void deref() const { }
65
66 private:
67     Optional<String> validatedPaymentNetwork(const String&) final;
68     bool canMakePayments() final;
69     void canMakePaymentsWithActiveCard(const String&, const String&, WTF::Function<void(bool)>&&);
70     void openPaymentSetup(const String&, const String&, WTF::Function<void(bool)>&&);
71     bool showPaymentUI(const URL&, const Vector<URL>&, const ApplePaySessionPaymentRequest&) final;
72     void completeMerchantValidation(const PaymentMerchantSession&) final;
73     void completeShippingMethodSelection(Optional<ShippingMethodUpdate>&&) final;
74     void completeShippingContactSelection(Optional<ShippingContactUpdate>&&) final;
75     void completePaymentMethodSelection(Optional<PaymentMethodUpdate>&&) final;
76     void completePaymentSession(Optional<PaymentAuthorizationResult>&&) final;
77     void abortPaymentSession() final;
78     void cancelPaymentSession() final;
79     void paymentCoordinatorDestroyed() final;
80
81     bool isMockPaymentCoordinator() const final { return true; }
82
83     void updateTotalAndLineItems(const ApplePaySessionPaymentRequest::TotalAndLineItems&);
84
85     Page& m_page;
86     bool m_canMakePayments { true };
87     bool m_canMakePaymentsWithActiveCard { true };
88     ApplePayPaymentContact m_shippingAddress;
89     ApplePayLineItem m_total;
90     Vector<ApplePayLineItem> m_lineItems;
91     Vector<MockPaymentError> m_errors;
92     Vector<ApplePayShippingMethod> m_shippingMethods;
93     HashSet<String, ASCIICaseInsensitiveHash> m_availablePaymentNetworks;
94     MockPaymentContactFields m_requiredBillingContactFields;
95     MockPaymentContactFields m_requiredShippingContactFields;
96 };
97
98 } // namespace WebCore
99
100 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MockPaymentCoordinator)
101     static bool isType(const WebCore::PaymentCoordinatorClient& paymentCoordinatorClient) { return paymentCoordinatorClient.isMockPaymentCoordinator(); }
102 SPECIALIZE_TYPE_TRAITS_END()
103
104 #endif // ENABLE(APPLE_PAY)