Skip to content

Commit

Permalink
Consolidate PayPal ViewControllers (#1102)
Browse files Browse the repository at this point in the history
* Consolidate PayPal Checkout, Vault and Pay Later ViewControllers into a single ViewController with all 3 buttons
* Update settings drop down to PayPal - Web Checkout
  • Loading branch information
jaxdesmarais authored Sep 26, 2023
1 parent 8f9e92f commit 77ca7d8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 160 deletions.
8 changes: 2 additions & 6 deletions Demo/Application/Base/Settings/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
<key>Titles</key>
<array>
<string>PayPal - Native Checkout</string>
<string>PayPal - Vault</string>
<string>PayPal - Checkout</string>
<string>PayPal - Pay Later</string>
<string>PayPal - Web Checkout</string>
<string>Credit Card Tokenization</string>
<string>Venmo - Custom Button</string>
<string>Apple Pay - PassKit</string>
Expand All @@ -33,9 +31,7 @@
<key>Values</key>
<array>
<string>BraintreeDemoPayPalNativeCheckoutViewController</string>
<string>BraintreeDemoPayPalVaultViewController</string>
<string>BraintreeDemoPayPalCheckoutViewController</string>
<string>BraintreeDemoPayPalPayLaterViewController</string>
<string>BraintreeDemoPayPalWebCheckoutViewController</string>
<string>BraintreeDemoCardTokenizationViewController</string>
<string>BraintreeDemoVenmoViewController</string>
<string>BraintreeDemoApplePayPassKitViewController</string>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Foundation
import UIKit
import BraintreePayPal

class BraintreeDemoPayPalWebCheckoutViewController: BraintreeDemoPaymentButtonBaseViewController {

lazy var payPalClient = BTPayPalClient(apiClient: apiClient)

override func createPaymentButton() -> UIView! {
let payPalCheckoutButton = paymentButton(title: "PayPal Checkout", action: #selector(tappedPayPalCheckout))
let payPalVaultButton = paymentButton(title: "PayPal Vault", action: #selector(tappedPayPalVault))
let payPalPayLaterButton = paymentButton(title: "PayPal with Pay Later Offered", action: #selector(tappedPayPalPayLater))

let buttons = [payPalCheckoutButton, payPalVaultButton, payPalPayLaterButton]
let stackView = UIStackView(arrangedSubviews: buttons)
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .fillEqually
stackView.translatesAutoresizingMaskIntoConstraints = false

return stackView
}

@objc func tappedPayPalCheckout(_ sender: UIButton) {
progressBlock("Tapped PayPal - Checkout using BTPayPalClient")
sender.setTitle("Processing...", for: .disabled)
sender.isEnabled = false

let request = BTPayPalCheckoutRequest(amount: "4.30")

payPalClient.tokenize(request) { nonce, error in
sender.isEnabled = true

guard let nonce else {
self.progressBlock(error?.localizedDescription)
return
}

self.completionBlock(nonce)
}
}

@objc func tappedPayPalVault(_ sender: UIButton) {
progressBlock("Tapped PayPal - Vault using BTPayPalClient")
sender.setTitle("Processing...", for: .disabled)
sender.isEnabled = false

let request = BTPayPalVaultRequest()

payPalClient.tokenize(request) { nonce, error in
sender.isEnabled = true

guard let nonce else {
self.progressBlock(error?.localizedDescription)
return
}

self.completionBlock(nonce)
}
}

@objc func tappedPayPalPayLater(_ sender: UIButton) {
progressBlock("Tapped PayPal - initiating with Pay Later offered")
sender.setTitle("Processing...", for: .disabled)
sender.isEnabled = false

let request = BTPayPalCheckoutRequest(amount: "4.30")
request.offerPayLater = true

payPalClient.tokenize(request) { nonce, error in
sender.isEnabled = true

guard let nonce else {
self.progressBlock(error?.localizedDescription)
return
}

self.completionBlock(nonce)
}
}

// TODO: move into a shared class once the base ViewControllers are converted to Swift
func paymentButton(title: String, action: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.setTitleColor(.blue, for: .normal)
button.setTitleColor(.lightGray, for: .highlighted)
button.setTitleColor(.lightGray, for: .disabled)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: action, for: .touchUpInside)
return button
}
}
Loading

0 comments on commit 77ca7d8

Please sign in to comment.