MockPaymentCoordinator.cpp [plain text]
#include "config.h"
#include "MockPaymentCoordinator.h"
#if ENABLE(APPLE_PAY)
#include "ApplePaySessionPaymentRequest.h"
#include "MockPayment.h"
#include "MockPaymentContact.h"
#include "MockPaymentMethod.h"
#include "Page.h"
#include "PaymentCoordinator.h"
#include <wtf/RunLoop.h>
#include <wtf/URL.h>
namespace WebCore {
MockPaymentCoordinator::MockPaymentCoordinator(Page& page)
: m_page { page }
{
m_availablePaymentNetworks.add("amex");
m_availablePaymentNetworks.add("carteBancaire");
m_availablePaymentNetworks.add("chinaUnionPay");
m_availablePaymentNetworks.add("discover");
m_availablePaymentNetworks.add("interac");
m_availablePaymentNetworks.add("jcb");
m_availablePaymentNetworks.add("masterCard");
m_availablePaymentNetworks.add("privateLabel");
m_availablePaymentNetworks.add("visa");
}
Optional<String> MockPaymentCoordinator::validatedPaymentNetwork(const String& paymentNetwork)
{
auto result = m_availablePaymentNetworks.find(paymentNetwork);
if (result == m_availablePaymentNetworks.end())
return WTF::nullopt;
return *result;
}
bool MockPaymentCoordinator::canMakePayments()
{
return m_canMakePayments;
}
void MockPaymentCoordinator::canMakePaymentsWithActiveCard(const String&, const String&, Function<void(bool)>&& completionHandler)
{
RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), canMakePaymentsWithActiveCard = m_canMakePaymentsWithActiveCard] {
completionHandler(canMakePaymentsWithActiveCard);
});
}
void MockPaymentCoordinator::openPaymentSetup(const String&, const String&, Function<void(bool)>&& completionHandler)
{
RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)] {
completionHandler(true);
});
}
static uint64_t showCount;
static uint64_t hideCount;
static void dispatchIfShowing(Function<void()>&& function)
{
ASSERT(showCount > hideCount);
RunLoop::main().dispatch([currentShowCount = showCount, function = WTFMove(function)]() {
if (showCount > hideCount && showCount == currentShowCount)
function();
});
}
static Vector<ApplePayShippingMethod> convert(const Vector<ApplePaySessionPaymentRequest::ShippingMethod>& shippingMethods)
{
return WTF::map(shippingMethods, [] (auto& shippingMethod) {
return ApplePayShippingMethod { shippingMethod.label, shippingMethod.detail, shippingMethod.amount, shippingMethod.identifier };
});
}
bool MockPaymentCoordinator::showPaymentUI(const URL&, const Vector<URL>&, const ApplePaySessionPaymentRequest& request)
{
if (request.shippingContact().pkContact())
m_shippingAddress = request.shippingContact().toApplePayPaymentContact(request.version());
m_shippingMethods = convert(request.shippingMethods());
m_requiredBillingContactFields = request.requiredBillingContactFields();
m_requiredShippingContactFields = request.requiredShippingContactFields();
ASSERT(showCount == hideCount);
++showCount;
dispatchIfShowing([page = &m_page]() {
page->paymentCoordinator().validateMerchant({ URL(), "https://webkit.org/"_s });
});
return true;
}
void MockPaymentCoordinator::completeMerchantValidation(const PaymentMerchantSession&)
{
dispatchIfShowing([page = &m_page, shippingAddress = m_shippingAddress]() mutable {
page->paymentCoordinator().didSelectShippingContact(MockPaymentContact { WTFMove(shippingAddress) });
});
}
static ApplePayLineItem convert(const ApplePaySessionPaymentRequest::LineItem& lineItem)
{
ApplePayLineItem result;
result.type = lineItem.type;
result.label = lineItem.label;
result.amount = lineItem.amount;
return result;
}
void MockPaymentCoordinator::updateTotalAndLineItems(const ApplePaySessionPaymentRequest::TotalAndLineItems& totalAndLineItems)
{
m_total = convert(totalAndLineItems.total);
m_lineItems.clear();
for (auto& lineItem : totalAndLineItems.lineItems)
m_lineItems.append(convert(lineItem));
}
void MockPaymentCoordinator::completeShippingMethodSelection(Optional<ShippingMethodUpdate>&& shippingMethodUpdate)
{
if (shippingMethodUpdate)
updateTotalAndLineItems(shippingMethodUpdate->newTotalAndLineItems);
}
void MockPaymentCoordinator::completeShippingContactSelection(Optional<ShippingContactUpdate>&& shippingContactUpdate)
{
if (!shippingContactUpdate)
return;
m_shippingMethods = convert(shippingContactUpdate->newShippingMethods);
updateTotalAndLineItems(shippingContactUpdate->newTotalAndLineItems);
m_errors = WTFMove(shippingContactUpdate->errors);
}
void MockPaymentCoordinator::completePaymentMethodSelection(Optional<PaymentMethodUpdate>&& paymentMethodUpdate)
{
if (paymentMethodUpdate)
updateTotalAndLineItems(paymentMethodUpdate->newTotalAndLineItems);
}
void MockPaymentCoordinator::changeShippingOption(String&& shippingOption)
{
dispatchIfShowing([page = &m_page, shippingOption = WTFMove(shippingOption)]() mutable {
ApplePaySessionPaymentRequest::ShippingMethod shippingMethod;
shippingMethod.identifier = WTFMove(shippingOption);
page->paymentCoordinator().didSelectShippingMethod(shippingMethod);
});
}
void MockPaymentCoordinator::changePaymentMethod(ApplePayPaymentMethod&& paymentMethod)
{
dispatchIfShowing([page = &m_page, paymentMethod = WTFMove(paymentMethod)]() mutable {
page->paymentCoordinator().didSelectPaymentMethod(MockPaymentMethod { WTFMove(paymentMethod) });
});
}
void MockPaymentCoordinator::acceptPayment()
{
dispatchIfShowing([page = &m_page, shippingAddress = m_shippingAddress]() mutable {
ApplePayPayment payment;
payment.shippingContact = WTFMove(shippingAddress);
page->paymentCoordinator().didAuthorizePayment(MockPayment { WTFMove(payment) });
});
}
void MockPaymentCoordinator::cancelPayment()
{
dispatchIfShowing([page = &m_page] {
page->paymentCoordinator().didCancelPaymentSession();
++hideCount;
ASSERT(showCount == hideCount);
});
}
void MockPaymentCoordinator::completePaymentSession(Optional<PaymentAuthorizationResult>&& result)
{
auto isFinalState = isFinalStateResult(result);
m_errors = WTFMove(result->errors);
if (!isFinalState)
return;
++hideCount;
ASSERT(showCount == hideCount);
}
void MockPaymentCoordinator::abortPaymentSession()
{
++hideCount;
ASSERT(showCount == hideCount);
}
void MockPaymentCoordinator::cancelPaymentSession()
{
++hideCount;
ASSERT(showCount == hideCount);
}
void MockPaymentCoordinator::paymentCoordinatorDestroyed()
{
ASSERT(showCount == hideCount);
delete this;
}
}
#endif // ENABLE(APPLE_PAY)