[Apple Pay] Clean up handling of summary items and payment method updates
authoraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 21 Sep 2019 06:37:34 +0000 (06:37 +0000)
committeraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 21 Sep 2019 06:37:34 +0000 (06:37 +0000)
https://bugs.webkit.org/show_bug.cgi?id=202018
<rdar://problem/55470632>

Reviewed by Tim Horton.

Source/WebCore:

Replaced the PaymentMethodUpdate struct with a class that knows how to convert from
ApplePayPaymentMethodUpdate structs to PKPaymentRequestPaymentMethodUpdate instances.

Moved some scattered-around free functions for converting payment summary items into
PaymentSummaryItems{.h,Cocoa.mm}.

* Modules/applepay/ApplePayPaymentMethodUpdate.h:
* Modules/applepay/ApplePaySession.cpp:
(WebCore::finishConverting):
(WebCore::convertAndValidateTotal):
(WebCore::convertAndValidate):
* Modules/applepay/ApplePaySessionPaymentRequest.h:
* Modules/applepay/PaymentCoordinator.h:
* Modules/applepay/PaymentCoordinatorClient.h:
* Modules/applepay/PaymentHeaders.h:
* Modules/applepay/PaymentMethodUpdate.h: Added.
* Modules/applepay/PaymentSummaryItems.h: Added.
* Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm: Added.
(WebCore::PaymentMethodUpdate::PaymentMethodUpdate):
(WebCore::PaymentMethodUpdate::totalAndLineItems const):
(WebCore::PaymentMethodUpdate::platformUpdate const):
* Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm: Added.
(WebCore::toDecimalNumber):
(WebCore::toPKPaymentSummaryItemType):
(WebCore::toPKPaymentSummaryItem):
(WebCore::platformSummaryItems):
* Modules/applepay/paymentrequest/ApplePayPaymentHandler.cpp:
(WebCore::ApplePayPaymentHandler::paymentMethodUpdated):
* SourcesCocoa.txt:
* WebCore.xcodeproj/project.pbxproj:
* testing/MockPaymentCoordinator.cpp:
(WebCore::MockPaymentCoordinator::completePaymentMethodSelection):

Source/WebKit:

Now that PaymentMethodUpdate knows how to convert itself to a
PKPaymentRequestPaymentMethodUpdate, PaymentAuthorizationPresenter can merely pass the
converted update directly to the platform delegate rather than passing the individual
components and relying on the delegate to instantiate the platform update itself. Added
FIXMEs for applying a similar treatment to ShippingContactUpdate and ShippingMethodUpdate.

* Platform/cocoa/PaymentAuthorizationPresenter.h:
* Platform/cocoa/PaymentAuthorizationPresenter.mm:
(WebKit::PaymentAuthorizationPresenter::completePaymentMethodSelection):
(WebKit::PaymentAuthorizationPresenter::completeShippingContactSelection):
(WebKit::PaymentAuthorizationPresenter::completeShippingMethodSelection):
* Platform/cocoa/WKPaymentAuthorizationDelegate.h:
* Platform/cocoa/WKPaymentAuthorizationDelegate.mm:
(-[WKPaymentAuthorizationDelegate completePaymentMethodSelection:]):
(-[WKPaymentAuthorizationDelegate completeShippingContactSelection:]):
(-[WKPaymentAuthorizationDelegate completeShippingMethodSelection:]):
(-[WKPaymentAuthorizationDelegate _didSelectPaymentMethod:completion:]):
(-[WKPaymentAuthorizationDelegate _didSelectShippingContact:completion:]):
(-[WKPaymentAuthorizationDelegate _didSelectShippingMethod:completion:]):
(-[WKPaymentAuthorizationDelegate completeShippingContactSelection:summaryItems:shippingMethods:errors:]): Deleted.
* Shared/ApplePay/WebPaymentCoordinatorProxy.h:
* Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.h:
* Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.mm:
(WebKit::WebPaymentCoordinatorProxy::platformPaymentRequest):
(WebKit::toPKPaymentSummaryItemType): Deleted.
(WebKit::toPKPaymentSummaryItem): Deleted.
(WebKit::toPKPaymentSummaryItems): Deleted.
* Shared/Cocoa/WebCoreArgumentCodersCocoa.mm:
(IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::encode):
(IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::decode):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@250179 268f45cc-cd09-0410-ab3c-d52691b4dbfc

25 files changed:
Source/WebCore/ChangeLog
Source/WebCore/Modules/applepay/ApplePayPaymentMethodUpdate.h
Source/WebCore/Modules/applepay/ApplePaySession.cpp
Source/WebCore/Modules/applepay/ApplePaySessionPaymentRequest.h
Source/WebCore/Modules/applepay/PaymentCoordinator.h
Source/WebCore/Modules/applepay/PaymentCoordinatorClient.h
Source/WebCore/Modules/applepay/PaymentHeaders.h
Source/WebCore/Modules/applepay/PaymentMethodUpdate.h [new file with mode: 0644]
Source/WebCore/Modules/applepay/PaymentSummaryItems.h [new file with mode: 0644]
Source/WebCore/Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm [new file with mode: 0644]
Source/WebCore/Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm [new file with mode: 0644]
Source/WebCore/Modules/applepay/paymentrequest/ApplePayPaymentHandler.cpp
Source/WebCore/PAL/pal/spi/cocoa/PassKitSPI.h
Source/WebCore/SourcesCocoa.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/testing/MockPaymentCoordinator.cpp
Source/WebKit/ChangeLog
Source/WebKit/Platform/cocoa/PaymentAuthorizationPresenter.h
Source/WebKit/Platform/cocoa/PaymentAuthorizationPresenter.mm
Source/WebKit/Platform/cocoa/WKPaymentAuthorizationDelegate.h
Source/WebKit/Platform/cocoa/WKPaymentAuthorizationDelegate.mm
Source/WebKit/Shared/ApplePay/WebPaymentCoordinatorProxy.h
Source/WebKit/Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.h
Source/WebKit/Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.mm
Source/WebKit/Shared/Cocoa/WebCoreArgumentCodersCocoa.mm

