Extract plan from Stripe - ruby-on-rails

How do I retrieve the Plan ID in Rails from Stripe's invoice.payment_succeeded webhook event?
I have tried the code below but keep on getting plan is nil error.
plan_id = event.data.object.lines.data[1].plan.id
Here is the web hook event.
{
"id"=>"evt_111111111",
"created"=>1207343135,
"livemode"=>true,
"type"=>"invoice.payment_succeeded",
"data"=>{
"object"=>{
"date"=>2437418246,
"id"=>"in_......",
"period_start"=>1406044997,
"period_end"=>1437238559,
"lines"=>{
"object"=>"list",
"total_count"=>1,
"has_more"=>false,
"url"=>"/v1/invoices/in_...../lines",
"data"=>[
{
"id"=>"sub_.....",
"object"=>"line_item",
"type"=>"subscription",
"livemode"=>true,
"amount"=>29900,
"currency"=>"cad",
"proration"=>false,
"period"=>{
"start"=>2437418246,
"end"=>2437418246
},
"quantity"=>1,
"plan"=>{
"interval"=>"month",
"name"=>"Business",
"created"=>2437446,
"amount"=>19900,
"currency"=>"cad",
"id"=>"P3",
"object"=>"plan",
"livemode"=>true,
"interval_count"=>1,
"trial_period_days"=>7,
"metadata"=>{},
"statement_description"=>nil
},
"description"=>nil,
"metadata"=>{}
}
],
"count"=>1
},
"subtotal"=>19900,
"total"=>19900,
"customer"=>"cus_.....",
"object"=>"invoice",
"attempted"=>true,
"closed"=>true,
"forgiven"=>false,
"paid"=>true,
"livemode"=>true,
"attempt_count"=>1,
"amount_due"=>19900,
"currency"=>"cad",
"starting_balance"=>0,
"ending_balance"=>0,
"next_payment_attempt"=>nil,
"webhooks_delivered_at"=>14022ert61,
"charge"=>"ch_......",
"discount"=>nil,
"application_fee"=>nil,
"subscription"=>"sub_......",
"metadata"=>{},
"statement_description"=>nil,
"description"=>nil
}
},
"object"=>"event",
"pending_webhooks"=>1,
"request"=>nil,
"webhook"=>{
"id"=>"evt_....."
}
}

You appear to only have a single item in the data list.
Note that Arrays in Ruby start at index 0.
I believe you will want to change data[1] to data[0].
Note, this can still break if there is a chance that the data list can be empty.

Related

how do I print a Rails POST request output?

