class KsExpressPaypal::KsRequest

Attributes

cancel_url[RW]
params[RW]
payment_action[RW]
payment_amount[RW]
payment_currency[RW]
return_url[RW]
version[RW]

Public Class Methods

ks_checkout_details(ks_token) click to toggle source

The app must invoke this method after the user redirection to the paypal's site GetExpressCheckoutDetails method will return the details of the checkout It will return the Payer id on successful invocation of the API utilised further For any error message an error response will be generated Format of the error response “response = {success:false,response:[error message]}” Format of the success response “response = {success:true,response:[response generated by Paypal]}”

# File lib/koduc_express_paypal/koduc_request.rb, line 82
def self.ks_checkout_details(ks_token)
        ks_response = ks_request :GetExpressCheckoutDetails,KsExpressPaypal.ks_merchants_details.merge(:TOKEN=>ks_token)
end
ks_do_checkout(ks_token,ks_payment_action,ks_payer_id) click to toggle source

Call this method for completing the checkout Pass the token, payer_id and token as the parameters Returns the response in the format written same as the above API Firstly call GetExpressCheckoutDetails to find out the details of the checkout If the required details are found it will call DoExpressCheckoutPayment method to do the complete process else will return the error message.

# File lib/koduc_express_paypal/koduc_request.rb, line 91
 def self.ks_do_checkout(ks_token,ks_payment_action,ks_payer_id)
         ks_response = ks_request :GetExpressCheckoutDetails,KsExpressPaypal.ks_merchants_details.merge(:TOKEN=>ks_token)
         if ks_response[:response][0].is_a? Hash
                 ks_do_checkout = ks_request :DoExpressCheckoutPayment,KsExpressPaypal.ks_merchants_details.merge({
          :PAYMENTREQUEST_0_AMT => ks_response[:response][0][:PAYMENTREQUEST_0_AMT],
                   :PAYMENTREQUEST_0_PAYMENTACTION => ks_payment_action,
                   :PAYMENTREQUEST_0_CURRENCYCODE => ks_response[:response][0][:PAYMENTREQUEST_0_CURRENCYCODE]
        }).merge(:TOKEN=>ks_token,:PAYERID=>ks_payer_id)
else 
                 return ks_response                                   
end                     
 end
ks_endpoint() click to toggle source

Method to check the endpoint The value is calculated according to the user's choice

# File lib/koduc_express_paypal/koduc_request.rb, line 26
def self.ks_endpoint
        ks_env = KsCheckEnvironment.ks_endpoint
end
ks_handle_response() { || ... } click to toggle source

call this method to handle each response return through the Paypal API collect the response and decode using CGI parsing and form key value pair If the ACK is Success or SuccessWithWarning call the custom response with the response message returned Else return the Error message

# File lib/koduc_express_paypal/koduc_request.rb, line 123
def self.ks_handle_response
                ks_response = yield
                ks_response = CGI.parse(ks_response).inject({}) do |ks_res, (ks_key, ks_value)|
              ks_res.merge(ks_key.to_sym => ks_value.first)
        end
        case ks_response[:ACK]
               when 'Success', 'SuccessWithWarning'
                       success =  KsResponse.new(ks_response)
                       success.ks_success_messages
               else
                       error_messages = ks_response[:L_LONGMESSAGE0]
                       error =  KsExceptionHandling::KsAPIErrors.new(error_messages)
                       error.ks_error_messages
               end  
        rescue => exc
        if !exc.message.nil?
                        error =  KsExceptionHandling::KsAPIErrors.new(exc.message)
                        error.ks_error_messages
                end
end
ks_post_request(ks_method,ks_parameters) click to toggle source

Call this method for post request through Rest Client Pass endpoint calculated above with the parameters with the requested method

# File lib/koduc_express_paypal/koduc_request.rb, line 115
def self.ks_post_request(ks_method,ks_parameters)
        RestClient.post(ks_endpoint,ks_parameters.merge(:METHOD=>ks_method))
end
ks_request(ks_method,ks_params) click to toggle source

method that will make all the API request Call ks_handle_response method to handle each request ks_post_request for every post request

# File lib/koduc_express_paypal/koduc_request.rb, line 107
def self.ks_request(ks_method,ks_params)
        ks_handle_response do
                ks_response = ks_post_request ks_method,ks_params
        end
end
new(params) click to toggle source
# File lib/koduc_express_paypal/koduc_request.rb, line 16
def initialize(params)
        @payment_amount = params[:payment_amount]
        @payment_action = params[:payment_action]
        @payment_currency = params[:payment_currency]
        @cancel_url = params[:cancel_url]
        @return_url = params[:return_url]
end

Public Instance Methods

ks_payment_details() click to toggle source

Method to dissociate the payment details passed by the user The parameters are dissociate so that they can be easily utilised in the further API call

# File lib/koduc_express_paypal/koduc_request.rb, line 32
 def ks_payment_details
ks_payment_details = {
          :PAYMENTREQUEST_0_AMT => payment_amount,
                   :PAYMENTREQUEST_0_PAYMENTACTION => payment_action,
                   :PAYMENTREQUEST_0_CURRENCYCODE => payment_currency
}
return ks_payment_details
 end
ks_redirect_urls() click to toggle source

Method to fetch the Redirect URLS

# File lib/koduc_express_paypal/koduc_request.rb, line 42
def ks_redirect_urls
    ks_redirect_urls = {
                      :cancelUrl=> cancel_url,
                      :returnUrl=> return_url
              }
    return ks_redirect_urls
end
ks_set_express_checkout() click to toggle source

SetExpressCheckout method will set the express checkout method Parameters : Merchant details, payment details and the redirect urls Method will return a Token that will be utilised further on successful completion For any error message an error response will be generated Format of the error response “response = {success:false,response:[error message]}” Format of the success response “response = {success:true,response:[response generated by Paypal]}” Call ks_request method everytime for making a request to the API

# File lib/koduc_express_paypal/koduc_request.rb, line 72
def ks_set_express_checkout
        ks_response = KsExpressPaypal::KsRequest.ks_request :SetExpressCheckout,KsExpressPaypal.ks_merchants_details.merge(ks_payment_details).merge(ks_redirect_urls)       
end
valid?() click to toggle source

Method to check the validity of the parameters before they are passed to the API call Initialize the KsValidation class object's constructor present in the KsAPIValidation module Initialize with the details send by the user through the app The method will return true if everything is properly validated Else it will return the response generated Format of the response “response = {success:true/false,response:[error message]}” Error message in case success is false For true value of success it is blank Will have to validate by the user

# File lib/koduc_express_paypal/koduc_request.rb, line 59
def valid?
        validation_obj = KsAPIValidation::KsValidation.new(KsExpressPaypal.ks_merchants_details,ks_payment_details,ks_redirect_urls)
        response = validation_obj.check_validity
        return response              
end