Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PaymentService extends AbstractService {
- public function checkout(Request $request, Pedido $pedido)
- {
- $paymentMethod = $pedido->metodo_pagamento;
- if (empty($paymentMethod)) {
- return false;
- }
- $loja = Loja::findById($pedido->loja_id);
- $carrinho = new Carrinho;
- Pedido::cloneToCarrinho($pedido->id(), $loja->id(), $carrinho);
- $pagamentoConfigModel = PagamentoConfigModel::getPaymentConfigModel(
- $paymentMethod,
- $loja,
- $request);
- switch ($paymentMethod) {
- case 'pagseguro':
- return $this->processPagseguro($pagamentoConfigModel);
- break;
- case 'paypal':
- return $this->processPayPal();
- break;
- default:
- return $pagamentoConfigModel->processPayment(
- $pedido,
- $carrinho
- );
- break;
- }
- }
- //TEMP: will be moved to Pagseguro class
- protected function processPagseguro($configModel)
- {
- if ($configModel->isProcessedOutside()) {
- return new RedirectResponse(Pagseguro::getCheckoutUrl(
- $this->request,
- $this->loja,
- $this->loja->url,
- $this->pedido->id(),
- $this->carrinho
- ));
- }
- //TODO: Atualizar para a versão nova (não depreciada) do PagSeguro quando ela funcionar direito
- return $this->processDirectPagseguroOld(
- $this->request,
- $this->loja,
- $this->loja->url,
- $this->pedido->id(),
- $this->carrinho
- );
- }
- private function processDirectPagseguroOld($request, $loja, $lojaUrl, $pedidoId, Carrinho $carrinho)
- {
- $pagseguroTransaction = Pagseguro::processDirectPaymentOld(
- $request,
- $loja,
- $lojaUrl,
- $pedidoId,
- $carrinho
- );
- $paymentLink = $pagseguroTransaction->getPaymentLink();
- $status = $pagseguroTransaction->getStatus()->getTypeFromValue();
- $pedido = Pedido::findForUpdateStatus($pedidoId);
- $pedido->status_pagamento = $status;
- $isSuccessful = false;
- switch ($status) {
- case 'INITIATED':
- case 'WAITING_PAYMENT':
- case 'IN_ANALYSIS':
- $pedido->status = Pedido::STATUS_PENDENTE;
- break;
- case 'PAID':
- case 'AVAILABLE':
- $pedido->status = Pedido::STATUS_APROVADO;
- $isSuccessful = true;
- break;
- case 'REFUNDED':
- case 'CANCELLED':
- $pedido->status = Pedido::STATUS_CANCELADO;
- break;
- default:
- $pedido->status = Pedido::STATUS_REPROVADO;
- break;
- }
- $pedido->transacao_id = $pagseguroTransaction->getCode();
- $pedido->save();
- if ($isSuccessful) {
- $this->dispatchNewOrderEvent($pedido);
- }
- if (empty($paymentLink)) {
- return true;
- } else {
- return new RedirectResponse($paymentLink);
- }
- }
- //TEMP: will be moved to Paypal class
- protected function processPayPal()
- {
- $url = Paypal::getCheckoutUrl(
- $this->request,
- $this->loja,
- $this->loja->url,
- $this->pedido->id(),
- $this->carrinho
- );
- if (empty($url)) {
- return null;
- }
- return new RedirectResponse($url);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement