public function executePayment($orderid, $userid) { $result = array("status" => true, "message" => ""); if ($orderid > 0) { $orderid = (int) $orderid; $paypalCredentials = $this->getPaypalCredentials(); $apiContext = new ApiContext(new OAuthTokenCredential($paypalCredentials["clientid"], $paypalCredentials["secret"])); $apiContext->setConfig($this->paypalConfig); $paypal_paymentinfo = $this->getOrderPaypalInfo($orderid); if (strlen($paypal_paymentinfo["paypal_paymentid"]) > 0 && strlen($paypal_paymentinfo["paypal_payerid"]) > 0) { try { $payment = Payment::get($paypal_paymentinfo["paypal_paymentid"], $apiContext); $execution = new PaymentExecution(); $execution->setPayer_id($paypal_paymentinfo["paypal_payerid"]); $payment_result = $payment->execute($execution, $apiContext); //Save payment info if ($this->saveExecutedPayment($payment_result, $orderid, $userid)) { $result["status"] = true; $result["message"] = "Payment processed successfully"; //Notify the customer and notification list that payment for the order has been approved $this->setNotificationOrderPaid($orderid, $userid); } else { $result["status"] = false; $result["message"] = "There was an error saving payment information"; } return $result; } catch (PayPal\Exception\PPConnectionException $ex) { $result["status"] = false; $result["message"] = "There was an error processing the payment."; $this->log->addError($ex->getMessage() . LOG_LINESEPARATOR . "TRACE: " . $ex->getTraceAsString() . LOG_LINESEPARATOR); return $result; } } else { $result["status"] = false; $result["message"] = "Invalid payment id."; $params = "orderid = {$orderid} - paymend_id = " . $paypal_paymentinfo["paypal_paymentid"] . " - payer_id = " . $paypal_paymentinfo["paypal_payerid"]; $this->log->addError("executePayment " . $result["message"] . " {$params} " . LOG_LINESEPARATOR); return $result; } } else { $result["status"] = false; $result["message"] = "Invalid order id."; return $result; } }
// a payment that has been approved by // the buyer by logging into paypal site. // You can optionally update transaction // information by passing in one or more transactions. // API used: POST '/v1/payments/payment/<payment-id>/execute'. require __DIR__ . '/../bootstrap.php'; use PayPal\Api\ExecutePayment; use PayPal\Api\Payment; use PayPal\Api\PaymentExecution; session_start(); if (isset($_GET['success']) && $_GET['success'] == 'true') { // Get the payment Object by passing paymentId // payment id was previously stored in session in // CreatePaymentUsingPayPal.php $paymentId = $_SESSION['paymentId']; $payment = Payment::get($paymentId, $apiContext); // PaymentExecution object includes information necessary // to execute a PayPal account payment. // The payer_id is added to the request query parameters // when the user is redirected from paypal back to your site $execution = new PaymentExecution(); $execution->setPayer_id($_GET['PayerID']); //Execute the payment // (See bootstrap.php for more on `ApiContext`) $payment->execute($execution, $apiContext); echo "<html><body><pre>"; var_dump($payment->toArray()); echo "</pre><a href='../index.html'>Back</a></body></html>"; } else { echo "User cancelled payment."; }