Marcelo

Progress in var specs

1 +--color
...\ No newline at end of file ...\ No newline at end of file
1 +module Var
2 + class ConektaCardHandler < ConektaHandler
3 + PAYMENT_METHOD_STRING = "Card".freeze
4 + end
5 +end
...\ No newline at end of file ...\ No newline at end of file
1 +module Var
2 + class ConektaHandler
3 + PAYMENT_METHOD_STRING = 'Conekta_payment'.freeze
4 +
5 + class << self
6 + def analyze(event)
7 + process_payment event[:data][:object] if
8 + event[:data][:object][:status] == "paid"
9 + process_charge event[:data][:object]
10 + end
11 +
12 + def process_payment(payment_info)
13 + object = Var.find_charge payment_info[:id]
14 + time = Time.strptime payment_info[:paid_at].to_s, '%s'
15 + amount = payment_info[:amount].to_f / 100.0
16 + fee = payment_info[:fee].to_f / 100.0
17 +
18 + object.update(var_fee: fee, var_paid_amount: amount,
19 + var_payment_method: self::PAYMENT_METHOD_STRING,
20 + var_payment_at: time,
21 + var_status: payment_info[:status])
22 + end
23 +
24 + def process_charge(charge_info)
25 + # TODO: raise error if object is nil
26 + object = Var.find_charge charge_info[:id]
27 + object.update(var_status: charge_info[:status])
28 + end
29 + end
30 + end
31 +end
...\ No newline at end of file ...\ No newline at end of file
1 +module Var
2 + class ConektaOxxoHandler < ConektaHandler
3 + PAYMENT_METHOD_STRING = "OXXO".freeze
4 + end
5 +end
...\ No newline at end of file ...\ No newline at end of file
1 +module Var
2 + class ConektaPayoutHandler
3 + class << self
4 + def analyze(event)
5 + end
6 + end
7 + end
8 +end
...\ No newline at end of file ...\ No newline at end of file
1 +module Var
2 + class ConektaSpeiHandler < ConektaHandler
3 + PAYMENT_METHOD_STRING = "Bank Transfer".freeze
4 + end
5 +end
...\ No newline at end of file ...\ No newline at end of file
1 class AddVarStatusTo<%=@model_name.pluralize.camelize%> < ActiveRecord::Migration 1 class AddVarStatusTo<%=@model_name.pluralize.camelize%> < ActiveRecord::Migration
2 def change 2 def change
3 - add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending' 3 + add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending_payment'
4 add_column :<%=@model_name.pluralize.underscore%>, :var_barcode_url, :string 4 add_column :<%=@model_name.pluralize.underscore%>, :var_barcode_url, :string
5 add_column :<%=@model_name.pluralize.underscore%>, :var_barcode, :string 5 add_column :<%=@model_name.pluralize.underscore%>, :var_barcode, :string
6 add_column :<%=@model_name.pluralize.underscore%>, :var_payment, :datetime 6 add_column :<%=@model_name.pluralize.underscore%>, :var_payment, :datetime
......
...@@ -2,6 +2,11 @@ require 'var/version' ...@@ -2,6 +2,11 @@ require 'var/version'
2 require 'conekta' 2 require 'conekta'
3 require 'paypal-sdk-rest' 3 require 'paypal-sdk-rest'
4 require 'acts_as_chargeable' 4 require 'acts_as_chargeable'
5 +require 'conekta_handler'
6 +require 'conekta_oxxo_handler'
7 +require 'conekta_card_handler'
8 +require 'conekta_spei_handler'
9 +require 'conekta_payout_handler'
5 10
6 # Main Module 11 # Main Module
7 module Var 12 module Var
...@@ -25,18 +30,28 @@ module Var ...@@ -25,18 +30,28 @@ module Var
25 30
26 def conekta_webhook(event) 31 def conekta_webhook(event)
27 raise "Hash expected, received #{event.class}" unless event.class == Hash 32 raise "Hash expected, received #{event.class}" unless event.class == Hash
28 - raise 'Malformed hash received' unless event[:data] && 33 + raise 'Malformed hash received' unless (event[:data] &&
29 - event[:data][:object] 34 + event[:data][:object]) ||
30 - payment = event[:data][:object] 35 + event[:object]
31 - process_charge_creation_with_card event 36 + if event.keys[0] == :object
37 + ConektaPayoutHandler.analyze(event)
38 + else
39 + payment_type = event[:data][:object][:payment_method][:object]
40 + case payment_type
41 + when "cash_payment"
42 + ConektaOxxoHandler.analyze(event)
43 + when "card_payment"
44 + ConektaCardHandler.analyze(event)
45 + when "bank_transfer_payment"
46 + ConektaSpeiHandler.analyze(event)
47 + end
48 + end
49 + # payment = event[:data][:object]
32 # object = Var.find_charge payment[:id] 50 # object = Var.find_charge payment[:id]
33 # # object.handle_conekta_event 51 # # object.handle_conekta_event
34 # update_object_var_data(object, payment) 52 # update_object_var_data(object, payment)
35 # object 53 # object
36 end 54 end
37 -
38 - def process_charge_creation_with_card(p)
39 - end
40 55
41 def update_object_var_data(object, data) 56 def update_object_var_data(object, data)
42 payment = data 57 payment = data
...@@ -51,7 +66,7 @@ module Var ...@@ -51,7 +66,7 @@ module Var
51 end 66 end
52 67
53 def find_charge(id) 68 def find_charge(id)
54 - Rails.application.eager_load! if Rails.env.development? 69 + # Rails.application.eager_load! if Rails.env.development?
55 @@var_classes.map do |class_name| 70 @@var_classes.map do |class_name|
56 class_name.where(var_id: id) 71 class_name.where(var_id: id)
57 end.flatten.first 72 end.flatten.first
......
1 +require 'spec_helper'
2 +
3 +describe ConektaHandler do
4 + it 'processes oxxo payment' do
5 + end
6 +
7 + it 'processes card payment' do
8 + end
9 +
10 + it 'processes oxxo payment' do
11 + end
12 +
13 + it 'processes payout' do
14 + end
15 +end
...\ No newline at end of file ...\ No newline at end of file
...@@ -65,46 +65,88 @@ describe Var do ...@@ -65,46 +65,88 @@ describe Var do
65 end.to raise_error "Malformed hash received" 65 end.to raise_error "Malformed hash received"
66 end 66 end
67 67
68 - it 'handles Cargo creado con tarjeta' do 68 + it 'handles card charge creation' do
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}]} 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}]}
70 - 70 + cardHandler = class_double("Var::ConektaCardHandler").
71 - expect(Var).to receive(:process_charge_creation_with_card).with(params) 71 + as_stubbed_const(transfer_nested_constants: true)
72 - r = Var.conekta_webhook params 72 + expect(cardHandler).to receive(:analyze).with(params)
73 - expect(r).to be true 73 + Var.conekta_webhook(params)
74 end 74 end
75 75
76 - xit 'processes card payment' do 76 + it 'handles oxxo charge creation' do
77 - 77 + null = nil
78 - # allow(Var).to receive(:find_charge).and_return chargable_product 78 + params = {"data":{"object":{"id":"5514803f241229981e0022e6","livemode":false,"created_at":1427406911,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":null,"failure_message":null,"monthly_installments":null,"object":"charge","amount":20000,"paid_at":null,"fee":812,"customer_id":"","refunds":[],"payment_method":{"barcode":"38100000000042290121213001160013","barcode_url":"http://s3.amazonaws.com/cash_payment_barcodes/38100000000042290121213001160013.png","object":"cash_payment","type":"oxxo","expires_at":1430006400,"store_name":"OXXO"},"details":{"name":"Wolverine","phone":"403-342-0642","email":"logan@x-men.org","line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"5514803f241229981e0022e9","object":"event","type":"charge.created","created_at":1427406911,"webhook_logs":[{"id":"webhl_PBmYvkfdNDYKezJ","url":"http://requestb.in/u3n4ywu3","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":0}]}
79 - # 79 + oxxoHandler = class_double("Var::ConektaOxxoHandler").
80 - # allow(chargable_product).to receive(:update_columns).and_return true 80 + as_stubbed_const(transfer_nested_constants: true)
81 - # allow(chargable_product).to receive(:var_payed?).and_return false 81 + expect(oxxoHandler).to receive(:analyze).with(params)
82 - # allow(chargable_product).to receive(:var_id).and_return '5511d4ce2412294cf6000081' 82 + Var.conekta_webhook(params)
83 + end
83 84
84 - # expect(result.var_id).to eq('5511d4ce2412294cf6000081') 85 + it 'handles SPEI charge creation' do
86 + null = nil
87 + params = {"data":{"object":{"id":"551499322412292eec002159","livemode":false,"created_at":1427413298,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":null,"failure_message":null,"monthly_installments":null,"object":"charge","amount":20000,"paid_at":null,"fee":928,"customer_id":"","refunds":[],"payment_method":{"clabe":"646180111800012432","bank":"STP","issuing_account_holder":null,"issuing_account_tax_id":null,"issuing_account_bank":null,"issuing_account_number":null,"receiving_account_holder":null,"receiving_account_tax_id":null,"receiving_account_number":"646180111800012432","receiving_account_bank":"STP","reference_number":null,"description":null,"tracking_code":null,"executed_at":null,"object":"bank_transfer_payment","type":"spei","expires_at":1435189298},"details":{"name":"Wolverine","phone":"403-342-0642","email":"logan@x-men.org","line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"551499322412292eec00215c","object":"event","type":"charge.created","created_at":1427413298,"webhook_logs":[{"id":"webhl_ENPXoFkKWyDqTAt","url":"http://requestb.in/151xj4r1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":1427413285}]}
88 + speiHandler = class_double("Var::ConektaSpeiHandler").
89 + as_stubbed_const(transfer_nested_constants: true)
90 + expect(speiHandler).to receive(:analyze).with(params)
91 + Var.conekta_webhook(params)
92 + end
85 93
86 - # TODO: Define what is actually being teted 94 + it 'handles card payment' do
87 - 95 + null = nil
96 + params = {"data":{"object":{"id":"5511d4ce2412294cf6000081","livemode":false,"created_at":1427231950,"status":"paid","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":null,"failure_message":null,"monthly_installments":null,"object":"charge","amount":20000,"paid_at":1427231952,"fee":963,"customer_id":"","refunds":[],"payment_method":{"name":"Jorge Lopez","exp_month":"12","exp_year":"19","auth_code":"000000","object":"card_payment","last4":"4242","brand":"visa"},"details":{"name":null,"phone":null,"email":null,"line_items":[]}},"previous_attributes":{"payment_method":{}}},"livemode":false,"webhook_status":"pending","id":"5511d4d02412294cf6000088","object":"event","type":"charge.paid","created_at":1427231952,"webhook_logs":[{"id":"webhl_m5kgkhmgETe6Y4s","url":"http://requestb.in/1em0jsx1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":0}]}
97 + cardHandler = class_double("Var::ConektaCardHandler").
98 + as_stubbed_const(transfer_nested_constants: true)
99 + expect(cardHandler).to receive(:analyze).with(params)
100 + Var.conekta_webhook(params)
88 end 101 end
89 - 102 +
90 - it 'handles Cargo pagado con tarjeta' do 103 + it 'handles oxxo payment' do
91 - pending 104 + null = nil
105 + params = {"data":{"object":{"id":"5514803f241229981e0022e6","livemode":false,"created_at":1427406911,"status":"paid","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":null,"failure_message":null,"monthly_installments":null,"object":"charge","amount":20000,"paid_at":1427406913,"fee":812,"customer_id":"","refunds":[],"payment_method":{"barcode":"38100000000042290121213001160013","barcode_url":"http://s3.amazonaws.com/cash_payment_barcodes/38100000000042290121213001160013.png","object":"cash_payment","type":"oxxo","expires_at":1430006400,"store_name":"OXXO"},"details":{"name":"Wolverine","phone":"403-342-0642","email":"logan@x-men.org","line_items":[]}},"previous_attributes":{"status":"pending_payment"}},"livemode":false,"webhook_status":"pending","id":"55148042241229d4fb00b58a","object":"event","type":"charge.paid","created_at":1427406914,"webhook_logs":[{"id":"webhl_yYes9gx41vAj6ag","url":"http://requestb.in/151xj4r1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":1427406984}]}
106 + oxxoHandler = class_double("Var::ConektaOxxoHandler").
107 + as_stubbed_const(transfer_nested_constants: true)
108 + expect(oxxoHandler).to receive(:analyze).with(params)
109 + Var.conekta_webhook(params)
92 end 110 end
93 111
94 - it 'handles Cargo de OXXO creado' do 112 + it 'handles SPEI payment' do
95 - pending 113 + null = nil
114 + params = {"data":{"object":{"id":"551499322412292eec002159","livemode":false,"created_at":1427413298,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":null,"failure_message":null,"monthly_installments":null,"object":"charge","amount":20000,"paid_at":null,"fee":928,"customer_id":"","refunds":[],"payment_method":{"clabe":"646180111800012432","bank":"STP","issuing_account_holder":null,"issuing_account_tax_id":null,"issuing_account_bank":null,"issuing_account_number":null,"receiving_account_holder":null,"receiving_account_tax_id":null,"receiving_account_number":"646180111800012432","receiving_account_bank":"STP","reference_number":null,"description":null,"tracking_code":null,"executed_at":null,"object":"bank_transfer_payment","type":"spei","expires_at":1435189298},"details":{"name":"Wolverine","phone":"403-342-0642","email":"logan@x-men.org","line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"551499322412292eec00215c","object":"event","type":"charge.created","created_at":1427413298,"webhook_logs":[{"id":"webhl_ENPXoFkKWyDqTAt","url":"http://requestb.in/151xj4r1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":1427413285}]}
115 + speiHandler = class_double("Var::ConektaSpeiHandler").
116 + as_stubbed_const(transfer_nested_constants: true)
117 + expect(speiHandler).to receive(:analyze).with(params)
118 + Var.conekta_webhook(params)
96 end 119 end
97 120
98 - it 'handles Cargo de OXXO pagado' do 121 + it 'handles payout scheduled' do
99 - pending 122 + null = nil
123 + params = {"object":{"livemode":true,"currency":"MXN","method":{"id":"pytmtd_MymQ7favB2hQJC","created_at":1467832748,"payee_id":null,"_type":"BankTransferPayoutMethod","object":"bank_transfer_payout_method","account_number":"072580002374915018","account_holder":"UKKO SOLUTIONS SA DE CV","bank":"Banco Mercantil del Norte, S.A."},"object":"transfer","amount":303534,"id":"577d59ac243c7fee8100146d","created_at":1467832748,"status":"scheduled","statement_reference":"4632539","statement_description":"Conekta 4632539"},"previous_attributes":{"status":"pending","broker_transaction_reference":null}}
124 + payoutHandler = class_double("Var::ConektaPayoutHandler").
125 + as_stubbed_const(transfer_nested_constants: true)
126 + expect(payoutHandler).to receive(:analyze).with(params)
127 + Var.conekta_webhook(params)
100 end 128 end
101 129
102 - xit 'handles Cargo de SPEI creado' do 130 + it 'handles payout paid' do
103 - pending 131 + null = nil
132 + params = {"object":{"livemode":true,"currency":"MXN","method":{"id":"pytmtd_MymQ7favB2hQJC","created_at":1467832748,"payee_id":null,"_type":"BankTransferPayoutMethod","object":"bank_transfer_payout_method","account_number":"072580002374915018","account_holder":"UKKO SOLUTIONS SA DE CV","bank":"Banco Mercantil del Norte, S.A."},"object":"transfer","amount":303534,"id":"577d59ac243c7fee8100146d","created_at":1467832748,"status":"scheduled","statement_reference":"4632539","statement_description":"Conekta 4632539"},"previous_attributes":{}}
133 + payoutHandler = class_double("Var::ConektaPayoutHandler").
134 + as_stubbed_const(transfer_nested_constants: true)
135 + expect(payoutHandler).to receive(:analyze).with(params)
136 + Var.conekta_webhook(params)
104 end 137 end
105 138
106 - xit 'handles Cargo de SPEI pagado' do 139 + it 'processes card charge' do
107 - pending 140 +
141 + # allow(Var).to receive(:find_charge).and_return chargable_product
142 + #
143 + # allow(chargable_product).to receive(:update_columns).and_return true
144 + # allow(chargable_product).to receive(:var_payed?).and_return false
145 + # allow(chargable_product).to receive(:var_id).and_return '5511d4ce2412294cf6000081'
146 +
147 + # expect(result.var_id).to eq('5511d4ce2412294cf6000081')
148 + # TODO: Define what is actually being teted
149 +
108 end 150 end
109 151
110 xit 'handles Plan creado' do 152 xit 'handles Plan creado' do
......