Showing
3 changed files
with
21 additions
and
6 deletions
| ... | @@ -23,14 +23,15 @@ module Var | ... | @@ -23,14 +23,15 @@ module Var |
| 23 | object.charge_with(service, options) | 23 | object.charge_with(service, options) |
| 24 | end | 24 | end |
| 25 | 25 | ||
| 26 | - def conekta_webhook(params) | 26 | + def conekta_webhook(event) |
| 27 | - payment = params[:data][:object] | 27 | + raise "Hash expected, received #{event.class}" unless event.class == Hash |
| 28 | + raise 'Malformed hash received' unless event[:data] && | ||
| 29 | + event[:data][:object] | ||
| 30 | + payment = event[:data][:object] | ||
| 28 | object = Var.find_charge payment[:id] | 31 | object = Var.find_charge payment[:id] |
| 32 | + # object.handle_conekta_event | ||
| 29 | update_object_var_data(object, payment) | 33 | update_object_var_data(object, payment) |
| 30 | object | 34 | object |
| 31 | - # rescue Exception => exception | ||
| 32 | - # puts exception | ||
| 33 | - # false | ||
| 34 | end | 35 | end |
| 35 | 36 | ||
| 36 | def update_object_var_data(object, data) | 37 | def update_object_var_data(object, data) | ... | ... |
| 1 | require 'pry' | 1 | require 'pry' |
| 2 | require 'active_record' | 2 | require 'active_record' |
| 3 | require 'active_support' | 3 | require 'active_support' |
| 4 | -require 'Var' | 4 | +require 'var' |
| 5 | 5 | ||
| 6 | require 'mocks/var_database_mock' | 6 | require 'mocks/var_database_mock' |
| 7 | require 'mocks/var_models_mock' | 7 | require 'mocks/var_models_mock' | ... | ... |
| ... | @@ -50,6 +50,20 @@ describe Var do | ... | @@ -50,6 +50,20 @@ describe Var do |
| 50 | Product.new # Weird hack to enable connection | 50 | Product.new # Weird hack to enable connection |
| 51 | instance_double('Product') | 51 | instance_double('Product') |
| 52 | end | 52 | end |
| 53 | + | ||
| 54 | + it 'expects a Hash as an input' do | ||
| 55 | + [[], '', nil].each do |input| | ||
| 56 | + expect do | ||
| 57 | + Var.conekta_webhook(input) | ||
| 58 | + end.to raise_error "Hash expected, received #{input.class}" | ||
| 59 | + end | ||
| 60 | + end | ||
| 61 | + | ||
| 62 | + it 'expects a {data: {object: ... }} as an input' do | ||
| 63 | + expect do | ||
| 64 | + Var.conekta_webhook({}) | ||
| 65 | + end.to raise_error "Malformed hash received" | ||
| 66 | + end | ||
| 53 | 67 | ||
| 54 | it 'handles Cargo creado con tarjeta' do | 68 | it 'handles Cargo creado con tarjeta' do |
| 55 | params = {"data":{"object":{"id":"5511d4ce2412294cf6000081","livemode":false,"created_at":1427231950,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":nil,"failure_message":nil,"monthly_installments":nil,"object":"charge","amount":20000,"paid_at":nil,"fee":963,"customer_id":"","refunds":[],"payment_method":{"name":"Jorge Lopez","exp_month":"12","exp_year":"19","auth_code":nil,"object":"card_payment","last4":"4242","brand":"visa"},"details":{"name":nil,"phone":nil,"email":nil,"line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"5511d4ce2412294cf6000084","object":"event","type":"charge.created","created_at":1427231950,"webhook_logs":[{"id":"webhl_nPzGMBeQmMUu7aQ","url":"http://requestb.in/1em0jsx1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":0}]} | 69 | params = {"data":{"object":{"id":"5511d4ce2412294cf6000081","livemode":false,"created_at":1427231950,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":nil,"failure_message":nil,"monthly_installments":nil,"object":"charge","amount":20000,"paid_at":nil,"fee":963,"customer_id":"","refunds":[],"payment_method":{"name":"Jorge Lopez","exp_month":"12","exp_year":"19","auth_code":nil,"object":"card_payment","last4":"4242","brand":"visa"},"details":{"name":nil,"phone":nil,"email":nil,"line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"5511d4ce2412294cf6000084","object":"event","type":"charge.created","created_at":1427231950,"webhook_logs":[{"id":"webhl_nPzGMBeQmMUu7aQ","url":"http://requestb.in/1em0jsx1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":0}]} | ... | ... |
-
Please register or login to post a comment