Commit d884b8f5 authored by Andy J's avatar Andy J

PayPal Plus (Added more validation, handling order/payment state while...

PayPal Plus (Added more validation, handling order/payment state while completing order request) (Part 1/3)
parent 11cd2ee1
......@@ -201,26 +201,37 @@ switch ($action) {
$success = isset($_GET['r']) && $_GET['r'] === 'true';
if (!$success) {
$this->doLog("PayPal Redirect (User Canceled)", LOGLEVEL_NOTICE);
_redirect('bestellvorgang.php?editZahlungsart=1');
}
$paymentId = $_GET['paymentId'];
$token = $_GET['token'];
$payerId = $_GET['PayerID'];
$args = [
'paymentId' => $_GET['paymentId'],
'token' => $_GET['token'],
'payerId' => $_GET['PayerID']
];
$validArgs = array_filter($args);
if (count($args) !== count($validArgs)) {
$argStr = $this->formatObject((object)$args);
$this->doLog("PayPal Redirect (Missing arguments)\n{$argStr}", LOGLEVEL_ERROR);
$api->addCache('paymentId', $paymentId)
->addCache('token', $token)
->addCache('payerId', $payerId);
_redirect('bestellvorgang.php?editZahlungsart=1');
}
foreach ($validArgs as $k => $v) {
$api->addCache($k, $v);
}
try {
$payment = Payment::get($paymentId, $apiContext);
$payment = Payment::get($validArgs['paymentId'], $apiContext);
$api->createPaymentSession();
$api->logResult('GetPayment', $paymentId, $payment);
_redirect('bestellvorgang.php');
} catch (Exception $ex) {
$api->handleException('GetPayment', $paymentId, $ex);
$api->handleException('GetPayment', $validArgs['paymentId'], $ex);
_redirect('bestellvorgang.php?editZahlungsart=1');
}
......
......@@ -540,10 +540,27 @@ class PayPalPlus extends PaymentMethod
public function preparePaymentProcess($order)
{
try {
$lastOrderId = $this->getCache('lastOrderId');
$paymentId = $this->getCache('paymentId');
$payerId = $this->getCache('payerId');
if (empty($paymentId) || empty($payerId)) {
// payment related information already deleted (multiply order submission)
// order already saved?
$lastOrder = new Bestellung($lastOrderId);
$lastOrder->fuelleBestellung(false);
if ($lastOrder->kBestellung > 0) {
$paymentHash = $this->generateHash($order);
$returnUrl = Shop::getURL().'/bestellabschluss.php?i='.$paymentHash;
header("location: {$returnUrl}");
}
$str = json_encode($this->getCache());
throw new Exception(sprintf('Missing payment/payer id, %s (%s)', $str, $this->moduleID));
}
......@@ -631,11 +648,16 @@ class PayPalPlus extends PaymentMethod
$order->updateInDB();
if ($payment->getState() === 'approved') {
$state = $payment->getTransactions()[0]
->getRelatedResources()[0]
->getSale()
->getState();
if ($payment->getState() === 'approved')
{
try {
$state = $payment->getTransactions()[0]
->getRelatedResources()[0]
->getSale()
->getState();
} catch (Exception $ex) {
$state = 'invalid';
}
if ($state === 'completed') {
$ip = new stdClass();
......@@ -660,12 +682,10 @@ class PayPalPlus extends PaymentMethod
}
$this->unsetCache();
$this->addCache('lastOrderId', $order->kBestellung);
$paymentHash = $this->generateHash($order);
$returnUrl = Shop::getURL().'/bestellabschluss.php?i='.$paymentHash;
$this->redirectToOrderDetails($order);
header("location: {$returnUrl}");
exit;
} catch (PayPal\Exception\PayPalConnectionException $exp) {
if ($data = $exp->getData()) {
$error = new PayPal\Api\Error($data);
......@@ -679,15 +699,27 @@ class PayPalPlus extends PaymentMethod
}
}
}
$this->handleException('ExecutePayment', $payment, $exp);
Shop::Smarty()->assign('error', $exp->getMessage());
throw $exp;
} catch (Exception $ex) {
$this->handleException('ExecutePayment', $payment, $ex);
Shop::Smarty()->assign('error', $ex->getMessage());
}
}
/**
* @param array $oArtikel_arr
*
* @return bool
*/
public function redirectToOrderDetails($order)
{
$paymentHash = $this->generateHash($order);
$returnUrl = Shop::getURL().'/bestellabschluss.php?i='.$paymentHash;
header("location: {$returnUrl}");
exit;
}
/**
* @param array $oArtikel_arr
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment