-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Shopper Insights Demo feature #1158
Merged
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
aacf5d9
Create new Source & UnitTest directories & targets
scannillo 5e9e19e
Update UnitTests scheme
scannillo c753d8a
Package manager updates + test info.plist
scannillo 9b6f87f
Fixup - address duplicate info.plist build error
scannillo 42eeff3
Add CHANGELOG entry
scannillo 8e713c7
Fixup - remove redundant top-level directory
scannillo fbf68cb
Fixup - remove reference to non-existing .h file
scannillo 36a110b
add BraintreeCore target to BraintreeShopperInsights Frameworks & Lib…
scannillo 8f75e1d
Fixup - fix typo in podspec sources path
scannillo 54a8f58
Only support iOS platform in ShopperInsights target
scannillo 599e714
Update CHANGELOG.md
scannillo a39153f
Fixup - remove reference to top-level, accidental, ShopperInsights dir
scannillo 0df387a
Restore old PreferredPaymentMethods demo ViewController
scannillo f619a69
Remove nested PreferredPayments dir
scannillo df9c4ee
Rename & update file for latest API & iOS V6 changes
scannillo a44a61d
Add Shopper Insights feature into Settings
scannillo 8d73d4c
Cleanup - rename old variables with PreferredPayments reference
scannillo 69bd6da
Adjust syntax/indendation for constraints array
scannillo 95845c0
Merge branch 'payment-insights-feature' into shopper-insights-demo
scannillo 4f47a26
Delete Braintree.xcworkspace/xcshareddata/swiftpm/Package.resolved
scannillo 92676b6
PR Feedback - cleanup demo to leverage PaymentButtonBaseViewControlle…
scannillo 45eb5f8
Fixup - move to lazy button initialization to be able to disable/enab…
scannillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
Demo/Application/Features/ShopperInsightsViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import UIKit | ||
import BraintreeCore | ||
import BraintreePayPal | ||
import BraintreeVenmo | ||
import BraintreeShopperInsights | ||
|
||
class ShopperInsightsViewController: PaymentButtonBaseViewController { | ||
|
||
private let shopperInsightsClient: BTShopperInsightsClient | ||
private let paypalClient: BTPayPalClient | ||
private let venmoClient: BTVenmoClient | ||
private let payPalCheckoutButton = UIButton(type: .system) | ||
private let payPalVaultButton = UIButton(type: .system) | ||
private let venmoButton = UIButton(type: .system) | ||
|
||
override init(authorization: String) { | ||
let apiClient = BTAPIClient(authorization: authorization)! | ||
|
||
shopperInsightsClient = BTShopperInsightsClient(apiClient: apiClient) | ||
paypalClient = BTPayPalClient(apiClient: apiClient) | ||
venmoClient = BTVenmoClient(apiClient: apiClient) | ||
|
||
super.init(authorization: authorization) | ||
|
||
title = "Shopper Insights" | ||
|
||
let shopperInsightsButton = UIButton(type: .system) | ||
shopperInsightsButton.setTitle("Fetch recommended payments", for: .normal) | ||
shopperInsightsButton.translatesAutoresizingMaskIntoConstraints = false | ||
shopperInsightsButton.addTarget(self, action: #selector(shopperInsightsButtonTapped(_:)), for: .touchUpInside) | ||
view.addSubview(shopperInsightsButton) | ||
|
||
payPalCheckoutButton.setTitle("PayPal Checkout", for: .normal) | ||
payPalCheckoutButton.translatesAutoresizingMaskIntoConstraints = false | ||
payPalCheckoutButton.addTarget(self, action: #selector(payPalCheckoutButtonTapped(_:)), for: .touchUpInside) | ||
payPalCheckoutButton.isEnabled = false | ||
view.addSubview(payPalCheckoutButton) | ||
|
||
payPalVaultButton.setTitle("PayPal Vault", for: .normal) | ||
payPalVaultButton.translatesAutoresizingMaskIntoConstraints = false | ||
payPalVaultButton.addTarget(self, action: #selector(payPalVaultButtonTapped(_:)), for: .touchUpInside) | ||
payPalVaultButton.isEnabled = false | ||
view.addSubview(payPalVaultButton) | ||
|
||
venmoButton.setTitle("Venmo", for: .normal) | ||
venmoButton.translatesAutoresizingMaskIntoConstraints = false | ||
venmoButton.addTarget(self, action: #selector(venmoButtonTapped(_:)), for: .touchUpInside) | ||
venmoButton.isEnabled = false | ||
view.addSubview(venmoButton) | ||
|
||
view.addConstraints([ | ||
shopperInsightsButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
shopperInsightsButton.centerYAnchor.constraint(equalTo: view.centerYAnchor), | ||
payPalVaultButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
payPalVaultButton.bottomAnchor.constraint(equalTo: payPalCheckoutButton.bottomAnchor, constant: -40), | ||
payPalCheckoutButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
payPalCheckoutButton.bottomAnchor.constraint(equalTo: venmoButton.bottomAnchor, constant: -40), | ||
venmoButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
venmoButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40) | ||
]) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
scannillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@objc func shopperInsightsButtonTapped(_ button: UIButton) { | ||
self.progressBlock("Fetching shopper insights...") | ||
|
||
let request = BTShopperInsightsRequest( | ||
email: "[email protected]", | ||
phone: Phone( | ||
countryCode: "1", | ||
nationalNumber: "1234567" | ||
) | ||
) | ||
Task { | ||
do { | ||
let result = try await shopperInsightsClient.getRecommendedPaymentMethods(request: request) | ||
self.progressBlock("PayPal Recommended: \(result.isPayPalRecommended)\nVenmo Recommended: \(result.isVenmoRecommended)") | ||
self.payPalCheckoutButton.isEnabled = result.isPayPalRecommended | ||
self.payPalVaultButton.isEnabled = result.isPayPalRecommended | ||
self.venmoButton.isEnabled = result.isVenmoRecommended | ||
} catch { | ||
self.progressBlock("Error: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
|
||
@objc func payPalCheckoutButtonTapped(_ button: UIButton) { | ||
self.progressBlock("Tapped PayPal Checkout") | ||
|
||
button.setTitle("Processing...", for: .disabled) | ||
button.isEnabled = false | ||
|
||
let paypalRequest = BTPayPalCheckoutRequest(amount: "4.30") | ||
paypalClient.tokenize(paypalRequest) { (nonce, error) in | ||
button.isEnabled = true | ||
|
||
if let e = error { | ||
self.progressBlock(e.localizedDescription) | ||
} else if let n = nonce { | ||
self.completionBlock(n) | ||
scannillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
self.progressBlock("Canceled") | ||
} | ||
} | ||
} | ||
|
||
@objc func payPalVaultButtonTapped(_ button: UIButton) { | ||
self.progressBlock("Tapped PayPal Vault") | ||
|
||
button.setTitle("Processing...", for: .disabled) | ||
button.isEnabled = false | ||
|
||
let paypalRequest = BTPayPalVaultRequest() | ||
paypalClient.tokenize(paypalRequest) { (nonce, error) in | ||
button.isEnabled = true | ||
|
||
if let e = error { | ||
self.progressBlock(e.localizedDescription) | ||
} else if let n = nonce { | ||
self.completionBlock(n) | ||
scannillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
self.progressBlock("Canceled") | ||
} | ||
} | ||
} | ||
|
||
@objc func venmoButtonTapped(_ button: UIButton) { | ||
self.progressBlock("Tapped Venmo") | ||
|
||
button.setTitle("Processing...", for: .disabled) | ||
button.isEnabled = false | ||
|
||
let venmoRequest = BTVenmoRequest(paymentMethodUsage: .multiUse) | ||
venmoClient.tokenize(venmoRequest) { (nonce, error) in | ||
button.isEnabled = true | ||
|
||
if let e = error { | ||
self.progressBlock(e.localizedDescription) | ||
} else if let n = nonce { | ||
self.completionBlock(n) | ||
scannillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
self.progressBlock("Canceled") | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we want to 🏈 on this based on the PR description but subclassing
PaymentButtonBaseViewController
should allow us to get a lot of this for "free". Specially thecreateButton
and ability to remove the override of the auth init.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is in the Demo app, so not as merchant impacting, but I personally think it's acceptable to take a little time and do this work now. If folks push back we can lean on the company's leadership principles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great callout! Addressed in 92676b6