Using the PayPal REST API, how can I cancel a payment? - ruby-on-rails

Using the PayPal REST API, I cannot seem to figure out how to cancel a payment after a client clicks the "Cancel order and return to website" link. Perhaps in production mode PayPal cancels these payments automatically, but in sandbox mode they seem to stay in the "created" state.
That observation lead me to believe that I need to programmatically cancel each payment upon return to the website's "cancel_url" page. However, I cannot seem to find a cancel function in the PayPal REST API documentation.
https://developer.paypal.com/docs/api/
For what it's worth, I'm using the Ruby API.

I have been in contact with PayPal's technical support last week and this is what they said:
If the buyer has completed the work on the PayPal checkout page
without cancelling the checkout, then they are redirected back to your
site. If you wish for them to have a cancellation at that point, you
can build the Return URL to have a final confirmation (showing final
total to be billed). So at this point the buyer is on your site, but
the payment execution has not happened. If the buyer decides to
proceed, then you run the execute command, updating your database with
the successful payment details. If the buyer decides to cancel at
that point, you do not run the execute command, and purge the payment
ID and the buyer's Payer ID. There would be no request that you would
need to pass to PayPal to cancel what the buyer did on PayPal. This
would be the same type of process done with the Classic APIs using
Express Checkout. Once the buyer gets sent over to PayPal, they
choose the funding source and shipping address and get sent back to
your site, if they want to cancel the transaction, your site does not
send any API call to PayPal to cancel or void the EC token. It is
just not used to collect the payment.
When asked how to handle non-executed payments and if they automatically void non-executed payments after a certain amount of time:
Yes, I recommend deleting the Payment ID from your database, so there
is no accidental payment. Our system, by default, will expire the
payment approval made by PayPal payers if the payment hasn't been
executed within 3 hours.
Permission has been given by PayPal to post their answer here.

I actually talked directly with someone from PayPal. The answer was:
Once a user authorized a sale, the sale has to be executed. It cannot be canceled
past beyond that point. So if the user comes back on your return URL, you are
simply expected to run an "execute" command on your payment.
This may not directly apply to your case. I think that the only way to "cancel" would be to first "execute", then apply a full "refund". Otherwise, never execute (which I also view as strange because that looks like a potential for security problems. That said, the main problem that could happen is an "execute" on the payment and your company would receive the money that you can then manually refund if necessary...)

I think you need to void the authorization of the payment:
https://developer.paypal.com/docs/api/#void-an-authorization

I used paypal api v2, and there is cancel order api.
https://developer.paypal.com/docs/api/orders/v1/?mark=cancel%20order#orders_cancel

Related

PayPal using MVC issues

I am trying to set up a simple payment using PayPal and MVC, right now I have code which accepts an ID from the database on items you can purchase.
I have managed to find code which takes me to this screen:
I have some code which after you click "Continue" PayPal will return to and call, but the only thing I have in that bit of code is this:
public ActionResult PayPalExpressCheckoutAuthorisedSuccess(string token, string PayerID)
{
return RedirectToAction("GivePurchasedItemsToUse");
}
I can see they have given me the token and payer id, but I am unsure where to go from here, in the PayPalExpressCheckourAuthorisedSuccess method, do I send something back to PayPal? Nothing yet has been displayed on any of the two PayPal accounts I am using.
The tutorial I followed was: http://rituranjangupta.blogspot.nl/2012/10/paypal-with-aspnet-mvc.html
Assuming I understood the state of where you are, which is after a user has "approved*" a Paypal Payment (where Paypal redirects back to your RETURNURL), you will need to "complete" the transaction.
GetExpressCheckoutDetails - (optional) if you want to obtain details e.g. Paypal shipping address
DoExpressCheckoutPayment - this is the actual step that completes the transaction
Extensive Reference: Paypal Developer -> Express Checkout
*approved
TIP: Don't misinterpret "approved" as "payment" - all it really means is that the user has approved a Paypal payment for you to use (e.g. user has agreed to pay some amount, chosen what funding source, shipping, etc.) - you still need to "finalize" things
Hth..

Returning params/credit card info during unsuccessful Braintree transaction

When I submit a braintree transaction, and it is unsuccessful, it returns result.params that I can use. However, due to PCI compliance reasons, it cannot return CC or CVV information.
Since it's an unsuccessful txn, the page renders the current page. I would like to auto populate the payment info, whether it's a CC or paypal transaction, so the user does not need to reenter the payment. I think it's bad user experience.
If repopulating the payment info is not possible, could I just somehow reuse the generated payment_method_nonce and send it to braintree. Looking through the docs, I can't seem to find anything that I can use to solve this.
https://developers.braintreepayments.com/javascript+ruby/reference/general/result-handling/result-objects#params
I work at Braintree. If you're looking for more help with your integration, you can always get in touch with our support team.
Instead of creating a transaction with the payment method nonce, create a payment method with the nonce. This will give you a reusable token you can then use to create a transaction.
From the "Getting Started" guide:
Create a transaction
Obtained from the Braintree client SDK, a payment method nonce can be used in any place credit card information or a payment method token is used in the Braintree server-side client libraries.
So since you can create a payment method token from credit card params, you can also create it from a payment method nonce.
Simply post it to your own controller/action that is set up to make the request. From there, you can check the success of the transaction and respond accordingly. You also have full access to the params, to do with as you wish. You probably want to keep them out of your logs.

