Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit f1a25cc

Browse files
authored
Add order_id to Charges and my_customer_id to Customers created in sample app. (#35)
This shows off using the `metadata` field to include application-specific information in the Stripe objects. These metadata fields can be used for looking up the associated Order/Customer in your app's backend, given the Order/Customer from Stripe.
1 parent 0a58d62 commit f1a25cc

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

web.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def log_info(message)
5656
:source => source,
5757
:description => "Example Charge",
5858
:shipping => payload[:shipping],
59+
:metadata => {
60+
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
61+
}.merge(payload[:metadata] || {}),
5962
)
6063
rescue Stripe::StripeError => e
6164
status 402
@@ -78,7 +81,13 @@ def authenticate!
7881
end
7982
else
8083
begin
81-
@customer = Stripe::Customer.create(:description => "mobile SDK example customer")
84+
@customer = Stripe::Customer.create(
85+
:description => 'mobile SDK example customer',
86+
:metadata => {
87+
# Add our application's customer id for this Customer, so it'll be easier to look up
88+
:my_customer_id => '72F8C533-FCD5-47A6-A45B-3956CA8C792D',
89+
},
90+
)
8291
rescue Stripe::InvalidRequestError
8392
end
8493
session[:customer_id] = @customer.id
@@ -94,7 +103,10 @@ def authenticate!
94103
:amount => params[:amount], # this number should be in cents
95104
:currency => "usd",
96105
:source => params[:source],
97-
:description => "Example Charge"
106+
:description => "Example Charge",
107+
:metadata => {
108+
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
109+
}.merge(params[:metadata] || {}),
98110
)
99111
rescue Stripe::StripeError => e
100112
status 402
@@ -117,6 +129,9 @@ def authenticate!
117129
:currency => params[:currency] || 'usd',
118130
:description => params[:description] || 'Example PaymentIntent charge',
119131
:return_url => params[:return_url],
132+
:metadata => {
133+
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
134+
}.merge(params[:metadata] || {}),
120135
)
121136
rescue Stripe::StripeError => e
122137
status 402
@@ -151,7 +166,10 @@ def authenticate!
151166
:currency => source.currency,
152167
:source => source.id,
153168
:customer => source.metadata["customer"],
154-
:description => "Example Charge"
169+
:description => "Example Charge",
170+
:metadata => {
171+
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
172+
}.merge(source.metadata || {}),
155173
)
156174
rescue Stripe::StripeError => e
157175
return log_info("Error creating charge: #{e.message}")

0 commit comments

Comments
 (0)