Skip to content

Commit

Permalink
Add client-side metadata charge_request_id to requests to `example-…
Browse files Browse the repository at this point in the history
…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-mobile-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
  • Loading branch information
danj-stripe authored Aug 8, 2018
1 parent 68ff597 commit a8922af
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
14 changes: 12 additions & 2 deletions Example/Custom Integration (ObjC)/BrowseExamplesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Example/Custom Integration (ObjC)/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Example/Custom Integration (ObjC)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Example/Standard Integration (Swift)/MyAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Example/Standard Integration (Swift)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Stripe/PublicHeaders/STPEphemeralKeyProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a8922af

Please sign in to comment.