Get first_name (and other info) from Paypal and create Rails Devise account using paypal-recurring gem

I have create the basics of subscription Paypal using the RailsCast and now I'm doing what is missing there.
Now I'm developing the process to do the Devise user registration together/just after the payment is done. For now, I'm trying something like this and this.
As the RailsCast got the e-mail from PayPal using this line:
#subscription.email = PayPal::Recurring.new(token: params[:token]).checkout_details.email
So, I thought that I could get first name, middle name and last name from PayPal as well. From PayPal documentation it seems that it is possible but I couldn't get it through paypal-recurring gem.
I have tried to see if I can learn what I have to do from paypal-recurring GitHub docs and code but I couln't find and tried some possibilities without success.
Is it possible? Maybe in another way not using paypal-recurring gem?
If you have another recomendation/reference to do this registration process, please, let me know.
Update
As #Andrew suggested PayPal IPN, I thought it would be better update my question as I want to have the first_name from PayPal as a default value to ease the process to the user register in my database but he or she may want to change to another name.
The process that I want is something like:
The user chooses his plan and to pay with PayPal
User is sent to PayPal
User fills payment info on PayPal site
User is sent again to my site
My site gets e-mail and name of the user from PayPal and asks the user to confirm or change the data, and provide his password to create the login to my site
My site uses the user data provided to register the user and sends the request to PayPal to request the payment and create the recurring profile
Ok, based on your current outline of steps you can handle that exact flow using Express Checkout.
SetExpressCheckout will generate a token for you, and you then redirect the user to PayPal. They review and approve the payment and are then redirected back to your site. There you can call GetExpressCheckoutDetails to obtain the email, name, address, etc. and display a final review page for the customer to confirm everything or make changes if necessary. Finally, you'd call DoExpressCheckoutPayment to finalize the payment using the customers confirmed details.
I would look at PayPal IPN (Instant Payment Notification). It'll POST transaction details to a listener script you have sitting on your server so you can process the data accordingly in real-time.

refund_status NO_API_ACCESS_TO_RECEIVER, paypal response

I'm using this gem for payments in my rails app:
https://github.com/jpablobr/active_paypal_adaptive_payment
I can not make a refund, I get the next message in response log:
...refund_status="NO_API_ACCESS_TO_RECEIVER">]> response_envelope=#<Hashie::Rash ack="Success"....
I am using the pay_key for make a refund.
Its possible fix this error?
Thank you so much!
Ok the problem was, For a receiver to grant permission to refund to the API Caller:
Login to www.paypal.com (For Sandbox: www.sandbox.paypal.com - Make sure you are already signed in https://developer.paypal.com) and login with the receiver account. Goto Profile -> API Access -> Grant API Permission -> Input the API Caller (Example: May6_1307031077_biz_api1.paypal.com) in Third Party Permission Username textbos and click Lookup button -> Enable checkbox for "Issue a refund for a specific transaction" and click Add button.
This is how receiver can grant permission to API Caller.
Once this is enabled, the refund should work.
Founded in https://www.x.com/developers/paypal/forums/adaptive-payments-api/adaptive-refund-notprocessed
Edited.......
You can see on https://www.x.com/devzone/articles/paypal-permissions-and-advantages-integrating-permissions-api If you need make this feature with Api you can use this gem:
https://github.com/moshbit/paypal_permissions
Thank you very much!
I was having the exact same problem and my issue could not be resolved by the accepted solution. My problem was that I sent money from a merchant account to a seller account and was trying to refund the transaction, but there is (currently) no way to grant API permissions from a personal account to allow the refund transaction to go through. I just called Paypal, and they said that there's no way to complete the refund I was attempting to do, but since I only needed the refund for testing purposes (to make sure there was always money in my merchant account), they suggested that I instead just send an equal payment from my personal account back to my merchant account. I hope this helps someone.

Redirect back to "purchases" page after order completion

I've inherited an app which uses the legacy shopify API and I have a hard deadline of August 21st, so I want to avoid updating the API to solve the problem.
We are selling e-documents, and the current purchase process is as follows:
User searches and places a product in the cart
The User checks out
and goes through the purchase process on the Shopify site
This all seems to work fine. What we would like is to redirect the user back to our app with information about the successful purchase (or failure) of the products. As it stands, we don't know what products the user has purchased and we are therefore unable to provide them links to download the documents.
Is there some parameter we can pass to shopify to create that redirect, or do we need to ping the API about past purchases, or is there some other step we are missing?
This one is quite easy... Shopify provides you with a textarea element in the admin (checkout and payments) where you can paste code. In this code, you have access to the entire order contents. You can get at this with Liquid.
All you have to do, is render a link to your App and pass the order details to the App via that link. Now your customers will see the Thank You for your purchase, and a button/link to click to complete things as you wish.
Okay, it turns out that what I'm looking for are shopify Webhooks: http://wiki.shopify.com/WebHook
It appears that the app is already set up to send order_create and order_payed and all we have to do is capture the data sent to us.

Resources