Class: Pesapal::Merchant

Inherits:
Object
  • Object
show all
Defined in:
lib/pesapal/merchant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :development, path_to_file = nil) ⇒ Merchant

constructor



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pesapal/merchant.rb', line 44

def initialize(mode = :development, path_to_file = nil)

    # initialize
    @params = nil
    @post_xml = nil
    @token_secret = nil

    set_mode mode

    # set the credentials if we have not set a custom path for the YAML config file
    if path_to_file.nil?
        # no path to file so no YAML override so we load from initializer
        set_configuration PesapalRails::Application.config.yaml[@mode]
    else
        # we have custom path so we load from file
        set_configuration_from_yaml path_to_file
    end

end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/pesapal/merchant.rb', line 5

def config
  @config
end

#order_detailsObject

Returns the value of attribute order_details.



5
6
7
# File 'lib/pesapal/merchant.rb', line 5

def order_details
  @order_details
end

Instance Method Details

#generate_order_urlObject

generate pesapal order url (often iframed)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pesapal/merchant.rb', line 65

def generate_order_url

    # build xml with input data, the format is standard so no editing is
    # required
    @post_xml = Pesapal::Post::generate_post_xml @order_details

    # initialize setting of @params (oauth_signature left empty)
    @params = Pesapal::Post::set_parameters(@config[:callback_url], @config[:consumer_key], @post_xml)

    # generate oauth signature and add signature to the request parameters
    @params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:postpesapaldirectorderv4], @params, @config[:consumer_secret], @token_secret)

    # change params (with signature) to a query string
    query_string = Pesapal::Oauth::generate_encoded_params_query_string @params

    "#{@api_endpoints[:postpesapaldirectorderv4]}?#{query_string}"
end

#ipn_listener(notification_type, merchant_reference, transaction_tracking_id) ⇒ Object

listen to ipn response



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pesapal/merchant.rb', line 137

def ipn_listener(notification_type, merchant_reference, transaction_tracking_id)
    
    status = query_payment_status(merchant_reference, transaction_tracking_id)

    output = { :status => status }

    if status == "COMPLETED"
        output[:response] = "pesapal_notification_type=CHANGE&pesapal_transaction_tracking_id=#{transaction_tracking_id}&pesapal_merchant_reference=#{merchant_reference}"
    elsif status == "FAILED"
        output[:response] = "pesapal_notification_type=CHANGE&pesapal_transaction_tracking_id=#{transaction_tracking_id}&pesapal_merchant_reference=#{merchant_reference}"
    else
        output[:response] = ""
    end

    output
end

#query_payment_details(merchant_reference, transaction_tracking_id) ⇒ Object

query the details of the transaction



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pesapal/merchant.rb', line 84

def query_payment_details(merchant_reference, transaction_tracking_id)

    # initialize setting of @params (oauth_signature left empty)
    @params = Pesapal::Details::set_parameters(@config[:consumer_key], merchant_reference, transaction_tracking_id)

    # generate oauth signature and add signature to the request parameters
    @params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:querypaymentdetails], @params, @config[:consumer_secret], @token_secret)

    # change params (with signature) to a query string
    query_string = Pesapal::Oauth::generate_encoded_params_query_string @params

    # get status response
    response = Net::HTTP.get(URI("#{@api_endpoints[:querypaymentdetails]}?#{query_string}"))
    response = CGI::parse(response)
    response = response["pesapal_response_data"][0].split(',')

    details = { :method => response[1],
                :status => response[2],
                :merchant_reference => response[3],
                :transaction_tracking_id => response[0] }
end

#query_payment_status(merchant_reference, transaction_tracking_id = nil) ⇒ Object

query the status of the transaction



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pesapal/merchant.rb', line 107

def query_payment_status(merchant_reference, transaction_tracking_id = nil)
    
    # initialize setting of @params (oauth_signature left empty)
    @params = Pesapal::Status::set_parameters(@config[:consumer_key], merchant_reference, transaction_tracking_id)

    # generate oauth signature and add signature to the request parameters
    @params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:querypaymentstatus], @params, @config[:consumer_secret], @token_secret)

    # change params (with signature) to a query string
    query_string = Pesapal::Oauth::generate_encoded_params_query_string @params

    # get status response
    response = Net::HTTP.get(URI("#{@api_endpoints[:querypaymentstatus]}?#{query_string}"))
    response = CGI::parse(response)

    # return the string result of what we want
    response["pesapal_response_data"][0]
end

#set_mode(mode = :development) ⇒ Object

set mode when called



127
128
129
130
131
132
133
134
# File 'lib/pesapal/merchant.rb', line 127

def set_mode(mode = :development)
    
    # convert symbol to string and downcase
    @mode = "#{mode.to_s.downcase}"

    # set api endpoints depending on the mode
    set_endpoints
end