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 "ApplePaySessionPaymentRequest.h"
31#include <wtf/Function.h>
32#include <wtf/WeakPtr.h>
33
34namespace WebCore {
35
36class Document;
37class Payment;
38class PaymentCoordinatorClient;
39class PaymentContact;
40class PaymentMerchantSession;
41class PaymentMethod;
42class PaymentSession;
43enum class PaymentAuthorizationStatus;
44struct PaymentAuthorizationResult;
45struct PaymentMethodUpdate;
46struct ShippingContactUpdate;
47struct ShippingMethodUpdate;
48
49class PaymentCoordinator : public CanMakeWeakPtr<PaymentCoordinator> {
50 WTF_MAKE_FAST_ALLOCATED;
51public:
52 WEBCORE_EXPORT explicit PaymentCoordinator(PaymentCoordinatorClient&);
53 WEBCORE_EXPORT ~PaymentCoordinator();
54
55 PaymentCoordinatorClient& client() { return m_client; }
56
57 bool supportsVersion(Document&, unsigned version) const;
58 bool canMakePayments(Document&);
59 void canMakePaymentsWithActiveCard(Document&, const String& merchantIdentifier, WTF::Function<void(bool)>&& completionHandler);
60 void openPaymentSetup(Document&, const String& merchantIdentifier, WTF::Function<void(bool)>&& completionHandler);
61
62 bool hasActiveSession() const { return m_activeSession; }
63
64 bool beginPaymentSession(Document&, PaymentSession&, const ApplePaySessionPaymentRequest&);
65 void completeMerchantValidation(const PaymentMerchantSession&);
66 void completeShippingMethodSelection(Optional<ShippingMethodUpdate>&&);
67 void completeShippingContactSelection(Optional<ShippingContactUpdate>&&);
68 void completePaymentMethodSelection(Optional<PaymentMethodUpdate>&&);
69 void completePaymentSession(Optional<PaymentAuthorizationResult>&&);
70 void abortPaymentSession();
71 void cancelPaymentSession();
72
73 WEBCORE_EXPORT void validateMerchant(URL&& validationURL);
74 WEBCORE_EXPORT void didAuthorizePayment(const Payment&);
75 WEBCORE_EXPORT void didSelectPaymentMethod(const PaymentMethod&);
76 WEBCORE_EXPORT void didSelectShippingMethod(const ApplePaySessionPaymentRequest::ShippingMethod&);
77 WEBCORE_EXPORT void didSelectShippingContact(const PaymentContact&);
78 WEBCORE_EXPORT void didCancelPaymentSession();
79
80 Optional<String> validatedPaymentNetwork(Document&, unsigned version, const String&) const;
81
82 bool shouldEnableApplePayAPIs(Document&) const;
83 WEBCORE_EXPORT bool shouldAllowUserAgentScripts(Document&) const;
84
85private:
86 bool setApplePayIsActiveIfAllowed(Document&) const;
87
88 PaymentCoordinatorClient& m_client;
89 RefPtr<PaymentSession> m_activeSession;
90};
91
92}
93
94#endif
95