lib/pagarme/transaction.rb in pagarme-0.14 vs lib/pagarme/transaction.rb in pagarme-0.15

- old
+ new

@@ -1,14 +1,15 @@ +# encoding: utf-8 require 'openssl' require 'base64' require File.join(File.dirname(__FILE__), '..', 'pagarme') require File.join(File.dirname(__FILE__), '.', 'utils') require File.join(File.dirname(__FILE__), '.', 'errors') module PagarMe class Transaction - attr_accessor :amount, :card_number, :card_holder_name, :card_expiracy_month, :card_expiracy_year, :card_cvv, :live, :card_hash, :installments + attr_accessor :amount, :card_number, :card_holder_name, :card_expiracy_month, :card_expiracy_year, :card_cvv, :live, :card_hash, :installments, :card_last_digits # initializers def initialize(first_parameter = nil, server_response = nil) @statuses_codes = { :local => 0, :approved => 1, :processing => 2, :refused => 3, :chargebacked => 4 } @@ -113,25 +114,38 @@ @date_created = response['date_created'] self.amount = response['amount'] self.live = response['live'] self.card_holder_name = response['costumer_name'] self.installments = (!response['installments'] ? 1 : response['installments'].to_i) + self.card_last_digits = response['card_last_digits'] @id = response['id'] end + def is_valid_credit_card(card) + s1 = s2 = 0 + card.to_s.reverse.chars.each_slice(2) do |odd, even| + s1 += odd.to_i + + double = even.to_i * 2 + double -= 9 if double >= 10 + s2 += double + end + (s1 + s2) % 10 == 0 + end + def error_in_transaction - if self.card_number.length < 16 || self.card_number.length > 20 - "Invalid card number." + if self.card_number.length < 16 || self.card_number.length > 20 || !is_valid_credit_card(self.card_number) + "Número do cartão inválido." elsif self.card_holder_name.length == 0 - "Invalid card holder name." + "Nome do portador inválido." elsif self.card_expiracy_month.to_i <= 0 || self.card_expiracy_month.to_i > 12 - "Invalid expiracy date month." + "Mês de expiração inválido." elsif self.card_expiracy_year.to_i <= 0 - "Invalid expiracy date year." + "Ano de expiração inválido." elsif self.card_cvv.length < 3 || self.card_cvv.length > 4 - "Invalid card security code." + "Código de segurança inválido." elsif self.amount.to_i <= 0 - "Invalid amount." + "Valor inválido." else nil end end