Sha256: 390a041d06bc016b72a22e06ec8c9d89560d872cb6748c53202f9ecc89b13eb0

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://sec.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.8 lib/pxpay/request.rb
pxpay-0.1.5 lib/pxpay/request.rb