Class: AstroPay::Direct

Inherits:
Model
  • Object
show all
Defined in:
lib/astro_pay/direct.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::Direct

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

Parameters:

  • attributes (Hash)

    with the following fields: :invoice, :amount, :iduser, :bank, :country, :currency, :description, :cpf, :sub_code, :return_url, :confirmation_url.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/astro_pay/direct.rb', line 21

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

  @x_login =  config.
  @x_trans_key = config.direct_x_trans_key
  @x_login_for_webpaystatus = config.
  @x_trans_key_for_webpaystatus = config.direct_x_trans_key_for_webpaystatus
  @secret_key = config.direct_secret_key
  @sandbox = config.sandbox
  @response_type = 'json'

  super

  subdomain = 'sandbox.' if @sandbox

  @astro_urls = {
    "create" => "https://#{subdomain}astropaycard.com/api_curl/apd/create",
    "status" => "https://#{subdomain}astropaycard.com/apd/webpaystatus",
    "exchange" => "https://#{subdomain}astropaycard.com/apd/webcurrencyexchange",
    "banks" => "https://#{subdomain}astropaycard.com/api_curl/apd/get_banks_by_country"
  }
end

Instance Attribute Details

#amountObject

Input params



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

def amount
  @amount
end

#bankObject

Input params



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

def bank
  @bank
end

#confirmation_urlObject

Returns the value of attribute confirmation_url.



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

def confirmation_url
  @confirmation_url
end

#countryObject

Input params



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

def country
  @country
end

#cpfObject

Returns the value of attribute cpf.



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

def cpf
  @cpf
end

#currencyObject

Input params



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

def currency
  @currency
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#iduserObject

Input params



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

def iduser
  @iduser
end

#invoiceObject

Input params



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

def invoice
  @invoice
end

#response_typeObject

Returns the value of attribute response_type.



13
14
15
# File 'lib/astro_pay/direct.rb', line 13

def response_type
  @response_type
end

#return_urlObject

Returns the value of attribute return_url.



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

def return_url
  @return_url
end

#sub_codeObject

Returns the value of attribute sub_code.



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

def sub_code
  @sub_code
end

Instance Method Details

#astro_curl(url, params_hash) ⇒ 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.



132
133
134
# File 'lib/astro_pay/direct.rb', line 132

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

#createHash

Creates a new transaction.

Returns:

  • (Hash)

    of the response that includes the URL to where an user should be redirected to validate and complete the process. If there is an error, the [String] response is returned.



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
# File 'lib/astro_pay/direct.rb', line 49

def create
  params_hash = {
    'x_login' => @x_login,
    'x_trans_key' => @x_trans_key,
    'x_invoice' => invoice,
    'x_amount' => amount,
    'x_iduser' => iduser,
    'x_bank' =>   bank,
    'x_country' => country,
    'x_sub_code' => sub_code,
    'type' => response_type
  }

  message_to_control = "#{invoice}D#{amount}P#{iduser}A"

  sha256 = OpenSSL::Digest::SHA256.new
  control = OpenSSL::HMAC.hexdigest(sha256, [@secret_key].pack('A*'), [message_to_control].pack('A*'))
  control = control.upcase

  params_hash['control'] = control
  params_hash['x_currency'] = currency if currency
  params_hash['x_description'] = description if description
  params_hash['x_cpf'] = cpf if cpf
  params_hash['x_return'] = return_url if return_url
  params_hash['x_confirmation'] = confirmation_url if confirmation_url

  astro_curl(@astro_urls['create'], params_hash)
end

#get_banks_by_countryHash

Requests a list of valid banks by country.

Returns:

  • (Hash)

    of the response that includes the list of banks. If there is an error, the [String] response is returned.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/astro_pay/direct.rb', line 82

def get_banks_by_country
  params_hash = {
    # Mandatory
    'x_login' => @x_login,
    'x_trans_key' => @x_trans_key,
    'country_code' => country,
    'type' => response_type
  }

  astro_curl(@astro_urls['banks'], params_hash)
end

#get_exchangeHash

Requests the exchange rate from USD to the currency of a target country.

Returns:

  • (Hash)

    of the response that includes the exchange rate. If there is an error, the [String] response is returned.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/astro_pay/direct.rb', line 114

def get_exchange
  params_hash = {
    # Mandatory
    'x_login' => @x_login_for_webpaystatus,
    'x_trans_key' => @x_trans_key_for_webpaystatus,
    'x_country' => country,
    'x_amount' => amount
  }

  astro_curl(@astro_urls['exchange'], params_hash)
end

#get_invoice_statusHash

Requests the status of a transaction.

Returns:

  • (Hash)

    of the response that includes the transaction status. If there is an error, the [String] response is returned.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/astro_pay/direct.rb', line 98

def get_invoice_status
  params_hash = {
    # Mandatory
    'x_login' => @x_login_for_webpaystatus,
    'x_trans_key' => @x_trans_key_for_webpaystatus,
    'x_invoice' => invoice,
    'x_response_format' => response_type
  }

  astro_curl(@astro_urls['status'], params_hash)
end