router = $router; $this->transactionManager = $transactionManager; } private function redirectURL(TransactionInterface $transaction): array { return [ 'urlConfirmation' => $this->router->generate ( 'api_paypal_confirmation_paiement', ['publicKey'=>$transaction->getPublicKey()], UrlGeneratorInterface::ABSOLUTE_URL ), 'urlCancel' => $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnError(), [], UrlGeneratorInterface::ABSOLUTE_URL ), 'urlSuccess' => $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnSuccess(), [], UrlGeneratorInterface::ABSOLUTE_URL ) ]; } /** * @param TransactionInterface $transaction * @return string|null */ public function inti(TransactionInterface $transaction){ // Create an ApiContest instance $apiContext = new ApiContext( new OAuthTokenCredential( $transaction->getPaymentMode()->getIdentifier(), $transaction->getPaymentMode()->getCertificate() ) ); // Check if the context of the payment method if production if ($transaction->getPaymentMode()->getContext() == 'PRODUCTION'){ $apiContext->setConfig([ 'mode'=>'live', ]); } // init redirect url $urls = $this->redirectURL($transaction); $urlRedirection = (new RedirectUrls()) ->setReturnUrl($urls['urlConfirmation']) ->setCancelUrl($urls['urlCancel']); $listItem = new ItemList(); /** * @var $basketLine BasketLine */ foreach ($transaction->getBasketLine() as $basketLine){ $item = (new Item()) ->setName($basketLine->getProduct()->getLabel()) ->setPrice($basketLine->getProduct()->getPrice()) ->setCurrency($transaction->getPaymentMode()->getCurrency()) ->setQuantity($basketLine->getQuantity()); $listItem->addItem($item); } // init paypal transaction $amount = $transaction->getTotal(); $details = (new Details())->setSubtotal($amount); $amount = (new Amount()) ->setTotal($amount) ->setCurrency($transaction->getPaymentMode()->getCurrency()) ->setDetails($details); $payPalTransaction = (new Transaction()) ->setItemList($listItem) ->setDescription($transaction->getInformation()) ->setAmount($amount) ->setCustom($transaction->getPaymentMode()->getPaypalCustom()); // init payment $payment = new Payment(); $payment->setTransactions([$payPalTransaction]); $payment->setIntent('sale'); $payment->setRedirectUrls($urlRedirection); // inti payer method $payment->setPayer((new Payer())->setPaymentMethod('paypal')); try { $payment->create($apiContext); $urlRedirection = $payment->getApprovalLink(); }catch (PayPalConnectionException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalConfigurationException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalInvalidCredentialException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalMissingCredentialException $exception){ $urlRedirection = $urls['urlCancel']; } return $urlRedirection; } /** * @param TransactionInterface $transaction * @param array $payPalParams * @return mixed */ public function confirmationPayment(TransactionInterface $transaction, array $payPalParams){ $urls = $this->redirectURL($transaction); $apiContext = new ApiContext( new OAuthTokenCredential( $transaction->getPaymentMode()->getIdentifier(), $transaction->getPaymentMode()->getCertificate() ) ); if ($transaction->getPaymentMode()->getContext() == 'PRODUCTION'){ $apiContext->setConfig([ 'mode'=>'live', ]); } $payment = Payment::get($payPalParams['paymentId'], $apiContext); $execution = (new PaymentExecution()) ->setPayerId($payPalParams['PayerID']); // ->setTransactions($payment->getTransactions()); try { $payment->execute($execution, $apiContext); $urlRedirection = $urls['urlSuccess']; $transaction->setInformation($payment->getState()); switch ($payment->getState()){ case "approved" : $total_paypal = 0; foreach ($payment->getTransactions() as $paypal_transaction){ $total_paypal += $paypal_transaction->amount->getTotal(); } $transaction->setStatus(TransactionStatusAction::TRANSACTION_PAID); $transaction->setTotalPayer($total_paypal); $transaction->setIdTransactionExternal($payment->getId()); break; default : $transaction->setIdTransactionExternal($payment->getId()); $transaction->setStatus(TransactionStatusAction::TRANSACTION_ERROR); break; } $this->transactionManager->update($transaction); }catch (PayPalConnectionException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalConfigurationException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalInvalidCredentialException $exception){ $urlRedirection = $urls['urlCancel']; }catch (PayPalMissingCredentialException $exception){ $urlRedirection = $urls['urlCancel']; } return $urlRedirection; } }