index 37343a5..7ddf589 100644 (file)
@@ -1,3 +1,44 @@
+2019-09-19  Andy Estes  <aestes@apple.com>
+
+        [Apple Pay] Clean up handling of summary items and payment method updates
+        https://bugs.webkit.org/show_bug.cgi?id=202018
+        <rdar://problem/55470632>
+
+        Reviewed by Tim Horton.
+
+        Replaced the PaymentMethodUpdate struct with a class that knows how to convert from
+        ApplePayPaymentMethodUpdate structs to PKPaymentRequestPaymentMethodUpdate instances.
+
+        Moved some scattered-around free functions for converting payment summary items into
+        PaymentSummaryItems{.h,Cocoa.mm}.
+
+        * Modules/applepay/ApplePayPaymentMethodUpdate.h:
+        * Modules/applepay/ApplePaySession.cpp:
+        (WebCore::finishConverting):
+        (WebCore::convertAndValidateTotal):
+        (WebCore::convertAndValidate):
+        * Modules/applepay/ApplePaySessionPaymentRequest.h:
+        * Modules/applepay/PaymentCoordinator.h:
+        * Modules/applepay/PaymentCoordinatorClient.h:
+        * Modules/applepay/PaymentHeaders.h:
+        * Modules/applepay/PaymentMethodUpdate.h: Added.
+        * Modules/applepay/PaymentSummaryItems.h: Added.
+        * Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm: Added.
+        (WebCore::PaymentMethodUpdate::PaymentMethodUpdate):
+        (WebCore::PaymentMethodUpdate::totalAndLineItems const):
+        (WebCore::PaymentMethodUpdate::platformUpdate const):
+        * Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm: Added.
+        (WebCore::toDecimalNumber):
+        (WebCore::toPKPaymentSummaryItemType):
+        (WebCore::toPKPaymentSummaryItem):
+        (WebCore::platformSummaryItems):
+        * Modules/applepay/paymentrequest/ApplePayPaymentHandler.cpp:
+        (WebCore::ApplePayPaymentHandler::paymentMethodUpdated):
+        * SourcesCocoa.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * testing/MockPaymentCoordinator.cpp:
+        (WebCore::MockPaymentCoordinator::completePaymentMethodSelection):
+
 2019-09-20  Said Abou-Hallawa  <sabouhallawa@apple.com>
 
         Assertion fires when animating a discrete property with values range and multiple animators
index a732212..5b673d3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Apple Inc. All rights reserved.
+ * Copyright (C) 2017-2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #include "ApplePayLineItem.h"
 
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/ApplePayPaymentMethodUpdateAdditions.h>
+#endif
+
 namespace WebCore {
 
 struct ApplePayPaymentMethodUpdate {
     ApplePayLineItem newTotal;
     Vector<ApplePayLineItem> newLineItems;
+
+#if defined(APPLEPAYPAYMENTMETHODUPDATE_ADDITIONS)
+APPLEPAYPAYMENTMETHODUPDATE_ADDITIONS
+#undef APPLEPAYPAYMENTMETHODUPDATE_ADDITIONS
+#endif
 };
 
 }
index 85a02be..69f8e32 100644 (file)
 #include "PaymentCoordinator.h"
 #include "PaymentMerchantSession.h"
 #include "PaymentMethod.h"
+#include "PaymentMethodUpdate.h"
 #include "PaymentRequestValidator.h"
 #include "SecurityOrigin.h"
 #include "Settings.h"
 #include "UserGestureIndicator.h"
 #include <wtf/IsoMallocInlines.h>
 
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/ApplePaySessionAdditions.cpp>
+#else
+namespace WebCore {
+static void finishConverting(PaymentMethodUpdate&, ApplePayPaymentMethodUpdate&&) { }
+}
+#endif
+
 namespace WebCore {
 
 WTF_MAKE_ISO_ALLOCATED_IMPL(ApplePaySession);
@@ -139,12 +148,13 @@ static ExceptionOr<ApplePaySessionPaymentRequest::LineItem> convertAndValidateTo
     if (!validateAmount(lineItem.amount))
         return Exception { TypeError, makeString("\"" + lineItem.amount, "\" is not a valid amount.") };
 
-    ApplePaySessionPaymentRequest::LineItem result;
-    result.amount = lineItem.amount;
-    result.type = lineItem.type;
-    result.label = lineItem.label;
+    ApplePaySessionPaymentRequest::LineItem total { lineItem.type, lineItem.amount, lineItem.label };
 
-    return WTFMove(result);
+    auto validatedTotal = PaymentRequestValidator::validateTotal(total);
+    if (validatedTotal.hasException())
+        return validatedTotal.releaseException();
+
+    return WTFMove(total);
 }
 
 static ExceptionOr<ApplePaySessionPaymentRequest::LineItem> convertAndValidate(ApplePayLineItem&& lineItem)
@@ -318,23 +328,16 @@ static ExceptionOr<PaymentAuthorizationResult> convertAndValidate(ApplePayPaymen
 
 static ExceptionOr<PaymentMethodUpdate> convertAndValidate(ApplePayPaymentMethodUpdate&& update)
 {
-    PaymentMethodUpdate convertedUpdate;
-
     auto convertedNewTotal = convertAndValidateTotal(WTFMove(update.newTotal));
     if (convertedNewTotal.hasException())
         return convertedNewTotal.releaseException();
-    convertedUpdate.newTotalAndLineItems.total = convertedNewTotal.releaseReturnValue();
-
-    // FIXME: Merge this validation into the validation we are doing above.
-    auto validatedTotal = PaymentRequestValidator::validateTotal(convertedUpdate.newTotalAndLineItems.total);
-    if (validatedTotal.hasException())
-        return validatedTotal.releaseException();
 
     auto convertedNewLineItems = convertAndValidate(WTFMove(update.newLineItems));
     if (convertedNewLineItems.hasException())
         return convertedNewLineItems.releaseException();
 
-    convertedUpdate.newTotalAndLineItems.lineItems = convertedNewLineItems.releaseReturnValue();
+    PaymentMethodUpdate convertedUpdate { convertedNewTotal.releaseReturnValue(), convertedNewLineItems.releaseReturnValue() };
+    finishConverting(convertedUpdate, WTFMove(update));
 
     return WTFMove(convertedUpdate);
 }
@@ -355,11 +358,6 @@ static ExceptionOr<ShippingContactUpdate> convertAndValidate(ApplePayShippingCon
         return convertedNewTotal.releaseException();
     convertedUpdate.newTotalAndLineItems.total = convertedNewTotal.releaseReturnValue();
 
-    // FIXME: Merge this validation into the validation we are doing above.
-    auto validatedTotal = PaymentRequestValidator::validateTotal(convertedUpdate.newTotalAndLineItems.total);
-    if (validatedTotal.hasException())
-        return validatedTotal.releaseException();
-
     auto convertedNewLineItems = convertAndValidate(WTFMove(update.newLineItems));
     if (convertedNewLineItems.hasException())
         return convertedNewLineItems.releaseException();
@@ -378,11 +376,6 @@ static ExceptionOr<ShippingMethodUpdate> convertAndValidate(ApplePayShippingMeth
 
     convertedUpdate.newTotalAndLineItems.total = convertedNewTotal.releaseReturnValue();
 
-    // FIXME: Merge this validation into the validation we are doing above.
-    auto validatedTotal = PaymentRequestValidator::validateTotal(convertedUpdate.newTotalAndLineItems.total);
-    if (validatedTotal.hasException())
-        return validatedTotal.releaseException();
-
     auto convertedNewLineItems = convertAndValidate(WTFMove(update.newLineItems));
     if (convertedNewLineItems.hasException())
         return convertedNewLineItems.releaseException();
index f575a54..8a65c49 100644 (file)
@@ -212,10 +212,6 @@ struct PaymentAuthorizationResult {
     Vector<PaymentError> errors;
 };
 
-struct PaymentMethodUpdate {
-    ApplePaySessionPaymentRequest::TotalAndLineItems newTotalAndLineItems;
-};
-
 struct ShippingContactUpdate {
     Vector<PaymentError> errors;
 
index 1a4af3b..4a0f96e 100644 (file)
@@ -39,11 +39,11 @@ class PaymentCoordinatorClient;
 class PaymentContact;
 class PaymentMerchantSession;
 class PaymentMethod;
+class PaymentMethodUpdate;
 class PaymentSession;
 class PaymentSessionError;
 enum class PaymentAuthorizationStatus;
 struct PaymentAuthorizationResult;
-struct PaymentMethodUpdate;
 struct ShippingContactUpdate;
 struct ShippingMethodUpdate;
 
index 8d523c0..03f5b28 100644 (file)
@@ -39,8 +39,8 @@ namespace WebCore {
 
 class Document;
 class PaymentMerchantSession;
+class PaymentMethodUpdate;
 struct PaymentAuthorizationResult;
-struct PaymentMethodUpdate;
 struct ShippingContactUpdate;
 struct ShippingMethodUpdate;
 
index 3f1b04e..c555117 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,4 +30,6 @@
 #include "PaymentContact.h"
 #include "PaymentMerchantSession.h"
 #include "PaymentMethod.h"
+#include "PaymentMethodUpdate.h"
 #include "PaymentSessionError.h"
+#include "PaymentSummaryItems.h"
diff --git a/Source/WebCore/Modules/applepay/PaymentMethodUpdate.h b/Source/WebCore/Modules/applepay/PaymentMethodUpdate.h
new file mode 100644 (file)
index 0000000..c1813cd
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(APPLE_PAY)
+
+#include "ApplePaySessionPaymentRequest.h"
+#include <wtf/RetainPtr.h>
+
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/PaymentMethodUpdateAdditions.h>
+#endif
+
+OBJC_CLASS PKPaymentRequestPaymentMethodUpdate;
+
+namespace WebCore {
+
+class WEBCORE_EXPORT PaymentMethodUpdate {
+public:
+    using LineItem = ApplePaySessionPaymentRequest::LineItem;
+    using TotalAndLineItems = ApplePaySessionPaymentRequest::TotalAndLineItems;
+
+    PaymentMethodUpdate(LineItem&& total, Vector<LineItem>&&);
+    explicit PaymentMethodUpdate(RetainPtr<PKPaymentRequestPaymentMethodUpdate>&&);
+    explicit PaymentMethodUpdate(TotalAndLineItems&&);
+
+    const TotalAndLineItems& totalAndLineItems() const;
+    PKPaymentRequestPaymentMethodUpdate *platformUpdate() const;
+
+#if defined(PAYMENTMETHODUPDATE_PUBLIC_ADDITIONS)
+PAYMENTMETHODUPDATE_PUBLIC_ADDITIONS
+#undef PAYMENTMETHODUPDATE_PUBLIC_ADDITIONS
+#endif
+    
+private:
+    Optional<TotalAndLineItems> m_totalAndLineItems;
+    RetainPtr<PKPaymentRequestPaymentMethodUpdate> m_platformUpdate;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(APPLE_PAY)
diff --git a/Source/WebCore/Modules/applepay/PaymentSummaryItems.h b/Source/WebCore/Modules/applepay/PaymentSummaryItems.h
new file mode 100644 (file)
index 0000000..5e9516a
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(APPLE_PAY)
+
+#include "ApplePaySessionPaymentRequest.h"
+
+OBJC_CLASS NSArray;
+
+namespace WebCore {
+
+WEBCORE_EXPORT NSArray *platformSummaryItems(const ApplePaySessionPaymentRequest::TotalAndLineItems&);
+WEBCORE_EXPORT NSArray *platformSummaryItems(const ApplePaySessionPaymentRequest::LineItem& total, const Vector<ApplePaySessionPaymentRequest::LineItem>&);
+
+} // namespace WebCore
+
+#endif // ENABLE(APPLE_PAY)
diff --git a/Source/WebCore/Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm b/Source/WebCore/Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm
new file mode 100644 (file)
index 0000000..25e1f76
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "PaymentMethodUpdate.h"
+
+#if ENABLE(APPLE_PAY)
+
+#import "PaymentSummaryItems.h"
+#import <pal/cocoa/PassKitSoftLink.h>
+
+namespace WebCore {
+
+PaymentMethodUpdate::PaymentMethodUpdate(LineItem&& total, Vector<LineItem>&& lineItems)
+    : m_totalAndLineItems { { WTFMove(total), WTFMove(lineItems) } }
+    , m_platformUpdate { adoptNS([PAL::allocPKPaymentRequestPaymentMethodUpdateInstance() initWithPaymentSummaryItems:platformSummaryItems(*m_totalAndLineItems)]) }
+{
+}
+
+PaymentMethodUpdate::PaymentMethodUpdate(RetainPtr<PKPaymentRequestPaymentMethodUpdate>&& platformUpdate)
+    : m_platformUpdate { WTFMove(platformUpdate) }
+{
+}
+
+PaymentMethodUpdate::PaymentMethodUpdate(TotalAndLineItems&& totalAndLineItems)
+    : PaymentMethodUpdate { WTFMove(totalAndLineItems.total), WTFMove(totalAndLineItems.lineItems) }
+{
+}
+
+const PaymentMethodUpdate::TotalAndLineItems& PaymentMethodUpdate::totalAndLineItems() const
+{
+    return *m_totalAndLineItems;
+}
+
+PKPaymentRequestPaymentMethodUpdate *PaymentMethodUpdate::platformUpdate() const
+{
+    return m_platformUpdate.get();
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(APPLE_PAY)
diff --git a/Source/WebCore/Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm b/Source/WebCore/Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm
new file mode 100644 (file)
index 0000000..8a8724a
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "PaymentSummaryItems.h"
+
+#if ENABLE(APPLE_PAY)
+
+#import <pal/cocoa/PassKitSoftLink.h>
+
+namespace WebCore {
+
+static NSDecimalNumber *toDecimalNumber(const String& amount)
+{
+    if (!amount)
+        return [NSDecimalNumber zero];
+    return [NSDecimalNumber decimalNumberWithString:amount locale:@{ NSLocaleDecimalSeparator : @"." }];
+}
+
+static PKPaymentSummaryItemType toPKPaymentSummaryItemType(ApplePaySessionPaymentRequest::LineItem::Type type)
+{
+    switch (type) {
+    case ApplePaySessionPaymentRequest::LineItem::Type::Final:
+        return PKPaymentSummaryItemTypeFinal;
+    case ApplePaySessionPaymentRequest::LineItem::Type::Pending:
+        return PKPaymentSummaryItemTypePending;
+    }
+}
+
+static PKPaymentSummaryItem *toPKPaymentSummaryItem(const ApplePaySessionPaymentRequest::LineItem& lineItem)
+{
+    return [PAL::getPKPaymentSummaryItemClass() summaryItemWithLabel:lineItem.label amount:toDecimalNumber(lineItem.amount) type:toPKPaymentSummaryItemType(lineItem.type)];
+}
+
+NSArray *platformSummaryItems(const ApplePaySessionPaymentRequest::TotalAndLineItems& totalAndLineItems)
+{
+    return platformSummaryItems(totalAndLineItems.total, totalAndLineItems.lineItems);
+}
+
+NSArray *platformSummaryItems(const ApplePaySessionPaymentRequest::LineItem& total, const Vector<ApplePaySessionPaymentRequest::LineItem>& lineItems)
+{
+    NSMutableArray *paymentSummaryItems = [NSMutableArray arrayWithCapacity:lineItems.size() + 1];
+    for (auto& lineItem : lineItems) {
+        if (PKPaymentSummaryItem *summaryItem = toPKPaymentSummaryItem(lineItem))
+            [paymentSummaryItems addObject:summaryItem];
+    }
+
+    if (PKPaymentSummaryItem *totalItem = toPKPaymentSummaryItem(total))
+        [paymentSummaryItems addObject:totalItem];
+
+    return adoptNS([paymentSummaryItems copy]).autorelease();
+}
+
+} // namespace WebbCore
+
+#endif // ENABLE(APPLE_PAY)
index d34d7cd..0f7e3ba 100644 (file)
@@ -51,6 +51,7 @@
 #include "PaymentCoordinator.h"
 #include "PaymentMerchantSession.h"
 #include "PaymentMethod.h"
+#include "PaymentMethodUpdate.h"
 #include "PaymentRequestValidator.h"
 #include "PaymentResponse.h"
 #include "PaymentValidationErrors.h"
@@ -473,14 +474,11 @@ ExceptionOr<void> ApplePayPaymentHandler::paymentMethodUpdated()
     ASSERT(m_isUpdating);
     m_isUpdating = false;
 
-    PaymentMethodUpdate update;
-
     auto newTotalAndLineItems = computeTotalAndLineItems();
     if (newTotalAndLineItems.hasException())
         return newTotalAndLineItems.releaseException();
-    update.newTotalAndLineItems = newTotalAndLineItems.releaseReturnValue();
 
-    paymentCoordinator().completePaymentMethodSelection(WTFMove(update));
+    paymentCoordinator().completePaymentMethodSelection(PaymentMethodUpdate { newTotalAndLineItems.releaseReturnValue() });
     return { };
 }
 
index a1ba2c9..3f88587 100644 (file)
@@ -348,16 +348,20 @@ NS_ASSUME_NONNULL_BEGIN
 @property (nonatomic, assign) PKPaymentAuthorizationStatus status;
 @end
 
-@interface PKPaymentRequestPaymentMethodUpdate : NSObject
-- (instancetype)initWithPaymentSummaryItems:(nonnull NSArray<PKPaymentSummaryItem *> *)paymentSummaryItems;
+@interface PKPaymentRequestUpdate : NSObject
+- (instancetype)initWithPaymentSummaryItems:(NSArray<PKPaymentSummaryItem *> *)paymentSummaryItems;
+@property (nonatomic, copy) NSArray<PKPaymentSummaryItem *> *paymentSummaryItems;
 @end
 
-@interface PKPaymentRequestUpdate : NSObject
+@interface PKPaymentRequestPaymentMethodUpdate : PKPaymentRequestUpdate
 @end
 
 @interface PKPaymentRequestShippingContactUpdate : PKPaymentRequestUpdate
-- (instancetype)initWithPaymentSummaryItems:(nonnull NSArray<PKPaymentSummaryItem *> *)summaryItems shippingMethods:(nonnull NSArray<PKShippingMethod *> *)shippingMethods;
 - (instancetype)initWithErrors:(nullable NSArray<NSError *> *)errors paymentSummaryItems:(nonnull NSArray<PKPaymentSummaryItem *> *)summaryItems shippingMethods:(nonnull NSArray<PKShippingMethod *> *)shippingMethods;
+@property (nonatomic, copy) NSArray<PKShippingMethod *> *shippingMethods;
+@end
+
+@interface PKPaymentRequestShippingMethodUpdate : PKPaymentRequestUpdate
 @end
 
 NS_ASSUME_NONNULL_END
index 525bafc..a0692a8 100644 (file)
@@ -592,7 +592,9 @@ WHLSLStandardLibraryFunctionMap.cpp
     Modules/applepay/cocoa/PaymentContactCocoa.mm
     Modules/applepay/cocoa/PaymentMerchantSessionCocoa.mm
     Modules/applepay/cocoa/PaymentMethodCocoa.mm
+    Modules/applepay/cocoa/PaymentMethodUpdateCocoa.mm
     Modules/applepay/cocoa/PaymentSessionErrorCocoa.mm
+    Modules/applepay/cocoa/PaymentSummaryItemsCocoa.mm
 
     Modules/applepay/paymentrequest/ApplePayPaymentHandler.cpp
 
index 7e4d307..300fc4b 100644 (file)
                A1DF5AA61F7F64BB0058A477 /* PaymentHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = A1DF5AA41F7F64BB0058A477 /* PaymentHandler.h */; };
                A1DF5AAD1F8075F00058A477 /* ApplePayRequestBase.h in Headers */ = {isa = PBXBuildFile; fileRef = A1DF5AAA1F8075F00058A477 /* ApplePayRequestBase.h */; };
                A1DF5AB31F8077530058A477 /* JSApplePayRequestBase.h in Headers */ = {isa = PBXBuildFile; fileRef = A1DF5AB11F8077520058A477 /* JSApplePayRequestBase.h */; };
+               A1DFD5252334593F0042FCDB /* PaymentMethodUpdate.h in Headers */ = {isa = PBXBuildFile; fileRef = A1DFD5242334593A0042FCDB /* PaymentMethodUpdate.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               A1DFD526233459480042FCDB /* PaymentSummaryItems.h in Headers */ = {isa = PBXBuildFile; fileRef = A1DFD5222334593A0042FCDB /* PaymentSummaryItems.h */; settings = {ATTRIBUTES = (Private, ); }; };
                A1ED778D1BE3294000DC1791 /* Device.h in Headers */ = {isa = PBXBuildFile; fileRef = A1ED778B1BE3294000DC1791 /* Device.h */; settings = {ATTRIBUTES = (Private, ); }; };
                A1F600441F4757A90077E83F /* PaymentRequestUpdateEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = A1F600411F4757A90077E83F /* PaymentRequestUpdateEvent.h */; };
                A1F600491F47594E0077E83F /* PaymentDetailsUpdate.h in Headers */ = {isa = PBXBuildFile; fileRef = A1F600471F47594E0077E83F /* PaymentDetailsUpdate.h */; };
                A1DF5AB01F8077520058A477 /* JSApplePayRequestBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSApplePayRequestBase.cpp; sourceTree = "<group>"; };
                A1DF5AB11F8077520058A477 /* JSApplePayRequestBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSApplePayRequestBase.h; sourceTree = "<group>"; };
                A1DF5AB41F8078F30058A477 /* ApplePayContactField.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ApplePayContactField.cpp; sourceTree = "<group>"; };
+               A1DFD5222334593A0042FCDB /* PaymentSummaryItems.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PaymentSummaryItems.h; sourceTree = "<group>"; };
+               A1DFD5242334593A0042FCDB /* PaymentMethodUpdate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PaymentMethodUpdate.h; sourceTree = "<group>"; };
+               A1DFD527233459730042FCDB /* PaymentSummaryItemsCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PaymentSummaryItemsCocoa.mm; sourceTree = "<group>"; };
+               A1DFD528233459740042FCDB /* PaymentMethodUpdateCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PaymentMethodUpdateCocoa.mm; sourceTree = "<group>"; };
                A1E1154313015C3D0054AC8C /* DistantLightSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DistantLightSource.cpp; sourceTree = "<group>"; };
                A1E1154513015C4E0054AC8C /* PointLightSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PointLightSource.cpp; sourceTree = "<group>"; };
                A1E1154713015C5D0054AC8C /* SpotLightSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SpotLightSource.cpp; sourceTree = "<group>"; };
                                7CF930E61E01F9AD00BAFFBE /* PaymentHeaders.h */,
                                1AE96A871D1A0CEB00B86768 /* PaymentMerchantSession.h */,
                                1A8A64391D19FC5300D0E00F /* PaymentMethod.h */,
+                               A1DFD5242334593A0042FCDB /* PaymentMethodUpdate.h */,
                                1A8A64661D19FDFF00D0E00F /* PaymentRequestValidator.h */,
                                1A8A64651D19FDFF00D0E00F /* PaymentRequestValidator.mm */,
                                A1C2C5751FA2851600BA46FA /* PaymentSession.cpp */,
                                A1491DA21F859D870095F5D4 /* PaymentSession.h */,
                                A113E02723318293009C33A0 /* PaymentSessionError.h */,
+                               A1DFD5222334593A0042FCDB /* PaymentSummaryItems.h */,
                        );
                        path = applepay;
                        sourceTree = "<group>";
                                1A8A646A1D19FF8700D0E00F /* PaymentContactCocoa.mm */,
                                1ADA4A1B1D22F2C0005A9A15 /* PaymentMerchantSessionCocoa.mm */,
                                1A8A646B1D19FF8700D0E00F /* PaymentMethodCocoa.mm */,
+                               A1DFD528233459740042FCDB /* PaymentMethodUpdateCocoa.mm */,
                                A113E02A23318EBC009C33A0 /* PaymentSessionErrorCocoa.mm */,
+                               A1DFD527233459730042FCDB /* PaymentSummaryItemsCocoa.mm */,
                        );
                        path = cocoa;
                        sourceTree = "<group>";
                                1A8A643C1D19FC5300D0E00F /* PaymentMethod.h in Headers */,
                                A164A2472134BC7100509156 /* PaymentMethodChangeEvent.h in Headers */,
                                A1F76B191F44C3900014C318 /* PaymentMethodData.h in Headers */,
+                               A1DFD5252334593F0042FCDB /* PaymentMethodUpdate.h in Headers */,
                                A1F76B2B1F44C4EF0014C318 /* PaymentOptions.h in Headers */,
                                A1F76B131F44C2450014C318 /* PaymentRequest.h in Headers */,
                                A1F600441F4757A90077E83F /* PaymentRequestUpdateEvent.h in Headers */,
                                A113E02823318293009C33A0 /* PaymentSessionError.h in Headers */,
                                A1F76B551F44D2C70014C318 /* PaymentShippingOption.h in Headers */,
                                A1F76B491F44D07A0014C318 /* PaymentShippingType.h in Headers */,
+                               A1DFD526233459480042FCDB /* PaymentSummaryItems.h in Headers */,
                                A1677DFD213E006600A08C34 /* PaymentValidationErrors.h in Headers */,
                                B27535650B053814002CE64F /* PDFDocumentImage.h in Headers */,
                                5E2C43511BCEE2F60001E2BC /* PeerConnectionBackend.h in Headers */,
index 03605d5..fd7fddd 100644 (file)
@@ -34,6 +34,7 @@
 #include "MockPaymentMethod.h"
 #include "Page.h"
 #include "PaymentCoordinator.h"
+#include "PaymentMethodUpdate.h"
 #include "PaymentSessionError.h"
 #include <wtf/CompletionHandler.h>
 #include <wtf/RunLoop.h>
@@ -160,7 +161,7 @@ void MockPaymentCoordinator::completeShippingContactSelection(Optional<ShippingC
 void MockPaymentCoordinator::completePaymentMethodSelection(Optional<PaymentMethodUpdate>&& paymentMethodUpdate)
 {
     if (paymentMethodUpdate)
-        updateTotalAndLineItems(paymentMethodUpdate->newTotalAndLineItems);
+        updateTotalAndLineItems(paymentMethodUpdate->totalAndLineItems());
 }
 
 void MockPaymentCoordinator::changeShippingOption(String&& shippingOption)
index 5a16de2..14cc840 100644 (file)
@@ -1,3 +1,42 @@
+2019-09-19  Andy Estes  <aestes@apple.com>
+
+        [Apple Pay] Clean up handling of summary items and payment method updates
+        https://bugs.webkit.org/show_bug.cgi?id=202018
+        <rdar://problem/55470632>
+
+        Reviewed by Tim Horton.
+
+        Now that PaymentMethodUpdate knows how to convert itself to a
+        PKPaymentRequestPaymentMethodUpdate, PaymentAuthorizationPresenter can merely pass the
+        converted update directly to the platform delegate rather than passing the individual
+        components and relying on the delegate to instantiate the platform update itself. Added
+        FIXMEs for applying a similar treatment to ShippingContactUpdate and ShippingMethodUpdate.
+
+        * Platform/cocoa/PaymentAuthorizationPresenter.h:
+        * Platform/cocoa/PaymentAuthorizationPresenter.mm:
+        (WebKit::PaymentAuthorizationPresenter::completePaymentMethodSelection):
+        (WebKit::PaymentAuthorizationPresenter::completeShippingContactSelection):
+        (WebKit::PaymentAuthorizationPresenter::completeShippingMethodSelection):
+        * Platform/cocoa/WKPaymentAuthorizationDelegate.h:
+        * Platform/cocoa/WKPaymentAuthorizationDelegate.mm:
+        (-[WKPaymentAuthorizationDelegate completePaymentMethodSelection:]):
+        (-[WKPaymentAuthorizationDelegate completeShippingContactSelection:]):
+        (-[WKPaymentAuthorizationDelegate completeShippingMethodSelection:]):
+        (-[WKPaymentAuthorizationDelegate _didSelectPaymentMethod:completion:]):
+        (-[WKPaymentAuthorizationDelegate _didSelectShippingContact:completion:]):
+        (-[WKPaymentAuthorizationDelegate _didSelectShippingMethod:completion:]):
+        (-[WKPaymentAuthorizationDelegate completeShippingContactSelection:summaryItems:shippingMethods:errors:]): Deleted.
+        * Shared/ApplePay/WebPaymentCoordinatorProxy.h:
+        * Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.h:
+        * Shared/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.mm:
+        (WebKit::WebPaymentCoordinatorProxy::platformPaymentRequest):
+        (WebKit::toPKPaymentSummaryItemType): Deleted.
+        (WebKit::toPKPaymentSummaryItem): Deleted.
+        (WebKit::toPKPaymentSummaryItems): Deleted.
+        * Shared/Cocoa/WebCoreArgumentCodersCocoa.mm:
+        (IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::encode):
+        (IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::decode):
+
 2019-09-20  Keith Rollin  <krollin@apple.com>
 
         Remove some support for < iOS 13
index cb64af9..d062784 100644 (file)
@@ -41,6 +41,7 @@ class Payment;
 class PaymentContact;
 class PaymentMerchantSession;
 class PaymentMethod;
+class PaymentMethodUpdate;
 class PaymentSessionError;
 }
 
index 12eab15..8e8f5f7 100644 (file)
@@ -32,6 +32,8 @@
 #import "WebPaymentCoordinatorProxyCocoa.h"
 #import <WebCore/PaymentAuthorizationStatus.h>
 #import <WebCore/PaymentMerchantSession.h>
+#import <WebCore/PaymentMethodUpdate.h>
+#import <WebCore/PaymentSummaryItems.h>
 #import <pal/cocoa/PassKitSoftLink.h>
 
 SOFT_LINK_FRAMEWORK(Contacts);
@@ -185,8 +187,12 @@ void PaymentAuthorizationPresenter::completeMerchantValidation(const WebCore::Pa
 void PaymentAuthorizationPresenter::completePaymentMethodSelection(const Optional<WebCore::PaymentMethodUpdate>& update)
 {
     ASSERT(platformDelegate());
-    NSArray *summaryItems = update ? toPKPaymentSummaryItems(update->newTotalAndLineItems) : platformDelegate().summaryItems;
-    [platformDelegate() completePaymentMethodSelection:summaryItems];
+    if (!update) {
+        [platformDelegate() completePaymentMethodSelection:nil];
+        return;
+    }
+
+    [platformDelegate() completePaymentMethodSelection:update->platformUpdate()];
 }
 
 void PaymentAuthorizationPresenter::completePaymentSession(const Optional<WebCore::PaymentAuthorizationResult>& result)
@@ -200,18 +206,27 @@ void PaymentAuthorizationPresenter::completePaymentSession(const Optional<WebCor
 void PaymentAuthorizationPresenter::completeShippingContactSelection(const Optional<WebCore::ShippingContactUpdate>& update)
 {
     ASSERT(platformDelegate());
-    NSArray *shippingMethods = update ? toPKShippingMethods(update->newShippingMethods) : platformDelegate().shippingMethods;
-    NSArray *summaryItems = update ? toPKPaymentSummaryItems(update->newTotalAndLineItems) : platformDelegate().summaryItems;
-    NSArray *errors = update ? toNSErrors(update->errors) : @[ ];
-    auto status = PKPaymentAuthorizationStatusSuccess;
-    [platformDelegate() completeShippingContactSelection:status summaryItems:summaryItems shippingMethods:shippingMethods errors:errors];
+    if (!update) {
+        [platformDelegate() completeShippingContactSelection:nil];
+        return;
+    }
+
+    // FIXME: WebCore::ShippingContactUpdate should know how to convert itself to a PKPaymentRequestShippingContactUpdate.
+    auto shippingContactUpdate = adoptNS([PAL::allocPKPaymentRequestShippingContactUpdateInstance() initWithErrors:toNSErrors(update->errors) paymentSummaryItems:WebCore::platformSummaryItems(update->newTotalAndLineItems) shippingMethods:toPKShippingMethods(update->newShippingMethods)]);
+    [platformDelegate() completeShippingContactSelection:shippingContactUpdate.get()];
 }
 
 void PaymentAuthorizationPresenter::completeShippingMethodSelection(const Optional<WebCore::ShippingMethodUpdate>& update)
 {
     ASSERT(platformDelegate());
-    NSArray *summaryItems = update ? toPKPaymentSummaryItems(update->newTotalAndLineItems) : platformDelegate().summaryItems;
-    [platformDelegate() completeShippingMethodSelection:summaryItems];
+    if (!update) {
+        [platformDelegate() completeShippingMethodSelection:nil];
+        return;
+    }
+
+    // FIXME: WebCore::ShippingMethodUpdate should know how to convert itself to a PKPaymentRequestShippingMethodUpdate.
+    auto shippingMethodUpdate = adoptNS([PAL::allocPKPaymentRequestShippingMethodUpdateInstance() initWithPaymentSummaryItems:WebCore::platformSummaryItems(update->newTotalAndLineItems)]);
+    [platformDelegate() completeShippingMethodSelection:shippingMethodUpdate.get()];
 }
 
 } // namespace WebKit
index 4033a3c..c935f27 100644 (file)
@@ -52,15 +52,11 @@ using DidSelectShippingMethodCompletion = BlockPtr<void(PKPaymentRequestShipping
 
 - (instancetype)init NS_UNAVAILABLE;
 
-@property (nonatomic, readonly) BOOL didReachFinalState;
-@property (nonatomic, readonly) NSArray<PKPaymentSummaryItem *> *summaryItems;
-@property (nonatomic, readonly) NSArray<PKShippingMethod *> *shippingMethods;
-
 - (void)completeMerchantValidation:(PKPaymentMerchantSession *)session error:(NSError *)error;
-- (void)completePaymentMethodSelection:(NSArray<PKPaymentSummaryItem *> *)summaryItems;
+- (void)completePaymentMethodSelection:(PKPaymentRequestPaymentMethodUpdate *)paymentMethodUpdate;
 - (void)completePaymentSession:(PKPaymentAuthorizationStatus)status errors:(NSArray<NSError *> *)errors didReachFinalState:(BOOL)didReachFinalState;
-- (void)completeShippingContactSelection:(PKPaymentAuthorizationStatus)status summaryItems:(NSArray<PKPaymentSummaryItem *> *)summaryItems shippingMethods:(NSArray<PKShippingMethod *> *)shippingMethods errors:(NSArray<NSError *> *)errors;
-- (void)completeShippingMethodSelection:(NSArray<PKPaymentSummaryItem *> *)summaryItems;
+- (void)completeShippingContactSelection:(PKPaymentRequestShippingContactUpdate *)shippingContactUpdate;
+- (void)completeShippingMethodSelection:(PKPaymentRequestShippingMethodUpdate *)shippingMethodUpdate;
 - (void)invalidate;
 
 @end
index fa3e8bb..c90fff7 100644 (file)
@@ -33,6 +33,7 @@
 #import <WebCore/PaymentSessionError.h>
 
 @implementation WKPaymentAuthorizationDelegate {
+    BOOL _didReachFinalState;
     RetainPtr<NSArray<PKPaymentSummaryItem *>> _summaryItems;
     RetainPtr<NSArray<PKShippingMethod *>> _shippingMethods;
     RetainPtr<NSError> _sessionError;
     std::exchange(_didRequestMerchantSessionCompletion, nil)(session, error);
 }
 
-- (void)completePaymentMethodSelection:(NSArray<PKPaymentSummaryItem *> *)summaryItems
+- (void)completePaymentMethodSelection:(PKPaymentRequestPaymentMethodUpdate *)paymentMethodUpdate
 {
-    _summaryItems = summaryItems;
-    auto update = adoptNS([PAL::allocPKPaymentRequestPaymentMethodUpdateInstance() initWithPaymentSummaryItems:_summaryItems.get()]);
-    std::exchange(_didSelectPaymentMethodCompletion, nil)(update.get());
+    PKPaymentRequestPaymentMethodUpdate *update = paymentMethodUpdate ?: [[PAL::allocPKPaymentRequestPaymentMethodUpdateInstance() initWithPaymentSummaryItems:_summaryItems.get()] autorelease];
+    _summaryItems = adoptNS([update.paymentSummaryItems copy]);
+    std::exchange(_didSelectPaymentMethodCompletion, nil)(update);
 }
 
 - (void)completePaymentSession:(PKPaymentAuthorizationStatus)status errors:(NSArray<NSError *> *)errors didReachFinalState:(BOOL)didReachFinalState
     auto result = adoptNS([PAL::allocPKPaymentAuthorizationResultInstance() initWithStatus:status errors:errors]);
     std::exchange(_didAuthorizePaymentCompletion, nil)(result.get());
 }
-
-- (void)completeShippingContactSelection:(PKPaymentAuthorizationStatus)status summaryItems:(NSArray<PKPaymentSummaryItem *> *)summaryItems shippingMethods:(NSArray<PKShippingMethod *> *)shippingMethods errors:(NSArray<NSError *> *)errors
+- (void)completeShippingContactSelection:(PKPaymentRequestShippingContactUpdate *)shippingContactUpdate
 {
-    _summaryItems = summaryItems;
-    _shippingMethods = shippingMethods;
-    ASSERT(status == PKPaymentAuthorizationStatusSuccess);
-    auto update = adoptNS([PAL::allocPKPaymentRequestShippingContactUpdateInstance() initWithErrors:errors paymentSummaryItems:_summaryItems.get() shippingMethods:_shippingMethods.get()]);
-    std::exchange(_didSelectShippingContactCompletion, nil)(update.get());
+    PKPaymentRequestShippingContactUpdate *update = shippingContactUpdate ?: [[PAL::allocPKPaymentRequestShippingContactUpdateInstance() initWithErrors:@[] paymentSummaryItems:_summaryItems.get() shippingMethods:_shippingMethods.get()] autorelease];
+    _summaryItems = adoptNS([update.paymentSummaryItems copy]);
+    _shippingMethods = adoptNS([update.shippingMethods copy]);
+    std::exchange(_didSelectShippingContactCompletion, nil)(update);
 }
 
-- (void)completeShippingMethodSelection:(NSArray<PKPaymentSummaryItem *> *)summaryItems
+- (void)completeShippingMethodSelection:(PKPaymentRequestShippingMethodUpdate *)shippingMethodUpdate
 {
-    _summaryItems = summaryItems;
-    auto update = adoptNS([PAL::allocPKPaymentRequestShippingMethodUpdateInstance() initWithPaymentSummaryItems:_summaryItems.get()]);
-    std::exchange(_didSelectShippingMethodCompletion, nil)(update.get());
+    PKPaymentRequestShippingMethodUpdate *update = shippingMethodUpdate ?: [[PAL::allocPKPaymentRequestShippingMethodUpdateInstance() initWithPaymentSummaryItems:_summaryItems.get()] autorelease];
+    _summaryItems = adoptNS([update.paymentSummaryItems copy]);
+    std::exchange(_didSelectShippingMethodCompletion, nil)(update);
 }
 
 - (void)invalidate
 
     auto presenter = _presenter.get();
     if (!presenter)
-        return [self completePaymentMethodSelection:@[ ]];
+        return [self completePaymentMethodSelection:nil];
 
     presenter->client().presenterDidSelectPaymentMethod(*presenter, WebCore::PaymentMethod(paymentMethod));
 }
 
     auto presenter = _presenter.get();
     if (!presenter)
-        return [self completeShippingContactSelection:PKPaymentAuthorizationStatusFailure summaryItems:@[ ] shippingMethods:@[ ] errors:@[ ]];
+        return [self completeShippingContactSelection:nil];
 
     presenter->client().presenterDidSelectShippingContact(*presenter, WebCore::PaymentContact(contact));
 }
@@ -196,7 +195,7 @@ static WebCore::ApplePaySessionPaymentRequest::ShippingMethod toShippingMethod(P
 
     auto presenter = _presenter.get();
     if (!presenter)
-        return [self completeShippingMethodSelection:@[ ]];
+        return [self completeShippingMethodSelection:nil];
 
     presenter->client().presenterDidSelectShippingMethod(*presenter, toShippingMethod(shippingMethod));
 }
index 6b36773..467cae4 100644 (file)
@@ -55,6 +55,7 @@ class Payment;
 class PaymentContact;
 class PaymentMerchantSession;
 class PaymentMethod;
+class PaymentMethodUpdate;
 }
 
 OBJC_CLASS NSObject;
index 4e942b9..2d6408b 100644 (file)
@@ -33,10 +33,7 @@ namespace WebKit {
 
 // FIXME: Rather than having these free functions scattered about, Apple Pay data types should know
 // how to convert themselves to and from their platform representations.
-NSArray *toPKPaymentSummaryItems(const WebCore::ApplePaySessionPaymentRequest::TotalAndLineItems&);
 NSDecimalNumber *toDecimalNumber(const String& amount);
-PKPaymentSummaryItem *toPKPaymentSummaryItem(const WebCore::ApplePaySessionPaymentRequest::LineItem&);
-PKPaymentSummaryItemType toPKPaymentSummaryItemType(WebCore::ApplePaySessionPaymentRequest::LineItem::Type);
 PKShippingMethod *toPKShippingMethod(const WebCore::ApplePaySessionPaymentRequest::ShippingMethod&);
 
 } // namespace WebKit
index 875c349..5915698 100644 (file)
@@ -109,17 +109,6 @@ static RetainPtr<NSSet> toPKContactFields(const WebCore::ApplePaySessionPaymentR
     return adoptNS([[NSSet alloc] initWithObjects:result.data() count:result.size()]);
 }
 
-PKPaymentSummaryItemType toPKPaymentSummaryItemType(WebCore::ApplePaySessionPaymentRequest::LineItem::Type type)
-{
-    switch (type) {
-    case WebCore::ApplePaySessionPaymentRequest::LineItem::Type::Final:
-        return PKPaymentSummaryItemTypeFinal;
-
-    case WebCore::ApplePaySessionPaymentRequest::LineItem::Type::Pending:
-        return PKPaymentSummaryItemTypePending;
-    }
-}
-
 NSDecimalNumber *toDecimalNumber(const String& amount)
 {
     if (!amount)
@@ -127,25 +116,6 @@ NSDecimalNumber *toDecimalNumber(const String& amount)
     return [NSDecimalNumber decimalNumberWithString:amount locale:@{ NSLocaleDecimalSeparator : @"." }];
 }
 
-PKPaymentSummaryItem *toPKPaymentSummaryItem(const WebCore::ApplePaySessionPaymentRequest::LineItem& lineItem)
-{
-    return [PAL::getPKPaymentSummaryItemClass() summaryItemWithLabel:lineItem.label amount:toDecimalNumber(lineItem.amount) type:toPKPaymentSummaryItemType(lineItem.type)];
-}
-
-NSArray *toPKPaymentSummaryItems(const WebCore::ApplePaySessionPaymentRequest::TotalAndLineItems& totalAndLineItems)
-{
-    NSMutableArray *paymentSummaryItems = [NSMutableArray arrayWithCapacity:totalAndLineItems.lineItems.size() + 1];
-    for (auto& lineItem : totalAndLineItems.lineItems) {
-        if (PKPaymentSummaryItem *summaryItem = toPKPaymentSummaryItem(lineItem))
-            [paymentSummaryItems addObject:summaryItem];
-    }
-
-    if (PKPaymentSummaryItem *totalItem = toPKPaymentSummaryItem(totalAndLineItems.total))
-        [paymentSummaryItems addObject:totalItem];
-
-    return paymentSummaryItems;
-}
-
 static PKMerchantCapability toPKMerchantCapabilities(const WebCore::ApplePaySessionPaymentRequest::MerchantCapabilities& merchantCapabilities)
 {
     PKMerchantCapability result = 0;
@@ -256,16 +226,7 @@ RetainPtr<PKPaymentRequest> WebPaymentCoordinatorProxy::platformPaymentRequest(c
         [shippingMethods addObject:toPKShippingMethod(shippingMethod)];
     [result setShippingMethods:shippingMethods.get()];
 
-    auto paymentSummaryItems = adoptNS([[NSMutableArray alloc] init]);
-    for (auto& lineItem : paymentRequest.lineItems()) {
-        if (PKPaymentSummaryItem *summaryItem = toPKPaymentSummaryItem(lineItem))
-            [paymentSummaryItems addObject:summaryItem];
-    }
-
-    if (PKPaymentSummaryItem *totalItem = toPKPaymentSummaryItem(paymentRequest.total()))
-        [paymentSummaryItems addObject:totalItem];
-
-    [result setPaymentSummaryItems:paymentSummaryItems.get()];
+    [result setPaymentSummaryItems:WebCore::platformSummaryItems(paymentRequest.total(), paymentRequest.lineItems())];
 
     [result setExpectsMerchantSession:YES];
 
index ee00377..32bf80c 100644 (file)
@@ -161,16 +161,16 @@ Optional<WebCore::PaymentMethod> ArgumentCoder<WebCore::PaymentMethod>::decode(D
 
 void ArgumentCoder<WebCore::PaymentMethodUpdate>::encode(Encoder& encoder, const WebCore::PaymentMethodUpdate& update)
 {
-    encoder << update.newTotalAndLineItems;
+    encoder << update.platformUpdate();
 }
 
 Optional<WebCore::PaymentMethodUpdate> ArgumentCoder<WebCore::PaymentMethodUpdate>::decode(Decoder& decoder)
 {
-    Optional<ApplePaySessionPaymentRequest::TotalAndLineItems> newTotalAndLineItems;
-    decoder >> newTotalAndLineItems;
-    if (!newTotalAndLineItems)
+    auto update = IPC::decode<PKPaymentRequestPaymentMethodUpdate>(decoder, PAL::getPKPaymentRequestPaymentMethodUpdateClass());
+    if (!update)
         return WTF::nullopt;
-    return {{ WTFMove(*newTotalAndLineItems) }};
+
+    return PaymentMethodUpdate { WTFMove(*update) };
 }
 
 void ArgumentCoder<ApplePaySessionPaymentRequest>::encode(Encoder& encoder, const ApplePaySessionPaymentRequest& request)