Sha256: e10db4de46e651dd58dfc72f80478aa9c0f6b927f493d00f34b7f70c1fef3666

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module Pxpay
  # The request object to send to Payment Express
  class Request
    
    attr_accessor :post
    
    # Create a new instance of Pxpay::Request
    # Pxpay::Request.new( id, amount, options = {} )
    # Current available options are:
    # :currency, currency for transaction, default is NZD, can be any of Pxpay::Base.currency_types
    # :reference, a reference field, default is the id
    # :email, email address of user, default is nil
    
    def initialize( id , price, options = {} )
      @post = build_xml( id, price, options )
    end
    
    # Get the redirect URL from Payment Express
    def url
      require 'rest_client'
      require 'nokogiri'
      response = ::RestClient.post("https://sec2.paymentexpress.com/pxpay/pxaccess.aspx", post )
      url = ::Nokogiri::XML(response).at_css("URI").inner_html
      return URI::extract(url).first.gsub("&", "&")
    end
    
    private
    # Internal method to build the xml to send to Payment Express
    def build_xml( id, price, options )
      xml = ::Builder::XmlMarkup.new
      xml.GenerateRequest do
        xml.PxPayUserId ::PXPAY_CONFIG[:pxpay][:pxpay_user_id]
        xml.PxPayKey ::PXPAY_CONFIG[:pxpay][:pxpay_key]
        xml.AmountInput sprintf("%.2f", price)
        xml.CurrencyInput options[:currency] || "NZD"
        xml.MerchantReference options[:reference] || id.to_s
        xml.EmailAddress options[:email]
        xml.TxnType "Purchase"
        xml.TxnId id
        xml.UrlSuccess ::PXPAY_CONFIG[:pxpay][:success_url]
        xml.UrlFail ::PXPAY_CONFIG[:pxpay][:failure_url]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pxpay-0.1.3 lib/pxpay/request.rb
pxpay-0.1.2 lib/pxpay/request.rb