From a8922af2f9da4eb09db0f50268bd30a90b6eda04 Mon Sep 17 00:00:00 2001
From: Dan Jackson <danj@stripe.com>
Date: Wed, 8 Aug 2018 16:56:07 -0700
Subject: [PATCH] Add client-side metadata `charge_request_id` to requests to
 `example-ios-backend` (#1008)

* Add client-side metadata `charge_request_id` to requests to `example-ios-backend`

The value is meaningless, it's just a randomly generated UUID
`B3E611D1-5FA1-4410-9CEC-00958A5126CB`.

However, along with the changes from stripe/example-ios-backend#35, this shows off some
of what `metadata` can be used for.

I am *not* adding metadata to each location where the Stripe API is directly used, only
the places that go through stripe/example-ios-backend. Many of the places that directly
deal with the stripe-ios SDK could *also* add metadata. That's being omitted for
simplicity.

See also IOS-356

* Update links to example-ios-backend to use v13.2.0 tag
---
 .../BrowseExamplesViewController.m                 | 14 ++++++++++++--
 Example/Custom Integration (ObjC)/Constants.m      |  2 +-
 Example/Custom Integration (ObjC)/README.md        |  2 +-
 .../CheckoutViewController.swift                   |  2 +-
 .../Standard Integration (Swift)/MyAPIClient.swift |  8 ++++++--
 Example/Standard Integration (Swift)/README.md     |  2 +-
 Stripe/PublicHeaders/STPEphemeralKeyProvider.h     |  2 +-
 7 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/Example/Custom Integration (ObjC)/BrowseExamplesViewController.m b/Example/Custom Integration (ObjC)/BrowseExamplesViewController.m
index bc4d5ca14ec..9765fcf529a 100644
--- a/Example/Custom Integration (ObjC)/BrowseExamplesViewController.m	
+++ b/Example/Custom Integration (ObjC)/BrowseExamplesViewController.m	
@@ -118,7 +118,12 @@ - (void)createBackendChargeWithSource:(NSString *)sourceID completion:(STPSource
     NSURL *url = [NSURL URLWithString:urlString];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
     request.HTTPMethod = @"POST";
-    NSString *postBody = [NSString stringWithFormat:@"source=%@&amount=%@", sourceID, @1099];
+    NSString *postBody = [NSString stringWithFormat:
+                          @"source=%@&amount=%@&metadata[charge_request_id]=%@",
+                          sourceID,
+                          @1099,
+                          // example-ios-backend allows passing metadata through to Stripe
+                          @"B3E611D1-5FA1-4410-9CEC-00958A5126CB"];
     NSData *data = [postBody dataUsingEncoding:NSUTF8StringEncoding];
 
     NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
@@ -169,7 +174,12 @@ - (void)createBackendPaymentIntentWithAmount:(NSNumber *)amount completion:(STPP
     NSURL *url = [NSURL URLWithString:urlString];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
     request.HTTPMethod = @"POST";
-    NSString *postBody = [NSString stringWithFormat:@"amount=%@", amount];
+    NSString *postBody = [NSString stringWithFormat:
+                          @"amount=%@&metadata[charge_request_id]=%@",
+                          amount,
+                          // example-ios-backend allows passing metadata through to Stripe
+                          @"B3E611D1-5FA1-4410-9CEC-00958A5126CB"
+                          ];
     NSData *data = [postBody dataUsingEncoding:NSUTF8StringEncoding];
 
     NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
diff --git a/Example/Custom Integration (ObjC)/Constants.m b/Example/Custom Integration (ObjC)/Constants.m
index bad62f61e09..6e7f694d1dc 100644
--- a/Example/Custom Integration (ObjC)/Constants.m	
+++ b/Example/Custom Integration (ObjC)/Constants.m	
@@ -11,7 +11,7 @@
 // This can be found at https://dashboard.stripe.com/account/apikeys
 NSString *const StripePublishableKey = nil; // TODO: replace nil with your own value
 
-// To set this up, check out https://github.com/stripe/example-ios-backend/tree/v13.1.0
+// To set this up, check out https://github.com/stripe/example-ios-backend/tree/v13.2.0
 // This should be in the format https://my-shiny-backend.herokuapp.com
 NSString *const BackendBaseURL = nil; // TODO: replace nil with your own value
 
diff --git a/Example/Custom Integration (ObjC)/README.md b/Example/Custom Integration (ObjC)/README.md
index 9bfef6234c3..67d52ee1f0b 100644
--- a/Example/Custom Integration (ObjC)/README.md	
+++ b/Example/Custom Integration (ObjC)/README.md	
@@ -10,7 +10,7 @@ For more details on using Sources, see https://stripe.com/docs/mobile/ios/source
 2. Execute `./setup.sh` from the root of the repository to build the necessary dependencies
 3. Open `./Stripe.xcworkspace` (not `./Stripe.xcodeproj`) with Xcode
 4. Fill in the `stripePublishableKey` constant in `./Example/Custom Integration (ObjC)/Constants.m` with your test "Publishable key" from Stripe. This key should start with `pk_test`.
-5. Head to [example-ios-backend](https://github.com/stripe/example-ios-backend/tree/v13.1.0) and click "Deploy to Heroku". Provide your Stripe test "Secret key" as the `STRIPE_TEST_SECRET_KEY` environment variable. This key should start with `sk_test`.
+5. Head to [example-ios-backend](https://github.com/stripe/example-ios-backend/tree/v13.2.0) and click "Deploy to Heroku". Provide your Stripe test "Secret key" as the `STRIPE_TEST_SECRET_KEY` environment variable. This key should start with `sk_test`.
 6. Fill in the `backendBaseURL` constant in `Constants.m` with the app URL Heroku provides (e.g. "https://my-example-app.herokuapp.com")
 
 After this is done, you can make test payments through the app and see them in your Stripe dashboard.
diff --git a/Example/Standard Integration (Swift)/CheckoutViewController.swift b/Example/Standard Integration (Swift)/CheckoutViewController.swift
index c7f8c148b71..c6a7f6d5f7a 100644
--- a/Example/Standard Integration (Swift)/CheckoutViewController.swift	
+++ b/Example/Standard Integration (Swift)/CheckoutViewController.swift	
@@ -16,7 +16,7 @@ class CheckoutViewController: UIViewController, STPPaymentContextDelegate {
     let stripePublishableKey = ""
 
     // 2) Next, optionally, to have this demo save your user's payment details, head to
-    // https://github.com/stripe/example-ios-backend/tree/v13.1.0, click "Deploy to Heroku", and follow
+    // https://github.com/stripe/example-ios-backend/tree/v13.2.0, click "Deploy to Heroku", and follow
     // the instructions (don't worry, it's free). Replace nil on the line below with your
     // Heroku URL (it looks like https://blazing-sunrise-1234.herokuapp.com ).
     let backendBaseURL: String? = nil
diff --git a/Example/Standard Integration (Swift)/MyAPIClient.swift b/Example/Standard Integration (Swift)/MyAPIClient.swift
index b84d25c4418..9518c7151a9 100644
--- a/Example/Standard Integration (Swift)/MyAPIClient.swift	
+++ b/Example/Standard Integration (Swift)/MyAPIClient.swift	
@@ -30,8 +30,12 @@ class MyAPIClient: NSObject, STPEphemeralKeyProvider {
         let url = self.baseURL.appendingPathComponent("charge")
         var params: [String: Any] = [
             "source": result.source.stripeID,
-            "amount": amount
-        ]
+            "amount": amount,
+            "metadata": [
+                // example-ios-backend allows passing metadata through to Stripe
+                "charge_request_id": "B3E611D1-5FA1-4410-9CEC-00958A5126CB",
+            ],
+            ]
         params["shipping"] = STPAddress.shippingInfoForCharge(with: shippingAddress, shippingMethod: shippingMethod)
         Alamofire.request(url, method: .post, parameters: params)
             .validate(statusCode: 200..<300)
diff --git a/Example/Standard Integration (Swift)/README.md b/Example/Standard Integration (Swift)/README.md
index 6b34335d4af..00ac9f8dcd0 100644
--- a/Example/Standard Integration (Swift)/README.md	
+++ b/Example/Standard Integration (Swift)/README.md	
@@ -8,7 +8,7 @@ For a detailed guide, see https://stripe.com/docs/mobile/ios/standard
 2. Execute `./setup.sh` from the root of the repository to build the necessary dependencies
 3. Open `./Stripe.xcworkspace` (not `./Stripe.xcodeproj`) with Xcode
 4. Fill in the `stripePublishableKey` constant in `./Example/Standard Integration (Swift)/CheckoutViewController.swift` with your test "Publishable key" from Stripe. This key should start with `pk_test`.
-5. Head to [example-ios-backend](https://github.com/stripe/example-ios-backend/tree/v13.1.0) and click "Deploy to Heroku". Provide your Stripe test "Secret key" as the `STRIPE_TEST_SECRET_KEY` environment variable. This key should start with `pk_test`.
+5. Head to [example-ios-backend](https://github.com/stripe/example-ios-backend/tree/v13.2.0) and click "Deploy to Heroku". Provide your Stripe test "Secret key" as the `STRIPE_TEST_SECRET_KEY` environment variable. This key should start with `pk_test`.
 6. Fill in the `backendBaseURL` constant in `./Example/Standard Integration (Swift)/CheckoutViewController.swift` with the app URL Heroku provides (e.g. "https://my-example-app.herokuapp.com")
 
 After this is done, you can make test payments through the app and see them in your Stripe dashboard.
diff --git a/Stripe/PublicHeaders/STPEphemeralKeyProvider.h b/Stripe/PublicHeaders/STPEphemeralKeyProvider.h
index 966ae180c7a..a545bc6be41 100644
--- a/Stripe/PublicHeaders/STPEphemeralKeyProvider.h
+++ b/Stripe/PublicHeaders/STPEphemeralKeyProvider.h
@@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
  On your backend, you should create a new ephemeral key for the Stripe customer
  associated with your user, and return the raw JSON response from the Stripe API.
  For an example Ruby implementation of this API, refer to our example backend:
- https://github.com/stripe/example-ios-backend/blob/v13.1.0/web.rb
+ https://github.com/stripe/example-ios-backend/blob/v13.2.0/web.rb
 
  Back in your iOS app, once you have a response from this API, call the provided
  completion block with the JSON response, or an error if one occurred.