Skip to content

Commit 1e98588

Browse files
[AC] Expose .label(label:) modifier on AcceleratedCheckoutButtons (#377)
* feat: expose the appleLabel modifier * refactor: don't expose which rendering style we use internally * refactor: move label enum below component definitions * refactor: reorder button label cases - add continue case * refactor: rename appleLabel -> applePayLabel
1 parent 6208ea8 commit 1e98588

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

Samples/ShopifyAcceleratedCheckoutsApp/ShopifyAcceleratedCheckoutsApp/Views/Components/ButtonSet.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct ButtonSet: View {
4343
) {
4444
// Cart-based checkout example with event handlers
4545
AcceleratedCheckoutButtons(cartID: cartID)
46+
.applePayLabel(.plain)
4647
.onComplete { event in
4748
print(
4849
"✅ Checkout completed successfully. Order ID: \(event.orderDetails.id)"
@@ -92,6 +93,7 @@ struct ButtonSet: View {
9293
variantID: productVariant.id,
9394
quantity: firstVariantQuantity
9495
)
96+
.applePayLabel(.buy)
9597
.cornerRadius(24)
9698
.wallets([.applePay, .shopPay])
9799
.onComplete { event in

Sources/ShopifyAcceleratedCheckouts/Wallets/AcceleratedCheckoutButtons.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public struct AcceleratedCheckoutButtons: View {
4848
var eventHandlers: EventHandlers = .init()
4949
var cornerRadius: CGFloat?
5050

51+
/// The Apple Pay button label style
52+
private var applePayLabel: ApplePayButtonLabel = .plain
53+
5154
@State private var shopSettings: ShopSettings?
5255
@State private var currentRenderState: RenderState = .loading
5356

@@ -86,6 +89,7 @@ public struct AcceleratedCheckoutButtons: View {
8689
eventHandlers: eventHandlers,
8790
cornerRadius: cornerRadius
8891
)
92+
.label(applePayLabel)
8993
case .shopPay:
9094
ShopPayButton(
9195
identifier: identifier,
@@ -129,6 +133,12 @@ public struct AcceleratedCheckoutButtons: View {
129133

130134
@available(iOS 17.0, *)
131135
extension AcceleratedCheckoutButtons {
136+
public func applePayLabel(_ label: ApplePayButtonLabel) -> AcceleratedCheckoutButtons {
137+
var view = self
138+
view.applePayLabel = label
139+
return view
140+
}
141+
132142
/// Modifies the wallet options supported
133143
/// Defaults: [.applePay]
134144
public func wallets(_ wallets: [Wallet]) -> AcceleratedCheckoutButtons {

Sources/ShopifyAcceleratedCheckouts/Wallets/ApplePay/ApplePayButton.swift

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct ApplePayButton: View {
4747
private let eventHandlers: EventHandlers
4848

4949
/// The Apple Pay button label style
50-
private var label: PayWithApplePayButtonLabel = .plain
50+
private var label: ApplePayButtonLabel = .plain
5151

5252
/// The corner radius for the button
5353
private let cornerRadius: CGFloat?
@@ -80,7 +80,7 @@ struct ApplePayButton: View {
8080
}
8181
}
8282

83-
public func withLabel(_ label: PayWithApplePayButtonLabel) -> some View {
83+
public func label(_ label: ApplePayButtonLabel) -> some View {
8484
var view = self
8585
view.label = label
8686
return view
@@ -93,7 +93,7 @@ struct ApplePayButton: View {
9393
@available(macOS, unavailable)
9494
struct Internal_ApplePayButton: View {
9595
/// The Apple Pay button label style
96-
private var label: PayWithApplePayButtonLabel = .plain
96+
private var label: ApplePayButtonLabel = .plain
9797

9898
/// The view controller for the Apple Pay button
9999
private var controller: ApplePayViewController
@@ -109,7 +109,7 @@ struct Internal_ApplePayButton: View {
109109
/// - eventHandlers: The event handlers for checkout events (defaults to EventHandlers())
110110
init(
111111
identifier: CheckoutIdentifier,
112-
label: PayWithApplePayButtonLabel,
112+
label: ApplePayButtonLabel,
113113
configuration: ApplePayConfigurationWrapper,
114114
eventHandlers: EventHandlers = EventHandlers(),
115115
cornerRadius: CGFloat?
@@ -132,15 +132,100 @@ struct Internal_ApplePayButton: View {
132132

133133
var body: some View {
134134
PayWithApplePayButton(
135-
label,
135+
label.toPayWithApplePayButtonLabel,
136136
action: {
137137
Task { await controller.startPayment() }
138138
},
139139
fallback: {
140-
// content == nil ? Text("errors.applePay.unsupported") : content
141140
Text("errors.applePay.unsupported".localizedString)
142141
}
143142
)
144143
.walletButtonStyle(cornerRadius: cornerRadius)
145144
}
146145
}
146+
147+
/// Used to set the label of the Apple Pay button
148+
/// see `.applePayLabel(label:)`
149+
public enum ApplePayButtonLabel {
150+
/// A button with the Apple Pay logo only
151+
case plain
152+
/// A button that uses the phrase "Buy with" in conjunction with the Apple Pay logo
153+
case buy
154+
/// A button that uses the phrase "Add Money with" in conjunction with the Apple Pay logo
155+
case addMoney
156+
/// A button that uses the phrase "Book with" in conjunction with the Apple Pay logo
157+
case book
158+
/// A button that uses the phrase "Check out with" in conjunction with the Apple Pay logo
159+
case checkout
160+
/// A button that uses the phrase "Continue with" in conjunction with the Apple Pay logo
161+
case `continue`
162+
/// A button that uses the phrase "Contribute with" in conjunction with the Apple Pay logo
163+
case contribute
164+
/// A button that uses the phrase "Donate with" in conjunction with the Apple Pay logo
165+
case donate
166+
/// A button that uses the phrase "Pay with" in conjunction with the Apple Pay logo
167+
case inStore
168+
/// A button that uses the phrase "Order with" in conjunction with the Apple Pay logo
169+
case order
170+
/// A button that uses the phrase "Reload with" in conjunction with the Apple Pay logo
171+
case reload
172+
/// A button that uses the phrase "Rent with" in conjunction with the Apple Pay logo
173+
case rent
174+
/// A button that prompts the user to set up Apple Pay
175+
case setUp
176+
/// A button that uses the phrase "Subscribe with" in conjunction with the Apple Pay logo
177+
case subscribe
178+
/// A button that uses the phrase "Support with" in conjunction with the Apple Pay logo
179+
case support
180+
/// A button that uses the phrase "Tip with" in conjunction with the Apple Pay logo
181+
case tip
182+
/// A button that uses the phrase "Top Up with" in conjunction with the Apple Pay logo
183+
case topUp
184+
185+
/// SwiftUI interop - will be removed when migrating to support iOS 15
186+
@available(iOS 17.0, *)
187+
var toPayWithApplePayButtonLabel: PayWithApplePayButtonLabel {
188+
switch self {
189+
case .plain: return .plain
190+
case .buy: return .buy
191+
case .addMoney: return .addMoney
192+
case .book: return .book
193+
case .checkout: return .checkout
194+
case .continue: return .continue
195+
case .contribute: return .contribute
196+
case .donate: return .donate
197+
case .inStore: return .inStore
198+
case .order: return .order
199+
case .reload: return .reload
200+
case .rent: return .rent
201+
case .setUp: return .setUp
202+
case .subscribe: return .subscribe
203+
case .support: return .support
204+
case .tip: return .tip
205+
case .topUp: return .topUp
206+
}
207+
}
208+
209+
@available(iOS 15.0, *)
210+
var toPKPaymentButtonType: PKPaymentButtonType {
211+
switch self {
212+
case .plain: return .plain
213+
case .buy: return .buy
214+
case .addMoney: return .addMoney
215+
case .book: return .book
216+
case .checkout: return .checkout
217+
case .continue: return .continue
218+
case .contribute: return .contribute
219+
case .donate: return .donate
220+
case .inStore: return .inStore
221+
case .order: return .order
222+
case .reload: return .reload
223+
case .rent: return .rent
224+
case .setUp: return .setUp
225+
case .subscribe: return .subscribe
226+
case .support: return .support
227+
case .tip: return .tip
228+
case .topUp: return .topUp
229+
}
230+
}
231+
}

0 commit comments

Comments
 (0)