Reputation: 611
I'm using the PayPal REST SDK to make a PayPal payment. When completing a purchase and calling the executePayment() method, the payment object's state isn't updating, and still says created instead of approved.
The payment goes through properly, and if I immediately look up the purchase after executing it, I get the expected approved state:
$paypal = new PayPal;
$payment = $paypal->executePayment($order_id, $payer_id);
echo $payment->getState(); // "created"
$lookup = Payment::get($order_id, getApiContext());
echo $lookup->getState(); // "approved"
(Here is the relevant method from the PayPal class..)
Class PayPal {
public function executePayment($paymentId, $payerId)
{
$payment = Payment::get($paymentId, getApiContext());
$paymentExecution = new PaymentExecution;
$paymentExecution->setPayer_id($payerId);
$payment->execute($paymentExecution, getApiContext());
return $payment;
}
}
On the PayPal pizza app, the executePayment method works perfectly, and the returned Payment object shows a state of "approved" immediately..
$payment = executePayment($order['payment_id'], $_GET['PayerID']);
echo $payment->getState(); // "approved"
My executePayment() is the exact, verbatim, code from the pizza app, the only difference is that I've put it into a class.
So what gives? Why would I not be getting "approved" back from the executePayment() method right away?
EDIT :
On further inspection, the PayPal pizza app is using rest-api-sdk-php v 0.6.* but the actual rest api sdk, is on version 0.7.*
The Payment::execute() method has changed. In v0.6 it returns $this payment object like so:
class Payment extends PPModel implements IResource {
...
public function execute( $payment_execution, $apiContext=null) {
...
$json = $call->execute( array('PayPal\Rest\RestHandler'),
$this->fromJson($json);
return $this;
}
}
but now in v0.7 it's creating a new payment object and setting the values of the new object, which seems to not be working correctly..
class Payment extends PPModel implements IResource {
...
public function execute( $payment_execution, $apiContext=null) {
...
$ret = new Payment();
$ret->fromJson($json);
return $ret;
}
}
So.. if you revert these changes to what they were in v0.6 instead of the $ret it works.. Not sure what the reasoning was for creating a new Payment(); object vs returning $this though.. maybe someone can enlighten me?
Upvotes: 3
Views: 1560
Reputation: 1489
The reason behind making the change in 0.7 was, the status you were getting before was because of a bug.
When you do a get Payment, you get a payment object. And previously, when you run execute, it was filling the object again, without resetting it. Because of that, you were able to do a getStatus, but that was not returned in Execute method. It was already there when you did a get call.
I hope this helps you understand the change.
Upvotes: 0
Reputation: 65
I was having a problem with the paypal sandbox not completing the order.
I was able to fix this problem without modifying the paypal api code.
also make sure on the developer option 'Review test payments before they're completed.' is set to off.
Upvotes: 2