Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal REST API: When do I get the approved state for payments over a PayPal account?

Tags:

paypal

When I process a payment via the REST API (using the PayPal REST SDK for .NET) I first create a payment

Payment payment = ...
Payment createdPayment = payment.Create(apiContext);

I store the ID of the created payment for later use. Then I redirect to the provided payment page. In the page that PayPal calls after the user approved tha payment I want to get information about the payment.

I call

Payment payment = Payment.Get(apiContext, paymentID);

using the stored payment ID.

I expected the state of the payment beeing 'approved' here but it is still 'created'.

What could be wrong? When or how do I get the 'approved' state?

like image 428
Jürgen Bayer Avatar asked Dec 06 '22 02:12

Jürgen Bayer


2 Answers

  1. You need to

    construct a payment object with payment id and use the payer id returned from PayPal and make the API call using the access token.

    That is the query params returned from the SuccesUrl you provided when redirecting to paypal. e.g

    http://yourserver.com/payment/paypal/success.asp?payerid=EC-43242423&token=FS321-DFSD-3123DFS-3G243

  2. Then

    Execute a payment:

    Payment payment = new Payment("");
    PaymentExecution pymntExecution = new PaymentExecution();
    pymntExecution.payer_id = ("");
    Payment executedPayment = pymnt.Execute(apiContext,pymntExecution);
    
  3. See step 4 / 5 from paypal tutorial:

    https://devtools-paypal.com/guide/pay_paypal/dotnet?interactive=OFF&env=sandbox

Edit by Jürgen Bayer

I accept this answer but I just add a few things I found out with the help of the PayPal support:

  • created results after a payment was created. It is still the state after the payer authorized the payment (that was my error in reasoning: approved does not mean "approved by the payer" but "approved by PayPal". There is no state for "authorized").
  • After a payment was executed the state can be pending, approved, failed, cancelled or expired
  • pending means that PyPal has to perform some security checks or that the payment is a bank transfer. In the latter PayPal must wait until the bank transfer was completed.
  • approved means that the payment was approved by PayPal. This state can result with credit card payments.
  • failed: According to https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/#id091EB04C0HS this state results if the payment is a bank transfer that could not be completed, for example because the bank account had not enough funds.
  • cancelled: The payment was canceled (question is though, by whom?)
  • expired: The payment authorization has expired and could not be captured.
like image 80
user3041539 Avatar answered Jun 05 '23 20:06

user3041539


for node.js...

You need to call execute to update the status to approve

var execute_payment_json = {
    "payer_id": "Appended to redirect url",
    "transactions": [{
        "amount": {
            "currency": "USD",
            "total": "1.00"
        }
    }]
};

var paymentId = 'PAYMENT id created in previous step';

paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
    if (error) {
        console.log(error.response);
        throw error;
    } else {
        console.log("Get Payment Response");
        console.log(JSON.stringify(payment));
    }
});
like image 25
keithics Avatar answered Jun 05 '23 22:06

keithics