Class: Paymob::AcceptAPI

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/paymob/accept_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, billing_data:, payment_reference:, payment_type: :onetime) ⇒ AcceptAPI

Returns a new instance of AcceptAPI.



12
13
14
15
16
17
18
19
# File 'lib/paymob/accept_api.rb', line 12

def initialize(amount:, billing_data:, payment_reference:, payment_type: :onetime)
  @amount = amount
  @billing_data = billing_data
  @payment_reference = payment_reference
  @payment_type = payment_type
  self.class.base_uri Paymob.base_url
  self.class.headers 'Content-Type' => 'application/json'
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def amount
  @amount
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def auth_token
  @auth_token
end

#billing_dataObject (readonly)

Returns the value of attribute billing_data.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def billing_data
  @billing_data
end

#merchant_idObject (readonly)

Returns the value of attribute merchant_id.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def merchant_id
  @merchant_id
end

#order_idObject (readonly)

Returns the value of attribute order_id.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def order_id
  @order_id
end

#payment_keyObject (readonly)

Returns the value of attribute payment_key.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def payment_key
  @payment_key
end

#payment_referenceObject (readonly)

Returns the value of attribute payment_reference.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def payment_reference
  @payment_reference
end

#payment_typeObject (readonly)

Returns the value of attribute payment_type.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def payment_type
  @payment_type
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def user
  @user
end

#wallet_redirect_urlObject (readonly)

Returns the value of attribute wallet_redirect_url.



9
10
11
# File 'lib/paymob/accept_api.rb', line 9

def wallet_redirect_url
  @wallet_redirect_url
end

Instance Method Details

#authenticateObject

def self.new_payment(amount:, payment_reference:, payment_type:, billing_data: {})

api = new(amount: amount, billing_data: billing_data, payment_reference: payment_reference,
          payment_type: payment_type)
api.authenticate
api.create_order
api.request_payment_key
api

end



30
31
32
33
34
# File 'lib/paymob/accept_api.rb', line 30

def authenticate
  response = self.class.post('/auth/tokens', { body: { api_key: api_key }.to_json }).parsed_response
  @auth_token = response['token']
  @merchant_id = response['profile']['id']
end

#create_orderObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paymob/accept_api.rb', line 36

def create_order
  response = self.class.post('/ecommerce/orders',
                             body: {
                               auth_token: auth_token,
                               merchant_id: merchant_id,
                               delivery_needed: false,
                               currency: 'EGP',
                               amount_cents: amount.to_i * 100,
                               merchant_order_id: payment_reference
                             }.to_json)
  body = response.parsed_response

  @order_id = body['id']
  raise Errors::PaymobRequestError, body if @order_id.blank?
end

#request_payment_key(integration_id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/paymob/accept_api.rb', line 52

def request_payment_key(integration_id)
  response = self.class.post('/acceptance/payment_keys', body: {
    auth_token: auth_token,
    amount_cents: amount.to_i * 100,
    order_id: order_id,
    currency: 'EGP',
    integration_id: integration_id,
    billing_data: {
      email: billing_data[:email],
      first_name: billing_data[:first_name],
      last_name: billing_data[:last_name],
      building: billing_data[:building] || 'NA',
      apartment: billing_data[:apartment] || 'NA',
      street: billing_data[:street] || 'NA',
      floor: billing_data[:floor] || 'NA',
      city: billing_data[:city] || 'NA',
      country: billing_data[:country] || 'NA',
      phone_number: billing_data[:phone_number]
    }
  }.to_json).parsed_response

  @payment_key = response['token']
end

#wallet_payment(phone_number) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/paymob/accept_api.rb', line 76

def wallet_payment(phone_number)
  response = self.class.post('/acceptance/payments/pay',
                             body: {
                               payment_token: payment_key,
                               source: {
                                 identifier: phone_number,
                                 subtype: 'WALLET'
                               }
                             }.to_json).parsed_response

  @wallet_redirect_url = response['redirect_url']
end