In-app Receipt Server Side Validation - Wrong is_trial_period value - ios

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.

Related

Rails: Sort Array based on an other array

I want to Sort Array in Ruby on Rails based on an other Array but still wants to keep first Array values in the result :-
all_countries = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Argentina", "Armenia", "Armenien", "Australia", ....,]
gcc = ["UAE", "Saudi Arabia", "Qatar", "Bahrain", "Kuwait", "Oman"]
Desired OUTPUT:
I want to sort all countries but want GCC countries (Without Sorting - any order) to appear first, remaining countries should appear in sorting (A-Z).
["UAE", "Saudi Arabia", "Qatar", "Bahrain", "Kuwait", "Oman", "Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa" .....]
I can do it in following way but it doesn't seem to be very good code and break alphabetical sorting for non-gcc countries.
countries.sort_by{|x| gcc.index(x) || gcc.size }
Any better way to do it ?
gcc + (all_countries - gcc).sort

how do I handle post request containing data like `array(1) {["key"]=> "value"}`?

From a 3rd party payment gateway system that we are using, we are getting following response for my request. How can I handle this request. It clearly does not look like a json.
array(18) {
["tran_id"]=>
string(13) "572F1950EA6D7"
["error"]=>
string(17) "Cancelled by User"
["amount"]=>
string(5) "783.8"
["bank_tran_id"]=>
string(0) ""
["status"]=>
string(9) "CANCELLED"
["tran_date"]=>
string(19) "2016-05-08 16:47:45"
["currency"]=>
string(3) "BDT"
["store_id"]=>
string(7) "testbox"
["verify_sign"]=>
string(32) "dd6f94dc58f31b9d75e3efd1fb6c7a9c"
["verify_key"]=>
string(154) "amount,bank_tran_id,base_fair,currency,currency_amount,currency_rate,currency_type,error,status,store_id,tran_date,tran_id,value_a,value_b,value_c,value_d"
["currency_type"]=>
string(3) "USD"
["currency_amount"]=>
string(5) "10.00"
["currency_rate"]=>
string(5) "78.38"
["base_fair"]=>
string(4) "0.00"
["value_a"]=>
string(6) "REF001"
["value_b"]=>
string(6) "REF002"
["value_c"]=>
string(6) "REF003"
["value_d"]=>
string(6) "REF004"
}
This seems to be the output of a PHP var_dump() function. This answer has an unvar_dump function to convert it back, but it's written in PHP.
For Ruby, this is the closest I could find.

Sort json data alphabetically in ruby on rails

I have this data with me:
[{:id=>250,
:application_date=>"02/04/2016",
:customer_number=>"",
:customer_name=>"Neymar Silva Junior",
:city=>"Auckland",
:region=>"Auckland",
:service=>"Electricity",
:name=>"Bosco and Sons",
:service_plan=>"Electricity Plan",
:connection_type=>nil,
:billing_method=>nil,
:icp_number=>nil,
:moving_date=>"",
:supplier_commission=>21.0,
:show_url=>"/applications/250"},
{:id=>257,
:application_date=>"27/05/2016",
:customer_number=>"",
:customer_name=>"Ariel name Parra",
:city=>"Dunedin",
:region=>"Dunedin",
:service=>"Electricity",
:name=>"Bosco and Sons",
:service_plan=>"Electricity Plan",
:connection_type=>nil,
:billing_method=>nil,
:icp_number=>nil,
:moving_date=>"28/05/2016",
:supplier_commission=>21.0,
:show_url=>"/applications/257"},
{:id=>291,
:application_date=>"29/04/2016",
:customer_number=>"aaaa",
:customer_name=>"Neymar Silva Junior",
:city=>"Auckland",
:region=>"Auckland",
:service=>"Electricity",
:name=>"Bosco and Sons",
:service_plan=>"Electricity Plan",
:connection_type=>nil,
:billing_method=>nil,
:icp_number=>"",
:moving_date=>"",
:supplier_commission=>28.0,
:show_url=>"/applications/291"},
{:id=>292,
:application_date=>"29/04/2016",
:customer_number=>"23223",
:customer_name=>"Neymar Silva Junior",
:city=>"Auckland",
:region=>"Auckland",
:service=>"Electricity",
:name=>"Bosco and Sons",
:service_plan=>"Electricity Plan",
:connection_type=>nil,
:billing_method=>nil,
:icp_number=>"",
:moving_date=>"",
:supplier_commission=>21.0,
:show_url=>"/applications/292"}]
I want to sort this data in two different ways, alphabetically(from A to Z) as well as Recursively(Z to A) according to its attributes in following scenarios:
If the sort parameter is service_plan alphabetically it will sort as per this attribute from A to Z, if recursively then Z to A for this attribute and so on for all attributes.
Id is integer so it should be sorted in increasing or decreasing order.
Moreover the nil value should not through an error and should be present in the result.
Thanks in advance!
def my_sort(data, attribute, asc = true)
# Make sure that all elements have attribute we want the data to be sorted by
return data unless data.all? { |elem| elem.key?(attribute) }
sorted = data.sort_by { |elem| elem[attribute] }
asc ? sorted : sorted.reverse
end
my_sort(data, :id) # ascending order by default
my_sort(data, :city, false)
If you want to sort by element that can be missing:
def my_sort(data, attribute, asc = true)
# Convert to string because of possible nil values
sorted = data.sort_by { |elem| elem[attribute].to_s }
asc ? sorted : sorted.reverse
end

