Marcelo

Progress in var specs

--color
\ No newline at end of file
module Var
class ConektaCardHandler < ConektaHandler
PAYMENT_METHOD_STRING = "Card".freeze
end
end
\ No newline at end of file
module Var
class ConektaHandler
PAYMENT_METHOD_STRING = 'Conekta_payment'.freeze
class << self
def analyze(event)
process_payment event[:data][:object] if
event[:data][:object][:status] == "paid"
process_charge event[:data][:object]
end
def process_payment(payment_info)
object = Var.find_charge payment_info[:id]
time = Time.strptime payment_info[:paid_at].to_s, '%s'
amount = payment_info[:amount].to_f / 100.0
fee = payment_info[:fee].to_f / 100.0
object.update(var_fee: fee, var_paid_amount: amount,
var_payment_method: self::PAYMENT_METHOD_STRING,
var_payment_at: time,
var_status: payment_info[:status])
end
def process_charge(charge_info)
# TODO: raise error if object is nil
object = Var.find_charge charge_info[:id]
object.update(var_status: charge_info[:status])
end
end
end
end
\ No newline at end of file
module Var
class ConektaOxxoHandler < ConektaHandler
PAYMENT_METHOD_STRING = "OXXO".freeze
end
end
\ No newline at end of file
module Var
class ConektaPayoutHandler
class << self
def analyze(event)
end
end
end
end
\ No newline at end of file
module Var
class ConektaSpeiHandler < ConektaHandler
PAYMENT_METHOD_STRING = "Bank Transfer".freeze
end
end
\ No newline at end of file
class AddVarStatusTo<%=@model_name.pluralize.camelize%> < ActiveRecord::Migration
def change
add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending'
add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending_payment'
add_column :<%=@model_name.pluralize.underscore%>, :var_barcode_url, :string
add_column :<%=@model_name.pluralize.underscore%>, :var_barcode, :string
add_column :<%=@model_name.pluralize.underscore%>, :var_payment, :datetime
......
......@@ -2,6 +2,11 @@ require 'var/version'
require 'conekta'
require 'paypal-sdk-rest'
require 'acts_as_chargeable'
require 'conekta_handler'
require 'conekta_oxxo_handler'
require 'conekta_card_handler'
require 'conekta_spei_handler'
require 'conekta_payout_handler'
# Main Module
module Var
......@@ -25,18 +30,28 @@ module Var
def conekta_webhook(event)
raise "Hash expected, received #{event.class}" unless event.class == Hash
raise 'Malformed hash received' unless event[:data] &&
event[:data][:object]
payment = event[:data][:object]
process_charge_creation_with_card event
raise 'Malformed hash received' unless (event[:data] &&
event[:data][:object]) ||
event[:object]
if event.keys[0] == :object
ConektaPayoutHandler.analyze(event)
else
payment_type = event[:data][:object][:payment_method][:object]
case payment_type
when "cash_payment"
ConektaOxxoHandler.analyze(event)
when "card_payment"
ConektaCardHandler.analyze(event)
when "bank_transfer_payment"
ConektaSpeiHandler.analyze(event)
end
end
# payment = event[:data][:object]
# object = Var.find_charge payment[:id]
# # object.handle_conekta_event
# update_object_var_data(object, payment)
# object
end
def process_charge_creation_with_card(p)
end
def update_object_var_data(object, data)
payment = data
......@@ -51,7 +66,7 @@ module Var
end
def find_charge(id)
Rails.application.eager_load! if Rails.env.development?
# Rails.application.eager_load! if Rails.env.development?
@@var_classes.map do |class_name|
class_name.where(var_id: id)
end.flatten.first
......
require 'spec_helper'
describe ConektaHandler do
it 'processes oxxo payment' do
end
it 'processes card payment' do
end
it 'processes oxxo payment' do
end
it 'processes payout' do
end
end
\ No newline at end of file
......@@ -65,46 +65,88 @@ describe Var do
end.to raise_error "Malformed hash received"
end
it 'handles Cargo creado con tarjeta' do
it 'handles card charge creation' do
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}]}
expect(Var).to receive(:process_charge_creation_with_card).with(params)
r = Var.conekta_webhook params
expect(r).to be true
cardHandler = class_double("Var::ConektaCardHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(cardHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
xit 'processes card payment' do
# allow(Var).to receive(:find_charge).and_return chargable_product
#
# allow(chargable_product).to receive(:update_columns).and_return true
# allow(chargable_product).to receive(:var_payed?).and_return false
# allow(chargable_product).to receive(:var_id).and_return '5511d4ce2412294cf6000081'
it 'handles oxxo charge creation' do
null = nil
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}]}
oxxoHandler = class_double("Var::ConektaOxxoHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(oxxoHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
# expect(result.var_id).to eq('5511d4ce2412294cf6000081')
it 'handles SPEI charge creation' do
null = nil
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}]}
speiHandler = class_double("Var::ConektaSpeiHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(speiHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
# TODO: Define what is actually being teted
it 'handles card payment' do
null = nil
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}]}
cardHandler = class_double("Var::ConektaCardHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(cardHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
it 'handles Cargo pagado con tarjeta' do
pending
it 'handles oxxo payment' do
null = nil
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}]}
oxxoHandler = class_double("Var::ConektaOxxoHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(oxxoHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
it 'handles Cargo de OXXO creado' do
pending
it 'handles SPEI payment' do
null = nil
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}]}
speiHandler = class_double("Var::ConektaSpeiHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(speiHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
it 'handles Cargo de OXXO pagado' do
pending
it 'handles payout scheduled' do
null = nil
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}}
payoutHandler = class_double("Var::ConektaPayoutHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(payoutHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
xit 'handles Cargo de SPEI creado' do
pending
it 'handles payout paid' do
null = nil
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":{}}
payoutHandler = class_double("Var::ConektaPayoutHandler").
as_stubbed_const(transfer_nested_constants: true)
expect(payoutHandler).to receive(:analyze).with(params)
Var.conekta_webhook(params)
end
xit 'handles Cargo de SPEI pagado' do
pending
it 'processes card charge' do
# allow(Var).to receive(:find_charge).and_return chargable_product
#
# allow(chargable_product).to receive(:update_columns).and_return true
# allow(chargable_product).to receive(:var_payed?).and_return false
# allow(chargable_product).to receive(:var_id).and_return '5511d4ce2412294cf6000081'
# expect(result.var_id).to eq('5511d4ce2412294cf6000081')
# TODO: Define what is actually being teted
end
xit 'handles Plan creado' do
......