I have this request:
post '/api/v1/fruit', params: valid_fruits, headers: lots_of_security
how can I see what's going on behind the scenes and see the many fruits contained in the raw output that will be sent after executing it?
[1] pry(#<Api::V1::InquiriesController>)> request
returns this very verbose and complete answer:
=> #<ActionDispatch::Request:0x0000aaaae4ba5308
#env=
{"rack.version"=>[1, 3],
"rack.input"=>#<StringIO:0x0000aaaae4bdffa8>,
"rack.errors"=>#<StringIO:0x0000aaaae4be0070>,
"rack.multithread"=>true,
"rack.multiprocess"=>true,
"rack.run_once"=>false,
"REQUEST_METHOD"=>"POST",
"SERVER_NAME"=>"www.example.com",
"SERVER_PORT"=>"80",
"QUERY_STRING"=>"",
"PATH_INFO"=>"/api/v1/inquiries",
"rack.url_scheme"=>"http",
"HTTPS"=>"off",
"SCRIPT_NAME"=>"",
"CONTENT_LENGTH"=>"141",
"rack.test"=>true,
"REMOTE_ADDR"=>"127.0.0.1",
"REQUEST_URI"=>"/api/v1/inquiries",
"HTTP_HOST"=>"www.example.com",
"CONTENT_TYPE"=>"application/x-www-form-urlencoded",
"HTTP_ACCEPT"=>"application/vnd",
"HTTP_AUTHORIZATION"=>"Bc29dEtvWGiYtmaZGTqL484r",
"HTTP_COOKIE"=>"",
"action_dispatch.parameter_filter"=>[:password],
"action_dispatch.redirect_filter"=>[],
"action_dispatch.secret_key_base"=>
"redacted",
"action_dispatch.show_exceptions"=>false,
"action_dispatch.show_detailed_exceptions"=>true,
"action_dispatch.logger"=>
#<ActiveSupport::Logger:0x0000aaaae205d4e8
#default_formatter=
#<Logger::Formatter:0x0000aaaae205d678 #datetime_format=nil>,
<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>

Need ISO 639-1 (2 letter) Languages code and List and translation in all languages

I need list of 2 letter language code and name , which is again translated in all languages .
Best I can give is a dictionary where keys are the codes and values are their associated language name in English:
{'ab': 'Abkhaz', 'aa': 'Afar', 'af': 'Afrikaans', 'ak': 'Akan', 'sq': 'Albanian', 'am': 'Amharic', 'ar': 'Arabic', 'an': 'Aragonese', 'hy': 'Armenian', 'as': 'Assamese', 'av': 'Avaric', 'ae': 'Avestan', 'ay': 'Aymara', 'az': 'Azerbaijani', 'bm': 'Bambara', 'ba': 'Bashkir', 'eu': 'Basque', 'be': 'Belarusian', 'bn': 'Bengali', 'bh': 'Bihari', 'bi': 'Bislama', 'bs': 'Bosnian', 'br': 'Breton', 'bg': 'Bulgarian', 'my': 'Burmese', 'ca': 'Catalan; Valencian', 'ch': 'Chamorro', 'ce': 'Chechen', 'ny': 'Chichewa; Chewa; Nyanja', 'zh': 'Chinese', 'cv': 'Chuvash', 'kw': 'Cornish', 'co': 'Corsican', 'cr': 'Cree', 'hr': 'Croatian', 'cs': 'Czech', 'da': 'Danish', 'dv': 'Divehi; Maldivian;', 'nl': 'Dutch', 'dz': 'Dzongkha', 'en': 'English', 'eo': 'Esperanto', 'et': 'Estonian', 'ee': 'Ewe', 'fo': 'Faroese', 'fj': 'Fijian', 'fi': 'Finnish', 'fr': 'French', 'ff': 'Fula', 'gl': 'Galician', 'ka': 'Georgian', 'de': 'German', 'el': 'Greek, Modern', 'gn': 'Guaraní', 'gu': 'Gujarati', 'ht': 'Haitian', 'ha': 'Hausa', 'he': 'Hebrew (modern)', 'hz': 'Herero', 'hi': 'Hindi', 'ho': 'Hiri Motu', 'hu': 'Hungarian', 'ia': 'Interlingua', 'id': 'Indonesian', 'ie': 'Interlingue', 'ga': 'Irish', 'ig': 'Igbo', 'ik': 'Inupiaq', 'io': 'Ido', 'is': 'Icelandic', 'it': 'Italian', 'iu': 'Inuktitut', 'ja': 'Japanese', 'jv': 'Javanese', 'kl': 'Kalaallisut', 'kn': 'Kannada', 'kr': 'Kanuri', 'ks': 'Kashmiri', 'kk': 'Kazakh', 'km': 'Khmer', 'ki': 'Kikuyu, Gikuyu', 'rw': 'Kinyarwanda', 'ky': 'Kirghiz, Kyrgyz', 'kv': 'Komi', 'kg': 'Kongo', 'ko': 'Korean', 'ku': 'Kurdish', 'kj': 'Kwanyama, Kuanyama', 'la': 'Latin', 'lb': 'Luxembourgish', 'lg': 'Luganda', 'li': 'Limburgish', 'ln': 'Lingala', 'lo': 'Lao', 'lt': 'Lithuanian', 'lu': 'Luba-Katanga', 'lv': 'Latvian', 'gv': 'Manx', 'mk': 'Macedonian', 'mg': 'Malagasy', 'ms': 'Malay', 'ml': 'Malayalam', 'mt': 'Maltese', 'mi': 'Māori', 'mr': 'Marathi (Marāṭhī)', 'mh': 'Marshallese', 'mn': 'Mongolian', 'na': 'Nauru', 'nv': 'Navajo, Navaho', 'nb': 'Norwegian Bokmål', 'nd': 'North Ndebele', 'ne': 'Nepali', 'ng': 'Ndonga', 'nn': 'Norwegian Nynorsk', 'no': 'Norwegian', 'ii': 'Nuosu', 'nr': 'South Ndebele', 'oc': 'Occitan', 'oj': 'Ojibwe, Ojibwa', 'cu': 'Old Church Slavonic', 'om': 'Oromo', 'or': 'Oriya', 'os': 'Ossetian, Ossetic', 'pa': 'Panjabi, Punjabi', 'pi': 'Pāli', 'fa': 'Persian', 'pl': 'Polish', 'ps': 'Pashto, Pushto', 'pt': 'Portuguese', 'qu': 'Quechua', 'rm': 'Romansh', 'rn': 'Kirundi', 'ro': 'Romanian, Moldavan', 'ru': 'Russian', 'sa': 'Sanskrit (Saṁskṛta)', 'sc': 'Sardinian', 'sd': 'Sindhi', 'se': 'Northern Sami', 'sm': 'Samoan', 'sg': 'Sango', 'sr': 'Serbian', 'gd': 'Scottish Gaelic', 'sn': 'Shona', 'si': 'Sinhala, Sinhalese', 'sk': 'Slovak', 'sl': 'Slovene', 'so': 'Somali', 'st': 'Southern Sotho', 'es': 'Spanish; Castilian', 'su': 'Sundanese', 'sw': 'Swahili', 'ss': 'Swati', 'sv': 'Swedish', 'ta': 'Tamil', 'te': 'Telugu', 'tg': 'Tajik', 'th': 'Thai', 'ti': 'Tigrinya', 'bo': 'Tibetan', 'tk': 'Turkmen', 'tl': 'Tagalog', 'tn': 'Tswana', 'to': 'Tonga', 'tr': 'Turkish', 'ts': 'Tsonga', 'tt': 'Tatar', 'tw': 'Twi', 'ty': 'Tahitian', 'ug': 'Uighur, Uyghur', 'uk': 'Ukrainian', 'ur': 'Urdu', 'uz': 'Uzbek', 've': 'Venda', 'vi': 'Vietnamese', 'vo': 'Volapük', 'wa': 'Walloon', 'cy': 'Welsh', 'wo': 'Wolof', 'fy': 'Western Frisian', 'xh': 'Xhosa', 'yi': 'Yiddish', 'yo': 'Yoruba', 'za': 'Zhuang, Chuang', 'zu': 'Zulu'}

Select few key using map

I have an array which contains man value I am trying to use map method using another array and I want to select few values from first array:
My first array is:
data = [
"Agriculture, forestry and fishing",
"Crops",
"Livestock",
"Forestry and logging",
"Fishing and aquaculture",
"Mining and quarrying",
]
And my second array is:
b = [
{
id: 1,
Sector: "Agriculture, forestry and fishing",
2011-12: 25.7,
2012-13: 27,
2013-14: 22.8,
2014-15: 22,
2015-16: 21.1,
2016-17: 20.6,
created_at: "2018-08-23T06:48:27.000Z",
updated_at: "2018-08-23T06:48:27.000Z"
},
{
id: 2,
Sector: "Crops",
2011-12: 17.6,
2012-13: 18.9,
2013-14: 14.2,
2014-15: 13.1,
2015-16: 12.3,
2016-17: 12.1,
created_at: "2018-08-23T06:48:27.000Z",
updated_at: "2018-08-23T06:48:27.000Z"
}
....
]
My full data you can see it here: My second data
I am using this code:
#hash_data = data.map{ |vegetable|
dataset = vegetable.to_s.gsub("_"," ")
{
type: views,
legendText: dataset,
showInLegend: true,
dataPoints: b.map { |value|
{ y: value[_year], label: value[:Sector] }
}
}
}
How can I just select values from b array and show only those values which are in my data array.
Using Regexp.union passing your data attributes returns a Regexp
/Agriculture,\ forestry\ and\ fishing|Crops|Livestock|Forestry\ and\ logging|Fishing\ and\ aquaculture|Mining\ and\ quarrying/
that you can use to check every Section key within the elements on b:
b = [...]
data = [
"Agriculture, forestry and fishing",
"Crops",
"Livestock",
"Forestry and logging",
"Fishing and aquaculture",
"Mining and quarrying",
]
regex = Regexp.union(data)
result = b.select { |hash| hash[:Sector] =~ regex }
p result
# [{:id=>1, :Sector=>"Agriculture, forestry and fishing", :"2011-12"=>25.7, :"2012-13"=>27, :"2013-14"=>22.8, :"2014-15"=>22, :"2015-16"=>21.1, :"2016-17"=>20.6, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>2, :Sector=>"Crops", :"2011-12"=>17.6, :"2012-13"=>18.9, :"2013-14"=>14.2, :"2014-15"=>13.1, :"2015-16"=>12.3, :"2016-17"=>12.1, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, ...]
p result.map { |hash| hash[:Sector] }.sort
# ["Agriculture, forestry and fishing", "Crops", "Fishing and aquaculture", "Forestry and logging", "Livestock", "Mining and quarrying"]
Notice this is working with each value on b as:
{
id: 1,
Sector: "Agriculture, forestry and fishing",
"2011-12": 25.7,
"2012-13": 27,
"2013-14": 22.8,
"2014-15": 22,
"2015-16": 21.1,
"2016-17": 20.6,
created_at: "2018-08-23T06:48:27.000Z",
updated_at: "2018-08-23T06:48:27.000Z"
}
you can try without Regexp:
result = b.select{|e| data.include?(e[:Sector])}
p result
#=> [{:id=>1, :Sector=>"Agriculture, forestry and fishing", :"2011-12"=>25.7, :"2012-13"=>27, :"2013-14"=>22.8, :"2014-15"=>22, :"2015-16"=>21.1, :"2016-17"=>20.6, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>2, :Sector=>"Crops", :"2011-12"=>17.6, :"2012-13"=>18.9, :"2013-14"=>14.2, :"2014-15"=>13.1, :"2015-16"=>12.3, :"2016-17"=>12.1, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>3, :Sector=>"Livestock", :"2011-12"=>5, :"2012-13"=>5, :"2013-14"=>5.4, :"2014-15"=>5.7, :"2015-16"=>5.6, :"2016-17"=>5.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>4, :Sector=>"Forestry and logging", :"2011-12"=>1.7, :"2012-13"=>1.7, :"2013-14"=>1.7, :"2014-15"=>1.6, :"2015-16"=>1.5, :"2016-17"=>1.4, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>5, :Sector=>"Fishing and aquaculture", :"2011-12"=>1.3, :"2012-13"=>1.5, :"2013-14"=>1.6, :"2014-15"=>1.7, :"2015-16"=>1.7, :"2016-17"=>1.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>6, :Sector=>"Mining and quarrying", :"2011-12"=>0.1, :"2012-13"=>0.1, :"2013-14"=>0.5, :"2014-15"=>0.2, :"2015-16"=>0.6, :"2016-17"=>0.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}]

In-app Receipt Server Side Validation - Wrong is_trial_period value

I am verifying receipts on the server side with no problem. I want to add extra information such as is_trial_period to our payment table to easily distinguish trial payments from renewed subscriptions but the information I get from apple is confusing. Apple returns is_trial_period as false in "in_app" array whereas the same value for same transaction_id is returned as true in "latest_receipt_info" array. I added a sample below:
{
"status":0,
"environment":"Production",
"receipt":{
"receipt_type":"Production",
"adam_id":1183270669,
"app_item_id":1183270669,
"bundle_id":"com.company.somebundle",
"application_version":"2",
"download_id":67027804835888,
"version_external_identifier":821012057,
"receipt_creation_date":"2017-03-05 03:47:06 Etc\/GMT",
"receipt_creation_date_ms":"1488685626000",
"receipt_creation_date_pst":"2017-03-04 19:47:06 America\/Los_Angeles",
"request_date":"2017-03-07 15:27:10 Etc\/GMT",
"request_date_ms":"1488900430458",
"request_date_pst":"2017-03-07 07:27:10 America\/Los_Angeles",
"original_purchase_date":"2017-03-04 22:26:28 Etc\/GMT",
"original_purchase_date_ms":"1488666388000",
"original_purchase_date_pst":"2017-03-04 14:26:28 America\/Los_Angeles",
"original_application_version":"2",
"in_app":[
{
"quantity":"1",
"product_id":"com.company.new.one.month.optionone",
"transaction_id":"270000278422870",
"original_transaction_id":"270000278422870",
"purchase_date":"2017-03-05 03:47:05 Etc\/GMT",
"purchase_date_ms":"1488685625000",
"purchase_date_pst":"2017-03-04 19:47:05 America\/Los_Angeles",
"original_purchase_date":"2017-03-05 03:47:05 Etc\/GMT",
"original_purchase_date_ms":"1488685625000",
"original_purchase_date_pst":"2017-03-04 19:47:05 America\/Los_Angeles",
"expires_date":"2017-03-12 03:47:05 Etc\/GMT",
"expires_date_ms":"1489290425000",
"expires_date_pst":"2017-03-11 19:47:05 America\/Los_Angeles",
"web_order_line_item_id":"270000054390322",
"is_trial_period":"false"
}
]
},
"latest_receipt_info":[
{
"quantity":"1",
"product_id":"com.company.new.one.month.optionone",
"transaction_id":"270000278422870",
"original_transaction_id":"270000278422870",
"purchase_date":"2017-03-05 03:47:05 Etc\/GMT",
"purchase_date_ms":"1488685625000",
"purchase_date_pst":"2017-03-04 19:47:05 America\/Los_Angeles",
"original_purchase_date":"2017-03-05 03:47:05 Etc\/GMT",
"original_purchase_date_ms":"1488685625000",
"original_purchase_date_pst":"2017-03-04 19:47:05 America\/Los_Angeles",
"expires_date":"2017-03-12 03:47:05 Etc\/GMT",
"expires_date_ms":"1489290425000",
"expires_date_pst":"2017-03-11 19:47:05 America\/Los_Angeles",
"web_order_line_item_id":"270000054390322",
"is_trial_period":"true"
}
]
}
As far as I know, "in_app" array has to be used for all kind of payments but "latest_receipt_info" array gives correct trial status which should be true for my case. Any help or would be really appreciated.

How to get required hash from a hash array

I have an array of hashes of the form:
[{"status"=>"Unshipped", "city"=>"thane", "buyer_name"=>"abc", "name"=>"abc def", "countryCode"=>"IN", "payment_method"=>"COD", "order_type"=>"StandardOrder", "y_id"=>"r123", "phone"=>"12345", "state"=>"Maharashtra", "service"=>"Expedited", "address_1"=>"abc xyz", "address_2"=>"yyyy", "postalCode"=>"400607", "shipped_by_y"=>"false", "channel"=>"MFN", "amout"=>"350.00"}, {"status"=>"Unshipped", "city"=>"Chembur, Mumbai", "buyer_name"=>"xyz", "name"=>"xyz lmn", "countryCode"=>"IN", "payment_method"=>"Other", "order_type"=>"StandardOrder", "y_id"=>"r124", "phone"=>"12436", "state"=>"Maharashtra", "service"=>"Expedited", "address_1"=>"add 1", "address_2"=>"add 2", "postalCode"=>"400071", "shipped_by_y"=>"false", "channel"=>"MFN", "amout"=>"399.00"}]
From this array I want to select the element which has given value for the key y_id.
Eg. if I want to get the element with 'y_id' = 'r124', I should get :
{"status"=>"Unshipped", "city"=>"Chembur, Mumbai", "buyer_name"=>"xyz", "name"=>"xyz lmn", "countryCode"=>"IN", "payment_method"=>"Other", "order_type"=>"StandardOrder", "y_id"=>"r124", "phone"=>"12436", "state"=>"Maharashtra", "service"=>"Expedited", "address_1"=>"add 1", "address_2"=>"add 2", "postalCode"=>"400071", "shipped_by_y"=>"false", "channel"=>"MFN", "amout"=>"399.00"}
You should use the find API. It returns the first element which matches what you look for, or nil if none is found:
arr.find { |element| element['y_id'] == 'r124' }
will return:
{"status"=>"Unshipped", "city"=>"Chembur, Mumbai", "buyer_name"=>"xyz", "name"=>"xyz lmn", "countryCode"=>"IN", "payment_method"=>"Other", "order_type"=>"StandardOrder", "y_id"=>"r124", "phone"=>"12436", "state"=>"Maharashtra", "service"=>"Expedited", "address_1"=>"add 1", "address_2"=>"add 2", "postalCode"=>"400071", "shipped_by_y"=>"false", "channel"=>"MFN", "amout"=>"399.00"}
You should be able to use
arr.find { |i| i['y_id] == *value* }
Which will iterate over your array of hashes, where i is any given hash. Select will return an array of objects where the block resolves to true.
So, for example if you passed in r123 as value, it would return
{"status"=>"Unshipped", "city"=>"thane", "buyer_name"=>"abc", "name"=>"abc def", "countryCode"=>"IN", "payment_method"=>"COD", "order_type"=>"StandardOrder", "y_id"=>"r123", "phone"=>"12345", "state"=>"Maharashtra", "service"=>"Expedited", "address_1"=>"abc xyz", "address_2"=>"yyyy", "postalCode"=>"400607", "shipped_by_y"=>"false", "channel"=>"MFN", "amout"=>"350.00"}
to get the hash. (Thanks #BroiSatse)

Resources