Extract plan from Stripe

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.

Why isnt Nokogiri grabbing data as an array

I am using Nokogiri to grab data from a webpage, I was under the impression that the following would grab the data and return is as an array? Instead I am getting one big string which is causing a few issues.
home_team = doc.css(".team-home.teams")
if i was to use
home_team = doc.css(".team-home.teams").text
i could understand the data being returned as as string. Am i looking at this the wrong way?
I have even tried
home_team = doc.css(".team-home.teams").map(&:text)
but that seems to be returning a string aswell? If i was getting an array returned in the console it would be in array format yes?
If someone could try this in their console
require 'open-uri'
require 'nokogiri'
FIXTURE_URL = "http://www.bbc.co.uk/sport/football/premier-league/fixtures"
doc = Nokogiri::HTML(open(FIXTURE_URL))
home_team = doc.css(".team-home.teams").map(&:text)
#home_team = doc.css(".team-home.teams")
puts home_team
and just confirm that the output is a string in both cases and what the difference between the two are. slightly lost at the mo
Thanks
You are getting an array. It's just that puts is doing a to_s on. Check this out:
require 'open-uri'
require 'nokogiri'
FIXTURE_URL = "http://www.bbc.co.uk/sport/football/premier-league/fixtures"
doc = Nokogiri::HTML(open(FIXTURE_URL))
home_team = doc.css(".team-home.teams").map(&:text)
# home_team = doc.css(".team-home.teams")
puts home_team.class
puts home_team.map(&:strip).inspect
#=> Array
#=> ["Everton", "Aston Villa", "Southampton", "Stoke", "Swansea", "Man Utd", "Sunderland", "Tottenham", "Chelsea", "Wigan", "Sunderland", "Arsenal", "Man City", "Swansea", "West Ham", "Wigan", "Everton", "Aston Villa", "Southampton", "Fulham", "Reading", "Chelsea", "Newcastle", "Norwich", "Stoke", "West Brom", "Liverpool", "Tottenham", "QPR", "Man Utd", "Newcastle", "Arsenal", "Aston Villa", "Everton", "Reading", "Southampton", "Stoke", "Chelsea", "Arsenal", "Fulham", "Norwich", "QPR", "Sunderland", "Swansea", "West Brom", "West Ham", "Tottenham", "Liverpool", "Man Utd", "Man City", "Aston Villa", "Chelsea", "Everton", "Southampton", "Stoke", "Wigan", "Newcastle", "Reading", "Arsenal", "Fulham", "Liverpool", "Man Utd", "Norwich", "QPR", "Sunderland", "Swansea", "Tottenham", "West Brom", "West Ham", "Arsenal", "Aston Villa", "Everton", "Fulham", "Man Utd", "Norwich", "QPR", "Reading", "Stoke", "Sunderland", "Chelsea", "Liverpool", "Man City", "Newcastle", "Southampton", "Swansea", "Tottenham", "West Brom", "West Ham", "Wigan"]
There's a lot of white space in the data. I get an array when I do this:
home_team = doc.css(".team-home.teams").map {|team| team.text.strip}

Resources