Skip to content

Commit

Permalink
Send selected products to backend (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidme-stripe authored Sep 10, 2019
1 parent 4de5ea9 commit 5c386e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Example/Standard Integration/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ extension CheckoutViewController: STPPaymentContextDelegate {
func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
// Create the PaymentIntent on the backend
// A real app should do this at the beginning of the checkout flow, instead of re-creating a PaymentIntent for every payment attempt.
MyAPIClient.sharedClient.createPaymentIntent() { result in
MyAPIClient.sharedClient.createPaymentIntent(products: self.products, shippingMethod: paymentContext.selectedShippingMethod) { result in
switch result {
case .success(let clientSecret):
// Confirm the PaymentIntent
Expand Down Expand Up @@ -381,6 +381,7 @@ extension CheckoutViewController: STPPaymentContextDelegate {
}
else {
fedEx.amount = 20.99
fedEx.identifier = "fedex_world"
completion(.valid, nil, [upsWorldwide, fedEx], fedEx)
}
}
Expand Down
11 changes: 8 additions & 3 deletions Example/Standard Integration/MyAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ class MyAPIClient: NSObject, STPCustomerEphemeralKeyProvider {
}
}

func createPaymentIntent(completion: @escaping ((Result<String, Error>) -> Void)) {
func createPaymentIntent(products: [Product], shippingMethod: PKShippingMethod?, completion: @escaping ((Result<String, Error>) -> Void)) {
let url = self.baseURL.appendingPathComponent("create_payment_intent")
let params: [String: Any] = [
var params: [String: Any] = [
"metadata": [
// example-ios-backend allows passing metadata through to Stripe
"payment_request_id": "B3E611D1-5FA1-4410-9CEC-00958A5126CB",
],
]

params["products"] = products.map({ (p) -> String in
return p.emoji
})
if let shippingMethod = shippingMethod {
params["shipping"] = shippingMethod.identifier
}
let jsonData = try? JSONSerialization.data(withJSONObject: params)
var request = URLRequest(url: url)
request.httpMethod = "POST"
Expand Down

0 comments on commit 5c386e6

Please sign in to comment.