Last active
December 17, 2015 14:39
-
-
Save Bodacious/5626257 to your computer and use it in GitHub Desktop.
pesa-pal integration in Rails [incomplete]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module PesaPal | |
| include ActiveSupport::Configurable | |
| self.configure do |config| | |
| config.test_mode = !Rails.env.production? | |
| config.consumer_key = Rails.env.production? ? "" : "" | |
| config.consumer_secret = Rails.env.production? ? "" : "" | |
| config.merchant_email = Rails.env.production? ? "" : "[email protected]" | |
| end | |
| end | |
| class TransactionUrl | |
| require 'oauth' | |
| require 'uri' | |
| require 'active_support/core_ext/object/to_query' | |
| attr_reader :post_data_xml, :callback_url, :token, :request_options | |
| HTTP_METHOD = 'get' | |
| API_ACTION = 'API/PostPesapalDirectOrderV4' | |
| def initialize(post_data_xml, callback_url) | |
| @post_data_xml = URI.escape(post_data_xml) | |
| @callback_url = callback_url | |
| @token = nil | |
| @request_options = {} | |
| end | |
| def api_domain | |
| PesaPal.config[:test_mode] ? 'https://demo.pesapal.com' : 'https://www.pesapal.com' | |
| end | |
| def url | |
| "#{signed_request.path}&#{joined_params}" | |
| end | |
| def joined_params | |
| params.to_query | |
| end | |
| def consumer | |
| @consumer ||= begin | |
| OAuth::Consumer.new(PesaPal.config[:consumer_key], PesaPal.config[:consumer_secret], { | |
| site: api_domain, | |
| scheme: :query_string, | |
| http_method: :get, | |
| } | |
| ) | |
| end | |
| end | |
| def signed_request | |
| consumer.create_signed_request(HTTP_METHOD, "#{api_domain}/#{API_ACTION}", token, request_options, params) | |
| end | |
| def params | |
| @params = { | |
| oauth_callback: URI.escape(callback_url), | |
| pesapal_request_data: post_data_xml, | |
| } | |
| end | |
| end | |
| url = TransactionUrl.new("your xml", "callbackURL").url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment