Class: IpagosRubySdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ipagos_ruby_sdk/client.rb

Constant Summary collapse

SANDBOX_URL =
"https://apitest.cybersource.com"
PRODUCTION_URL =
"https://api.cybersource.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
# File 'lib/ipagos_ruby_sdk/client.rb', line 17

def initialize(config)
  @config = config
  @base_url = config.environment == :production ? PRODUCTION_URL : SANDBOX_URL
  
  @conn = Faraday.new(url: @base_url) do |f|
    f.headers["Content-Type"] = "application/json"
    f.headers["v-c-merchant-id"] = config.merchant_id
    f.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/ipagos_ruby_sdk/client.rb', line 15

def config
  @config
end

Instance Method Details

#create_payment(amount:, bill_to:, token: nil, card_data: nil, reference: nil, line_items: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ipagos_ruby_sdk/client.rb', line 39

def create_payment(amount:, bill_to:, token: nil, card_data: nil, reference: nil, line_items: nil)
  path = "/pts/v2/payments"
  
  items = if line_items.nil? || line_items.empty?
            [{
              unitPrice: amount.total,
              quantity: 1,
              productName: "Servicio General",
              productSku: "GEN-001"
            }]
          else
            line_items.map(&:to_h)
          end

  payload = {
    clientReferenceInformation: { code: reference || "ORD-#{Time.now.to_i}" },
    processingInformation: { capture: true },
    orderInformation: {
      amountDetails: amount.to_h,
      billTo: bill_to.to_h,
      lineItems: items
    }
  }

  if token
    payload[:tokenInformation] = { transientTokenJwt: token }
  elsif card_data
    payload[:paymentInformation] = {
      card: {
        number: card_data[:number],
        expirationMonth: card_data[:expiration_month],
        expirationYear: card_data[:expiration_year],
        cvv: card_data[:cvv]
      }
    }
  else
    raise ArgumentError, "Se requiere un 'token' o 'card_data' para procesar el pago"
  end

  send_request(:post, path, payload)
end

#create_session(origin) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ipagos_ruby_sdk/client.rb', line 28

def create_session(origin)
  path = "/microform/v2/sessions"
  payload = {
    targetOrigins: [origin.chomp("/")],
    allowedCardNetworks: ["VISA", "MASTERCARD", "AMEX"],
    allowedPaymentTypes: ["CARD"],
    clientVersion: "v2"
  }
  send_request(:post, path, payload)
end

#get_payment(id) ⇒ Object



103
104
105
# File 'lib/ipagos_ruby_sdk/client.rb', line 103

def get_payment(id)
  send_request(:get, "/pts/v2/payments/#{id}")
end

#get_payment_status(id) ⇒ Object



107
108
109
# File 'lib/ipagos_ruby_sdk/client.rb', line 107

def get_payment_status(id)
  get_payment(id)
end

#refund_payment(id, amount) ⇒ Object

Reembolsar un pago (Refund) - Versión Corregida para CyberSource



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ipagos_ruby_sdk/client.rb', line 88

def refund_payment(id, amount)
  path = "/pts/v2/payments/#{id}/refunds"
  
  payload = {
    clientReferenceInformation: { code: "refund_#{Time.now.to_i}" },
    orderInformation: {
      amountDetails: {
        totalAmount: amount.total,
        currency: amount.currency
      }
    }
  }
  send_request(:post, path, payload)
end

#void_payment(id) ⇒ Object



81
82
83
84
85
# File 'lib/ipagos_ruby_sdk/client.rb', line 81

def void_payment(id)
  path = "/pts/v2/payments/#{id}/voids"
  payload = { clientReferenceInformation: { code: "void_#{Time.now.to_i}" } }
  send_request(:post, path, payload)
end