Created
May 22, 2013 09:52
-
-
Save Bodacious/5626474 to your computer and use it in GitHub Desktop.
PesaPal Implementation in Ruby (test)
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 | |
| require 'active_support' | |
| include ActiveSupport::Configurable | |
| self.configure do |config| | |
| config.test_mode = true | |
| config.consumer_key = "<your key>" | |
| config.consumer_secret = "<your secret>" | |
| 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] ? 'http://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: HTTP_METHOD, | |
| signature_method: 'HMAC-SHA1', | |
| oauth_version: '1.0' | |
| } | |
| ) | |
| 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 | |
| XML = %{<?xml version="1.0" encoding="utf-8"?><PesapalDirectOrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Amount="1000" Description="DateMe Kenya Subscription" Type="MERCHANT" Reference="asdjfaosidjfoa" FirstName="" LastName="" Email="[email protected]" xmlns="http://www.pesapal.com" />} | |
| puts url = TransactionUrl.new(XML, "http://localhost:3000").url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For all who end up here looking for a solution ... I've started on a rubygem that works. Find it on RubyGems.org (here) and on GitHub (here).