6ae68cdf8326ba05638026842e9eaf24af0ddf18
[WebKit-https.git] / Source / WebCore / Modules / applepay / cocoa / PaymentMethodCocoa.mm
1 /*
2  * Copyright (C) 2016 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 #import "config.h"
27 #import "PaymentMethod.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #import "ApplePayPaymentMethod.h"
32 #import "ApplePayPaymentMethodType.h"
33 #import <pal/spi/cocoa/PassKitSPI.h>
34
35 namespace WebCore {
36
37 static ApplePayPaymentPass::ActivationState convert(PKPaymentPassActivationState paymentPassActivationState)
38 {
39     switch (paymentPassActivationState) {
40     case PKPaymentPassActivationStateActivated:
41         return ApplePayPaymentPass::ActivationState::Activated;
42     case PKPaymentPassActivationStateRequiresActivation:
43         return ApplePayPaymentPass::ActivationState::RequiresActivation;
44     case PKPaymentPassActivationStateActivating:
45         return ApplePayPaymentPass::ActivationState::Activating;
46     case PKPaymentPassActivationStateSuspended:
47         return ApplePayPaymentPass::ActivationState::Suspended;
48     case PKPaymentPassActivationStateDeactivated:
49         return ApplePayPaymentPass::ActivationState::Deactivated;
50     }
51 }
52
53 static std::optional<ApplePayPaymentPass> convert(PKPaymentPass *paymentPass)
54 {
55     if (!paymentPass)
56         return std::nullopt;
57
58     ApplePayPaymentPass result;
59
60     result.primaryAccountIdentifier = paymentPass.primaryAccountIdentifier;
61     result.primaryAccountNumberSuffix = paymentPass.primaryAccountNumberSuffix;
62
63     if (NSString *deviceAccountIdentifier = paymentPass.deviceAccountIdentifier)
64         result.deviceAccountIdentifier = deviceAccountIdentifier;
65     if (NSString *deviceAccountNumberSuffix = paymentPass.deviceAccountNumberSuffix)
66         result.deviceAccountNumberSuffix = deviceAccountNumberSuffix;
67
68     result.activationState = convert(paymentPass.activationState);
69
70     return result;
71 }
72
73 static std::optional<ApplePayPaymentMethod::Type> convert(PKPaymentMethodType paymentMethodType)
74 {
75     switch (paymentMethodType) {
76     case PKPaymentMethodTypeDebit:
77         return ApplePayPaymentMethod::Type::Debit;
78     case PKPaymentMethodTypeCredit:
79         return ApplePayPaymentMethod::Type::Credit;
80     case PKPaymentMethodTypePrepaid:
81         return ApplePayPaymentMethod::Type::Prepaid;
82     case PKPaymentMethodTypeStore:
83         return ApplePayPaymentMethod::Type::Store;
84     case PKPaymentMethodTypeUnknown:
85         return std::nullopt;
86     }
87 }
88
89 static ApplePayPaymentMethod convert(PKPaymentMethod *paymentMethod)
90 {
91     ApplePayPaymentMethod result;
92     
93     if (NSString *displayName = paymentMethod.displayName)
94         result.displayName = displayName;
95     if (NSString *network = paymentMethod.network)
96         result.network = network;
97
98     result.type = convert(paymentMethod.type);
99     result.paymentPass = convert(paymentMethod.paymentPass);
100
101     return result;
102 }
103
104 ApplePayPaymentMethod PaymentMethod::toApplePayPaymentMethod() const
105 {
106     return convert(m_pkPaymentMethod.get());
107 }
108
109 }
110
111 #endif