Builds for
1 pipeline
failed
in
0 seconds
Finished first draft of all act_as_chargeable specs
Showing
4 changed files
with
220 additions
and
13 deletions
| ... | @@ -25,6 +25,16 @@ module ActsAsChargeable | ... | @@ -25,6 +25,16 @@ module ActsAsChargeable |
| 25 | 25 | ||
| 26 | def charge_with_conekta(options) | 26 | def charge_with_conekta(options) |
| 27 | charge = conekta_charge(options) | 27 | charge = conekta_charge(options) |
| 28 | + save_charge_info(charge, options) | ||
| 29 | + charge | ||
| 30 | + rescue Conekta::ParameterValidationError, Conekta::ProcessingError, | ||
| 31 | + Conekta::Error => e | ||
| 32 | + update_columns(var_status: 'failed') | ||
| 33 | + { error_message: e.message } | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + def save_charge_info(charge, options) | ||
| 37 | + # TODO: update more info | ||
| 28 | update_columns(var_status: charge.status, var_id: charge.id, | 38 | update_columns(var_status: charge.status, var_id: charge.id, |
| 29 | var_service: 'conekta') | 39 | var_service: 'conekta') |
| 30 | update_conekta_barcode(charge) if options[:conekta_type] == 'oxxo' | 40 | update_conekta_barcode(charge) if options[:conekta_type] == 'oxxo' |
| ... | @@ -32,11 +42,6 @@ module ActsAsChargeable | ... | @@ -32,11 +42,6 @@ module ActsAsChargeable |
| 32 | time = Time.zone.now | 42 | time = Time.zone.now |
| 33 | update_columns(var_payment_at: time) | 43 | update_columns(var_payment_at: time) |
| 34 | end | 44 | end |
| 35 | - charge | ||
| 36 | - rescue Conekta::ParameterValidationError, Conekta::ProcessingError, | ||
| 37 | - Conekta::Error => e | ||
| 38 | - update_columns(var_status: 'failed') | ||
| 39 | - { error_message: e.message } | ||
| 40 | end | 45 | end |
| 41 | 46 | ||
| 42 | def conekta_charge(options) | 47 | def conekta_charge(options) |
| ... | @@ -157,6 +162,7 @@ module ActsAsChargeable | ... | @@ -157,6 +162,7 @@ module ActsAsChargeable |
| 157 | return { error_message: 'Not charged yet' } unless var_service | 162 | return { error_message: 'Not charged yet' } unless var_service |
| 158 | return { error_message: 'Not charged with oxxo' } unless var_barcode | 163 | return { error_message: 'Not charged with oxxo' } unless var_barcode |
| 159 | charge = find_charge | 164 | charge = find_charge |
| 165 | + # TODO: Revisar redundancia aquí | ||
| 160 | if charge.status == 'paid' | 166 | if charge.status == 'paid' |
| 161 | update_columns(var_status: 'paid', | 167 | update_columns(var_status: 'paid', |
| 162 | var_payment_at: Time.zone.at(charge.paid_at)) | 168 | var_payment_at: Time.zone.at(charge.paid_at)) |
| ... | @@ -172,6 +178,8 @@ module ActsAsChargeable | ... | @@ -172,6 +178,8 @@ module ActsAsChargeable |
| 172 | 178 | ||
| 173 | private | 179 | private |
| 174 | 180 | ||
| 181 | + # TODO: Missing var attributes to clean | ||
| 182 | + # such as var_paid_amount, var_fee, var_payment_method | ||
| 175 | def clean_var_variables | 183 | def clean_var_variables |
| 176 | return { object: code } if update(var_status: nil, var_barcode: nil, | 184 | return { object: code } if update(var_status: nil, var_barcode: nil, |
| 177 | var_barcode_url: nil, var_id: nil, | 185 | var_barcode_url: nil, var_id: nil, | ... | ... |
| ... | @@ -22,7 +22,7 @@ module Var | ... | @@ -22,7 +22,7 @@ module Var |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | def process_charge(charge_info) | 24 | def process_charge(charge_info) |
| 25 | - # TODO: raise error if object is nil | 25 | + # TODO: update status and time |
| 26 | object = Var.find_charge charge_info[:id] | 26 | object = Var.find_charge charge_info[:id] |
| 27 | object.update(var_status: charge_info[:status]) | 27 | object.update(var_status: charge_info[:status]) |
| 28 | end | 28 | end | ... | ... |
| ... | @@ -6,14 +6,22 @@ describe ActsAsChargeable do | ... | @@ -6,14 +6,22 @@ describe ActsAsChargeable do |
| 6 | Product.delete_all | 6 | Product.delete_all |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | + let!(:Time) { | ||
| 10 | + zone = Time.zone | ||
| 11 | + now = zone.now | ||
| 12 | + time = class_double(Time) | ||
| 13 | + allow(zone).to receive(:now) { now } | ||
| 14 | + allow(time).to receive(:zone) { zone } | ||
| 15 | + } | ||
| 16 | + | ||
| 9 | context 'as a module' do | 17 | context 'as a module' do |
| 10 | it 'can be implemented in ActiveRecord' do | 18 | it 'can be implemented in ActiveRecord' do |
| 11 | expect(Product).to respond_to(:acts_as_chargeable) | 19 | expect(Product).to respond_to(:acts_as_chargeable) |
| 12 | end | 20 | end |
| 13 | end | 21 | end |
| 14 | 22 | ||
| 15 | - context 'processing charges' do | 23 | + context 'Charges with conekta' do |
| 16 | - it ': oxxo' do | 24 | + it 'An oxxo charge' do |
| 17 | product = Product.create | 25 | product = Product.create |
| 18 | conekta_object_mock = JSON.parse( | 26 | conekta_object_mock = JSON.parse( |
| 19 | ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, | 27 | ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, |
| ... | @@ -21,25 +29,23 @@ describe ActsAsChargeable do | ... | @@ -21,25 +29,23 @@ describe ActsAsChargeable do |
| 21 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) | 29 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) |
| 22 | 30 | ||
| 23 | Var.create_charge(:conekta, product, {"conekta_type": "oxxo"}) | 31 | Var.create_charge(:conekta, product, {"conekta_type": "oxxo"}) |
| 24 | - | ||
| 25 | expect(product.var_id).to eq conekta_object_mock.id | 32 | expect(product.var_id).to eq conekta_object_mock.id |
| 26 | expect(product.var_service).to eq 'conekta' | 33 | expect(product.var_service).to eq 'conekta' |
| 27 | expect(product.var_status).to eq 'pending_payment' | 34 | expect(product.var_status).to eq 'pending_payment' |
| 28 | expect(product.var_paid_amount).to eq conekta_object_mock.amount | 35 | expect(product.var_paid_amount).to eq conekta_object_mock.amount |
| 29 | expect(product.var_fee).to eq conekta_object_mock.fee | 36 | expect(product.var_fee).to eq conekta_object_mock.fee |
| 30 | expect(product.var_payment_method).to eq 'OXXO' | 37 | expect(product.var_payment_method).to eq 'OXXO' |
| 31 | - expect(product.var_payment_at).to eq(Time.strptime conekta_object_mock.paid_at.to_s, '%s') | ||
| 32 | end | 38 | end |
| 33 | 39 | ||
| 34 | - it ': card' do | 40 | + it 'A card charge' do |
| 35 | product = Product.create | 41 | product = Product.create |
| 36 | conekta_object_mock = JSON.parse( | 42 | conekta_object_mock = JSON.parse( |
| 37 | ConektaChargeCreateObject::CARD_CREATE_CHARGE_OBJECT, | 43 | ConektaChargeCreateObject::CARD_CREATE_CHARGE_OBJECT, |
| 38 | object_class: OpenStruct) | 44 | object_class: OpenStruct) |
| 39 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) | 45 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) |
| 40 | 46 | ||
| 41 | - Var.create_charge(:conekta, product, { conekta_type: 'card' }) | 47 | + Var.create_charge(:conekta, product, { conekta_type: 'card', card_token: 'token' }) |
| 42 | - | 48 | + product.reload |
| 43 | expect(product.var_id).to eq conekta_object_mock.id | 49 | expect(product.var_id).to eq conekta_object_mock.id |
| 44 | expect(product.var_service).to eq 'conekta' | 50 | expect(product.var_service).to eq 'conekta' |
| 45 | expect(product.var_status).to eq 'paid' | 51 | expect(product.var_status).to eq 'paid' |
| ... | @@ -52,6 +58,198 @@ describe ActsAsChargeable do | ... | @@ -52,6 +58,198 @@ describe ActsAsChargeable do |
| 52 | xit ': spei' do | 58 | xit ': spei' do |
| 53 | end | 59 | end |
| 54 | end | 60 | end |
| 61 | + | ||
| 62 | + context 'instance methods' do | ||
| 63 | + it 'returns error message if does not support specified service' do | ||
| 64 | + product = instance_double('Product') | ||
| 65 | + expect do | ||
| 66 | + product.charge_with("paypal", { conekta_type: 'card' }) | ||
| 67 | + end.to raise_error "#{self.class} doesn't support" \ | ||
| 68 | + " charges with #{service}" | ||
| 69 | + end | ||
| 70 | + | ||
| 71 | + it 'Chooses proper method to attend specified service payment' do | ||
| 72 | + product = instance_double('Product') | ||
| 73 | + options = { conekta_type: 'card' } | ||
| 74 | + expect do | ||
| 75 | + product.charge_with("conekta", options) | ||
| 76 | + end.to send("charge_with_#{service}", options) | ||
| 77 | + end | ||
| 78 | + | ||
| 79 | + it 'Raises Conekta authentication error' do | ||
| 80 | + product = instance_double('Product') | ||
| 81 | + expect do | ||
| 82 | + charge = product.conekta_charge({}) | ||
| 83 | + end.to raise_error Conekta::AuthenticationError | ||
| 84 | + end | ||
| 85 | + | ||
| 86 | + it 'Raises Conekta parameter validation error' do | ||
| 87 | + product = instance_double('Product') | ||
| 88 | + allow(product).to receive(:conekta_charge).and_raise(Conekta::ParameterValidationError) | ||
| 89 | + expect do | ||
| 90 | + charge = product.conekta_charge({}) | ||
| 91 | + end.to raise_error Conekta::ParameterValidationError | ||
| 92 | + end | ||
| 93 | + | ||
| 94 | + it 'Raises Conekta parameter processing error' do | ||
| 95 | + product = instance_double('Product') | ||
| 96 | + allow(product).to receive(:conekta_charge).and_raise(Conekta::ProcessingError) | ||
| 97 | + expect do | ||
| 98 | + charge = product.conekta_charge({}) | ||
| 99 | + end.to raise_error Conekta::ProcessingError | ||
| 100 | + end | ||
| 101 | + | ||
| 102 | + it 'Raises Conekta error' do | ||
| 103 | + product = instance_double('Product') | ||
| 104 | + allow(product).to receive(:conekta_charge).and_raise(Conekta::Error) | ||
| 105 | + expect do | ||
| 106 | + charge = product.conekta_charge({}) | ||
| 107 | + end.to raise_error Conekta::Error | ||
| 108 | + end | ||
| 109 | + | ||
| 110 | + it 'Returns card token' do | ||
| 111 | + product = instance_double('Product') | ||
| 112 | + hash = product.conekta_type_of_charge( | ||
| 113 | + {conekta_type: 'card', card_token: 'token'}) | ||
| 114 | + returned_hash = {card: 'token'} | ||
| 115 | + expect(hash).to eq returned_hash | ||
| 116 | + end | ||
| 117 | + | ||
| 118 | + it 'Returns card token' do | ||
| 119 | + product = Product.new | ||
| 120 | + hash = product.conekta_type_of_charge({conekta_type: 'oxxo'}) | ||
| 121 | + returned_hash = { cash: { type: 'oxxo', expires_at: (Time.zone.now + 3.days).strftime('%Y-%m-%d') } } | ||
| 122 | + expect(hash).to eq returned_hash | ||
| 123 | + end | ||
| 124 | + | ||
| 125 | + it 'Updates conekta barcode' do | ||
| 126 | + product = instance_double('Product') | ||
| 127 | + conekta_object_mock = JSON.parse( | ||
| 128 | + ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, | ||
| 129 | + object_class: OpenStruct) | ||
| 130 | + previous_payment_expiration = product.var_payment_expires_at | ||
| 131 | + | ||
| 132 | + product.update_conekta_barcode conekta_object_mock | ||
| 133 | + expect(product.var_barcode).to eq conekta_object_mock.payment_method.barcode | ||
| 134 | + expect(product.var_barcode_url).to eq conekta_object_mock.payment_method.barcode_url | ||
| 135 | + expect(product.var_payment_expires_at).to_not eq previous_payment_expiration | ||
| 136 | + end | ||
| 137 | + | ||
| 138 | + it 'Makes manual charge' do | ||
| 139 | + product = instance_double('Product') | ||
| 140 | + product.manual_charge | ||
| 141 | + previous_time = product.var_payment_at | ||
| 142 | + | ||
| 143 | + expect(product.var_service).to eq 'manual' | ||
| 144 | + expect(product.status).to eq 'paid' | ||
| 145 | + expect(product.var_payment_at).to_not eq previous_time | ||
| 146 | + end | ||
| 147 | + | ||
| 148 | + it 'Makes manual discharge' do | ||
| 149 | + product = instance_double('Product') | ||
| 150 | + product.manual_charge | ||
| 151 | + | ||
| 152 | + expect(product.var_service).to eq 'manual' | ||
| 153 | + expect(product.status).to eq 'pending' | ||
| 154 | + expect(product.var_payment_at).to be_nil | ||
| 155 | + end | ||
| 156 | + | ||
| 157 | + it 'Return not charged yet error' do | ||
| 158 | + product = Product.create | ||
| 159 | + msg = product.find_charge | ||
| 160 | + error = { error_message: 'Not charged yet' } | ||
| 161 | + expect(msg).to eq error | ||
| 162 | + end | ||
| 163 | + | ||
| 164 | + it 'Returns Conekta Object' do | ||
| 165 | + product = Product.create | ||
| 166 | + conekta_object_mock = JSON.parse( | ||
| 167 | + ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, | ||
| 168 | + object_class: OpenStruct) | ||
| 169 | + allow(product).to receive(:find_conekta_charge).and_return conekta_object_mock | ||
| 170 | + expect(product.find_conekta_charge).to eq conekta_object_mock | ||
| 171 | + end | ||
| 172 | + | ||
| 173 | + it 'Returns true when object is charged' do | ||
| 174 | + product = Product.create | ||
| 175 | + conekta_object_mock = JSON.parse( | ||
| 176 | + ConektaChargeCreateObject::CARD_CREATE_CHARGE_OBJECT, | ||
| 177 | + object_class: OpenStruct) | ||
| 178 | + allow(product).to receive(:find_charge).and_return(conekta_object_mock) | ||
| 179 | + | ||
| 180 | + expect(product.charged?).to be true | ||
| 181 | + end | ||
| 182 | + | ||
| 183 | + it 'Returns false when object has not been charged' do | ||
| 184 | + product = Product.create | ||
| 185 | + expect(product.charged?).to be false | ||
| 186 | + end | ||
| 187 | + | ||
| 188 | + xit 'Test instance support' do | ||
| 189 | + end | ||
| 190 | + | ||
| 191 | + xit 'Test sync' do | ||
| 192 | + end | ||
| 193 | + | ||
| 194 | + xit 'Test conekta_attributes' do | ||
| 195 | + end | ||
| 196 | + | ||
| 197 | + it 'Returns true if object is payed' do | ||
| 198 | + product = Product.new | ||
| 199 | + product.status = 'paid' | ||
| 200 | + expect(product.payed?).to be true | ||
| 201 | + end | ||
| 202 | + | ||
| 203 | + it 'Returns true if object is payed' do | ||
| 204 | + product = Product.new | ||
| 205 | + expect(product.payed?).to be false | ||
| 206 | + end | ||
| 207 | + | ||
| 208 | + it 'Returns already paid error when cancelling oxxo payment' do | ||
| 209 | + product = Product.new | ||
| 210 | + product.status = 'paid' | ||
| 211 | + error_msg = { error_message: 'Already paid' } | ||
| 212 | + | ||
| 213 | + expect(product.cancel_oxxo_payment).to eq error_msg | ||
| 214 | + end | ||
| 215 | + | ||
| 216 | + it 'Returns object not charged error when cancelling oxxo payment' do | ||
| 217 | + product = Product.new | ||
| 218 | + error_msg = { error_message: 'Not charged yet' } | ||
| 219 | + | ||
| 220 | + expect(product.cancel_oxxo_payment).to eq error_msg | ||
| 221 | + end | ||
| 222 | + | ||
| 223 | + it 'Returns not charged with oxxo error when cancelling oxxo payment' do | ||
| 224 | + product = Product.new | ||
| 225 | + product.var_service = 'conekta' | ||
| 226 | + error_msg = { error_message: 'Not charged with oxxo' } | ||
| 227 | + expect(product.cancel_oxxo_payment).to eq error_msg | ||
| 228 | + end | ||
| 229 | + | ||
| 230 | + it 'Nils every object attribute' do | ||
| 231 | + product = Product.create | ||
| 232 | + conekta_object_mock = JSON.parse( | ||
| 233 | + ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, | ||
| 234 | + object_class: OpenStruct) | ||
| 235 | + allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) | ||
| 236 | + | ||
| 237 | + Var.create_charge(:conekta, product, {"conekta_type": "oxxo"}) | ||
| 238 | + product.clean_var_variables | ||
| 239 | + | ||
| 240 | + # TODO: Check unused attribute in migration "var_payment::Datetime" | ||
| 241 | + expect(product.var_id).to be_nil | ||
| 242 | + expect(product.var_service).to be_nil | ||
| 243 | + expect(product.var_status).to be_nil | ||
| 244 | + expect(product.var_paid_amount).to be_nil | ||
| 245 | + expect(product.var_fee).to be_nil | ||
| 246 | + expect(product.var_payment_method).to be_nil | ||
| 247 | + expect(product.var_barcode).to be_nil | ||
| 248 | + expect(product.var_barcode_url).to be_nil | ||
| 249 | + expect(product.var_payment_at).to be_nil | ||
| 250 | + expect(product.var_payment_expires_at).to be_nil | ||
| 251 | + end | ||
| 252 | + end | ||
| 55 | end | 253 | end |
| 56 | 254 | ||
| 57 | # Tests for mock on Chargeable class | 255 | # Tests for mock on Chargeable class | ... | ... |
| ... | @@ -12,6 +12,7 @@ describe Var::ConektaOxxoHandler do | ... | @@ -12,6 +12,7 @@ describe Var::ConektaOxxoHandler do |
| 12 | ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, | 12 | ConektaChargeCreateObject::OXXO_CREATE_CHARGE_OBJECT, |
| 13 | object_class: OpenStruct) | 13 | object_class: OpenStruct) |
| 14 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) | 14 | allow(product).to receive(:conekta_charge).and_return(conekta_object_mock) |
| 15 | + | ||
| 15 | Var.create_charge(:conekta, product, {"conekta_type": "oxxo"}) | 16 | Var.create_charge(:conekta, product, {"conekta_type": "oxxo"}) |
| 16 | 17 | ||
| 17 | Var::ConektaOxxoHandler.analyze(WebHookPostRequest::OXXO_CHARGE_CREATED) | 18 | Var::ConektaOxxoHandler.analyze(WebHookPostRequest::OXXO_CHARGE_CREATED) | ... | ... |
-
Please register or login to post a comment