Class: AstroPay::Card

Inherits:
Model
  • Object
show all
Defined in:
lib/astro_pay/card.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#error, #message

Instance Method Summary collapse

Methods inherited from Model

#attributes, #attributes=

Constructor Details

#initialize(args = {}) ⇒ AstroPay::Card

Creates a new instance of [AstroPay::Card].

Parameters:

  • attributes (Hash)

    with the following fields: :approval_code, :number, :ccv, :exp_date, :amount, :unique_id :invoice_num, :transaction_id, :additional_params, :type.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/astro_pay/card.rb', line 20

def initialize(args = {})
  config = AstroPay.configuration

  @x_login = config.
  @x_trans_key = config.card_x_trans_key
  @sandbox = config.sandbox

  base_url = "https://#{'sandbox-' if @sandbox}api.astropaycard.com/"

  # AstroPay API version (default "2.0")
  @x_version = "2.0"
  # Field delimiter (default "|")
  @x_delim_char = "|"
  # Change to N for production
  @x_test_request = 'N'
  # Time window of a transaction with the sames values is taken as
  # duplicated (default 120)
  @x_duplicate_window = 30
  @x_method = "CC"
  # Response format:
  # "string", "json", "xml" (default: string; recommended: json)
  @x_response_format = "json"

  @additional_params = Hash.new

  super

  @validator_url = "#{base_url}verif/validator"
  @transtatus_url = "#{base_url}verif/transtatus"
end

Instance Attribute Details

#additional_paramsObject

Returns the value of attribute additional_params.



12
13
14
# File 'lib/astro_pay/card.rb', line 12

def additional_params
  @additional_params
end

#amountObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def amount
  @amount
end

#approval_codeObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def approval_code
  @approval_code
end

#ccvObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def ccv
  @ccv
end

#exp_dateObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def exp_date
  @exp_date
end

#invoice_numObject

Returns the value of attribute invoice_num.



12
13
14
# File 'lib/astro_pay/card.rb', line 12

def invoice_num
  @invoice_num
end

#numberObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def number
  @number
end

#transaction_idObject

Returns the value of attribute transaction_id.



12
13
14
# File 'lib/astro_pay/card.rb', line 12

def transaction_id
  @transaction_id
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/astro_pay/card.rb', line 12

def type
  @type
end

#unique_idObject

Input params



11
12
13
# File 'lib/astro_pay/card.rb', line 11

def unique_id
  @unique_id
end

Instance Method Details

#astro_curl(url, params) ⇒ Hash

Makes a request to the AstroPay API.

Parameters:

  • url (String)

    endpoint for the AstroPay API.

  • params (Hash)

    data and options for the request.

Returns:

  • (Hash)

    of the successful response or [String] of the response if an error rises.



163
164
165
# File 'lib/astro_pay/card.rb', line 163

def astro_curl(url, params)
  AstroPay::Curl.post(url, params)
end

#auth_capture_transactionHash

Note:

(See #auth_transaction) to known the data sent on the request.

Requests AstroPay to AUTHORIZE and CAPTURE a transaction at the same time (if it is possible).

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



97
98
99
100
101
102
103
104
105
# File 'lib/astro_pay/card.rb', line 97

def auth_capture_transaction
  data = full_params.merge(
    'x_unique_id' => unique_id,
    'x_invoice_num' => invoice_num,
    'x_type' => "AUTH_CAPTURE"
  )

  astro_curl(@validator_url, data)
end

#auth_transactionHash

Note:

This method sends in the request the following data: ‘number’, AstroPay Card number (16 digits); ‘ccv’, AstroPay Card security code (CVV); ‘exp_date’, AstroPay Card expiration date; ‘amount’, Amount of the transaction; ‘unique_id’, Unique user ID of the merchant; ‘invoice_num’, Merchant transaction identifier, i.e. the order number; ‘additional_params’, Array of additional info that you would send to AstroPay for reference purpose.

Requests AstroPay to AUTHORIZE a transaction.

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



65
66
67
68
69
70
71
72
73
# File 'lib/astro_pay/card.rb', line 65

def auth_transaction
  data = full_params.merge(
    'x_unique_id' => unique_id,
    'x_invoice_num' => invoice_num,
    'x_type' => "AUTH_ONLY"
  )

  astro_curl(@validator_url, data)
end

#calculate_control(transaction_id, amount) ⇒ String

Generates an hexadecimal code intended to be used in the checksum of the messages received.

Parameters:

  • transaction_id (String)

    merchant’s id for the transaction.

  • amount (Float)

    of the transaction.

Returns:

  • (String)

    of 64 uppercase characters.



173
174
175
# File 'lib/astro_pay/card.rb', line 173

def calculate_control(transaction_id, amount)
  Digest::MD5.hexdigest("#{@x_login}#{transaction_id}#{amount}")
end

#capture_transactionHash

Note:

(See #auth_transaction) to known the data sent on the request.

Requests AstroPay to CAPTURE the previous authorized transaction.

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



80
81
82
83
84
85
86
87
88
89
# File 'lib/astro_pay/card.rb', line 80

def capture_transaction
  data = full_params.merge(
    'x_unique_id' => unique_id,
    'x_invoice_num' => invoice_num,
    'x_auth_code' => approval_code,
    'x_type' => "CAPTURE_ONLY"
  )

  astro_curl(@validator_url, data)
end

#check_transaction_statusHash

Note:

This request includes the basic credentials data and the following fields: ‘invoice_num’, The merchant id sent in the transaction; ‘type’, 0 for basic info, 1 for detailed info.

Requests AstroPay the status of a transaction.

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



147
148
149
150
151
152
153
154
155
# File 'lib/astro_pay/card.rb', line 147

def check_transaction_status
  data = basic_credentials.merge(
    'x_trans_key' => @x_trans_key,
    'x_invoice_num' => invoice_num,
    'x_type' => (type || 0)
  )

  astro_curl(@transtatus_url, data)
end

#refund_transactionHash

Note:

This request includes the transaction_id merchant invoice number sent in previous call of capture_transaction or auth_transaction. (See #auth_transaction) to known the data sent on the request.

Requests AstroPay to REFUND a transaction.

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



114
115
116
117
118
119
120
121
# File 'lib/astro_pay/card.rb', line 114

def refund_transaction
  data = full_params.merge(
    'x_trans_id' => transaction_id,
    'x_type' => "REFUND"
  )

  astro_curl(@validator_url, data)
end

#void_transactionHash

Note:

This request includes the transaction_id merchant invoice number sent in previous call of capture_transaction or auth_transaction. (See #auth_transaction) to known the data sent on the request.

Requests AstroPay to VOID a transaction.

Returns:

  • (Hash)

    response by AstroPay capture API. Please see section 3.1.3 “Response” of AstroPay Card integration manual for more info.



130
131
132
133
134
135
136
137
# File 'lib/astro_pay/card.rb', line 130

def void_transaction
  data = full_params.merge(
    'x_trans_id' => transaction_id,
    'x_type' => "VOID"
  )

  astro_curl(@validator_url, data)
end