Created
June 12, 2011 17:12
-
-
Save elricstorm/1021771 to your computer and use it in GitHub Desktop.
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
| =begin | |
| The following class below works with Ruby v. 1.9.2 and can be used as a libs class with Ruby on Rails 3.x. | |
| =end | |
| require 'net/https' | |
| require 'uri' | |
| class Zong | |
| def initialize(customer_key = nil) | |
| @xml_data = <<EOF | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <requestMobilePaymentProcessEntrypoints xmlns="http://pay01.zong.com/zongpay" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pay01.zong.com/zongpay/zongpay.xsd"> | |
| <customerKey>#{customer_key}</customerKey> | |
| <countryCode>US</countryCode> | |
| <items currency="USD" /> | |
| </requestMobilePaymentProcessEntrypoints> | |
| EOF | |
| @uri = URI.parse('https://pay01.zong.com/zongpay/actions/default?method=lookup') | |
| @cust_path = 'https://pay01.zong.com/zongpay/actions/default?method=lookup' | |
| @data = "method=lookup&request=#{URI.escape(@xml_data)}" | |
| end | |
| def open_connection | |
| @http_session = Net::HTTP.new(@uri.host, @uri.port) | |
| @http_session.use_ssl = true if @uri.scheme == "https" | |
| if @http_session.use_ssl? | |
| @http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| @http_session.ssl_timeout=30 | |
| end | |
| end | |
| def post_request | |
| msg_response = {} | |
| @http_session.start { |http| | |
| response = http.post(@uri.path, @data) | |
| msg_response = {:header => response, :body => response.body, :message => response.message} | |
| return msg_response | |
| } | |
| end | |
| end | |
| # Now that the class has been built, we can initiate a request through the Zong class. | |
| # add your customer key here. | |
| req = Zong.new('your_customer_key') | |
| # open our https connection | |
| req.open_connection | |
| # Below will send a post request with the correct path and request parameters | |
| # a hash containing the header, the body, and the message response is returned | |
| my_response = req.post_request | |
| # you can access the xml returned through | |
| print my_response[:body] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment