Reputation: 2410
Is there a way to mock the purchase of PaypalExpressGateway
I have used two actions for PayPal payment processing, first action take the user to paypal sandbox login and will redirect back to my application for payment confirmation
But the PAYPAL_GATEWAY.purchase
method is not succeeding and returns with an error message Payment has not been authorized by the user.
Well I guess this is due to the skipped step for authorizing purchase by user
Is there a way I could just fake or mock PayPal to accept the transaction ?
My ActionController::TestCase
to test the actions products#initiate_payment
products#confirm_payment
call these actions as
post :initiate_payment, initiate_payment_action_params(@product)
get :confirm_payment, confirm_action_params(@product, product_purchased, paypal_token)
And to setup the paypal callback response then redirect to paypal login
@setup_response = PAYPAL_GATEWAY.setup_purchase(amount, ip: request.remote_ip,
items: [{name: @product.name, quantity: 1, description: @product.description, amount: amount}],
currency: 'USD',
return_url: url_for(action: 'confirm_payment',
product_purchased: @product_purchased.id,
only_path: false),
cancel_return_url: url_for(action: 'index',
product_purchased: @product_purchased.id,
only_path: false))
redirect_to PAYPAL_GATEWAY.redirect_url_for(@setup_response.token)
I have handled the purchase transaction in my products#confirm_payment
action
I have mocked the params for confirm_payment as
{"product_purchased"=>"10", "token"=>"EC-91J25480XA799581U", "PayerID"=>"4QBC9Y658K6MA", "id"=>"55", "controller"=>"products", "action"=>"confirm_payment"}
get :confirm_payment, confirm_action_params(@product, product_purchased, paypal_token)
Configuration for PAYPAL_GATEWAY
PAYPAL_GATEWAY ||= ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
I haven't found any approach to achieve this so this might help many of them...
Any better solution other than this will be most appreciated
Thanks in advance
Upvotes: 3
Views: 523
Reputation: 7319
Short answer is you would have to walk through and make a test purchase. Plus, this would allow you to go through the experience that the buyer would from start to finish. Is there a particular reason you are trying to skip a step or two?
Some merchants will also walk thru the flow just once, and capture the information that is sent back and then just use that same string to create a form page that they just use to post the information back to their page for testing. That way they don't have to go thru the flow over and over, but this may or may not work depending on your set up.
Most typically go with option A and go through the flow from start to finish.
Upvotes: 1