ActionController::TestCase mock PayPal purchase - ruby-on-rails-3

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

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.

Related

Hosted Payment Page and http response

We are investigating and issue where the payment action is successful but we haven't received a response from Global payments. We believe this is the root cause. We are obviously keen to confirm this. Does global payments track the responses sent back to the customer. If so if we specified a specific date and time is it possible to get logs?
On separate thread how can we determine when the submit button has been clicked when we are using a Hosted Payment Page.
We made a payment to global payments which went through but the subsequent actions that depended on the response from global payment didn't fire.
These things are usually just a misconfiguration in the response URL of the HPP. Please contact us directly at developers#globalpay.com and we will be able to discuss more specifics about your implementation which may not be suitable for more public forums.
Thanks

How does one associate a website user with a Paypal subscription?

I am implementing subscriptions to a premium service on a website using Paypal as the payment service. I have successfully created a Catalog Product and Billing Plan through the API, and I am able to get to the payment page on Paypal, but it's not clear how I'm supposed to persist a user identifier through the purchase process.
I assumed it would be something along the lines of passing a user id somewhere, but there's nothing in the Paypal documentation about this. I need to be able to let the user make a purchase and have the Paypal webhook send the confirmation to an endpoint on my site, and that's where I'd expect to get their user id to toggle the subscription on their account on my end.
Is there something I'm missing? There has to be a way to do this cause I'd imagine it's a pretty common use case. If anyone has information or has done this before, I'd love to hear. Thanks.
The only truly secure way I've found when using javascript SDK, is to securely generate a unique custom_id on your server side associated with the user.
Then when you create the buttons, the 'createSubscription' function takes custom_id as a parameter.
Then use a webhook to receive events from your subscription and the custom_id will be present in the body of all BILLING.SUBSCRIPTION events under resource.custom_id.
I am able to get to the payment page on PayPal,
You are vague about what you are doing here. There are multiple ways (and some ways have multiple versions) of accepting subscriptions via PayPal, so it is important that you provide full details about the method you are using.
The time to associate a created subscription ID with a user ID is when it is approved, in the onApprove function if you are using a Smart Payment Button: https://developer.paypal.com/docs/subscriptions/integrate/#4-create-a-subscription

Paypal rest api express checkout with no shipping field (WebProfile handling)

I'm using Paypal rest api to make payment
the workflow is:
Create payment
Redirect to approval Url
User approved (return back to my site)
Execute payment
But there's one thing that I don't want users re-filling shipping address again because it was filled in my website.
So I change the workflow to:
Create web profile (set no shipping field)
Get web profile ID
Create payment with experienceProfileId given
Redirect to approval Url
User approved (return back to my site)
Execute payment
But I found this will create a lots WebProfile every time user request payment.
I think it is crazy to do:
create and delete it later again and again
attempt listing WebProfiles and check which is the one I want to use every time while creating payment
store experienceProfileId as a constant
What is the best practice for handling WebProfile or does there any solution just hiding shipping address while user approving payments?
Maybe this is not the answer regarding this "WebProfile". As a fact, I dont know what exactly "WebProfile" does or is.
I worked on the same Workflow these days. As you wrote I needed to predefine some address. For me it was obvious, that I have to do the database-stuff on my Website. Then I exactly define the order, shipping_address, etc. and send the users to Paypal.
If you predefine the new ShippingAddress() to your ´new ItemList()´ by
$itemlist->setShippingAddress($shippingaddress) the user cannot change it within the Process.
http://i.imgur.com/nAg8jxU.png
Maybe this helps you a little.

Get transaction details in real time, Paypal

I am creating a website and instead of using a fully featured shopping cart I want to use the Paypal API to "automate" some functionality.
By that, the user clicks on the "buy now" button goes and makes payment to paypal, and I want them to be redirected back to my site so I can process some info for my database. I looked into IPN but it seems like an overkill. Is there any simpler way of doing it?
You should look at either IPN or Express Checkout.
If you are using web standard payments it is important to note that a customer does not have to return to your page, once they hit pay on the Paypal site - the transaction is complete. Hence relying on them to return to your page to do a database update is a bad idea. This is where IPN comes into play.
Express Checkout gets around this by allowing the customer to authorize on Paypal and complete a transaction on your site. Passing the data directly to your site once the transaction is complete.

PayPal Custom variable returned back

I am new to PayPal API, so I have a nooby question.
When I send user from my shopping site to PayPal payment page e.g. https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=xxxxxx&custom=xxxx and I use custom variable with unique ID about product inside my database and when user get returned back to my website thank you page, do I use simple $_GET["custom"] inside my php script to grab that value or I need some PayPal API code implemented inside my website?
Thanks for help.
In order to retrieve this value, you would not be able to get it as you described, however, you can do one of a couple of things:
post the custom value to a cookie or session variable before sending the user to paypal, then retrieving the cookie or session variable value when they are returned from paypal.
(preferred method) implement an IPN listener that will be able to parse the posted transaction details, one of which is the custom value. This method would allow you to confirm a completed transaction as well as do pretty much anything else with the data that you need to do.
Paypal has PHP code samples for an IPN listener on their DEV site. It's pretty easy to implement and follow along.
I hope this helps.
Old thread I know but if I understand the user correctly and he is using the IPN url to collect the returned data he can use $myphpvar = $_POST[custom];
More info # http://www.brianmoreau.com/articles/paypal_buy_now_button_sending_custom_variables.php