-
Notifications
You must be signed in to change notification settings - Fork 35
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
PayPal Web Demo App Refactor #223
Changes from 30 commits
b34ed3f
32c4b9b
e327686
9d956dd
bc4a9e3
d685080
8aed081
4474449
72aaa7a
5a02090
9e8019b
d96b216
994a3ac
3dd128e
c01d5c5
664f56c
e1faf50
a7b9b64
af90a4e
5f95aa4
ee4f5da
6c30414
94f8f9b
bb5596f
c2785ae
fa6cd08
0a3305e
c137eb7
8cd8cd2
5a95765
af04b83
518e8ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,21 @@ final class DemoMerchantAPI { | |
throw error | ||
} | ||
} | ||
|
||
|
||
func completeOrder(intent: Intent, orderID: String) async throws -> Order { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we refactor card we should use this singular method - the only difference between authorize and capture is the intent so we don't need 2 separate methods for that |
||
let intent = intent == .authorize ? "authorize" : "capture" | ||
guard let url = buildBaseURL( | ||
with: "/orders/\(orderID)/\(intent)", | ||
selectedMerchantIntegration: DemoSettings.merchantIntegration | ||
) else { | ||
throw URLResponseError.invalidURL | ||
} | ||
|
||
let urlRequest = buildURLRequest(method: "POST", url: url, body: EmptyBodyParams()) | ||
let data = try await data(for: urlRequest) | ||
return try parse(from: data) | ||
} | ||
|
||
func captureOrder(orderID: String, selectedMerchantIntegration: MerchantIntegration) async throws -> Order { | ||
guard let url = buildBaseURL(with: "/orders/\(orderID)/capture", selectedMerchantIntegration: selectedMerchantIntegration) else { | ||
throw URLResponseError.invalidURL | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
enum CurrentState: Equatable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we can use this across the other demo features as well vs the individual states per feature that exist today |
||
case idle | ||
case loading | ||
case success | ||
case error(message: String) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SwiftUI | ||
|
||
struct PayPalWebPaymentsView: View { | ||
|
||
@StateObject var payPalWebViewModel = PayPalWebViewModel() | ||
|
||
var body: some View { | ||
ScrollView { | ||
VStack(spacing: 16) { | ||
PayPalWebCreateOrderView(payPalWebViewModel: payPalWebViewModel) | ||
if payPalWebViewModel.order != nil { | ||
PayPalWebResultView(payPalWebViewModel: payPalWebViewModel, status: .created) | ||
NavigationLink { | ||
jaxdesmarais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PayPalWebButtonsView(payPalWebViewModel: payPalWebViewModel) | ||
.navigationTitle("Checkout with PayPal") | ||
} label: { | ||
Text("Checkout with PayPal") | ||
} | ||
.buttonStyle(RoundedBlueButtonStyle()) | ||
.padding() | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SwiftUI | ||
|
||
enum OrderStatus { | ||
case created | ||
case approved | ||
case completed | ||
} | ||
|
||
struct PayPalWebResultView: View { | ||
|
||
@ObservedObject var payPalWebViewModel: PayPalWebViewModel | ||
|
||
var status: OrderStatus | ||
|
||
var body: some View { | ||
switch payPalWebViewModel.state { | ||
case .idle, .loading: | ||
EmptyView() | ||
case .success: | ||
PayPalWebStatusView(status: status, payPalWebViewModel: payPalWebViewModel) | ||
case .error(let errorMessage): | ||
ErrorView(errorMessage: errorMessage) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import SwiftUI | ||
|
||
struct PayPalWebStatusView: View { | ||
|
||
var status: OrderStatus | ||
var payPalWebViewModel: PayPalWebViewModel | ||
|
||
var body: some View { | ||
VStack(spacing: 16) { | ||
switch status { | ||
case .created: | ||
HStack { | ||
Text("Order Created") | ||
.font(.system(size: 20)) | ||
Spacer() | ||
} | ||
if let order = payPalWebViewModel.order { | ||
LeadingText("Order ID", weight: .bold) | ||
LeadingText("\(order.id)") | ||
LeadingText("Status", weight: .bold) | ||
LeadingText("\(order.status)") | ||
} | ||
case .approved: | ||
HStack { | ||
Text("Order Approved") | ||
.font(.system(size: 20)) | ||
Spacer() | ||
} | ||
if let order = payPalWebViewModel.order { | ||
LeadingText("Intent", weight: .bold) | ||
LeadingText("\(payPalWebViewModel.intent)") | ||
LeadingText("Order ID", weight: .bold) | ||
LeadingText("\(order.id)") | ||
LeadingText("Payer ID", weight: .bold) | ||
LeadingText("\(payPalWebViewModel.checkoutResult?.payerID ?? "")") | ||
} | ||
case .completed: | ||
if let order = payPalWebViewModel.order { | ||
HStack { | ||
Text("Order \(payPalWebViewModel.intent.rawValue.capitalized)d") | ||
.font(.system(size: 20)) | ||
Spacer() | ||
} | ||
LeadingText("Order ID", weight: .bold) | ||
LeadingText("\(order.id)") | ||
LeadingText("Status", weight: .bold) | ||
LeadingText("\(order.status)") | ||
|
||
if let emailAddress = payPalWebViewModel.order?.paymentSource?.paypal?.emailAddress { | ||
LeadingText("Email", weight: .bold) | ||
LeadingText("\(emailAddress)") | ||
} | ||
|
||
if let vaultID = payPalWebViewModel.order?.paymentSource?.paypal?.attributes?.vault.id { | ||
LeadingText("Vault ID / Payment Token", weight: .bold) | ||
LeadingText("\(vaultID)") | ||
} | ||
|
||
if let customerID = payPalWebViewModel.order?.paymentSource?.paypal?.attributes?.vault.customer.id { | ||
LeadingText("Customer ID", weight: .bold) | ||
LeadingText("\(customerID)") | ||
} | ||
} | ||
} | ||
} | ||
.frame(maxWidth: .infinity) | ||
.padding() | ||
.background( | ||
RoundedRectangle(cornerRadius: 10) | ||
.stroke(.gray, lineWidth: 2) | ||
.padding(5) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import SwiftUI | ||
|
||
struct PayPalWebTransactionView: View { | ||
|
||
@ObservedObject var payPalWebViewModel: PayPalWebViewModel | ||
|
||
var body: some View { | ||
ScrollView { | ||
ScrollViewReader { scrollView in | ||
VStack { | ||
PayPalWebStatusView(status: .approved, payPalWebViewModel: payPalWebViewModel) | ||
ZStack { | ||
Button("\(payPalWebViewModel.intent.rawValue.capitalized) Order") { | ||
Task { | ||
do { | ||
try await payPalWebViewModel.completeTransaction() | ||
} catch { | ||
print("Error capturing order: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
.buttonStyle(RoundedBlueButtonStyle()) | ||
.padding() | ||
|
||
if payPalWebViewModel.state == .loading { | ||
CircularProgressView() | ||
} | ||
} | ||
|
||
if payPalWebViewModel.state == .success && payPalWebViewModel.order?.status == "COMPLETED" { | ||
PayPalWebResultView(payPalWebViewModel: payPalWebViewModel, status: .completed) | ||
jaxdesmarais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.id("bottomView") | ||
} | ||
} | ||
.onChange(of: payPalWebViewModel.order) { _ in | ||
withAnimation { | ||
scrollView.scrollTo("bottomView") | ||
} | ||
} | ||
Spacer() | ||
} | ||
} | ||
} | ||
} |
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 isn't related to this PR but at one point I was poking around at this file and noticed we had this one as a variable - should be a constant to match all other structs in this file