View difference between Paste ID: h888ur4W and iwvF66kV
SHOW: | | - or go back to the newest paste.
1
class PaymentService extends AbstractService {
2
3
    public function checkout(Request $request, Pedido $pedido)
4
    {       
5
        $paymentMethod = $pedido->metodo_pagamento;
6
7
        if (empty($paymentMethod)) {
8
            return false;
9
        }
10
        
11
        $loja = Loja::findById($pedido->loja_id);
12
        $carrinho = new Carrinho;
13
        Pedido::cloneToCarrinho($pedido->id(), $loja->id(), $carrinho);
14
        
15
        $pagamentoConfigModel = PagamentoConfigModel::getPaymentConfigModel(
16
            $paymentMethod, 
17
            $loja, 
18
            $request);
19
20
        switch ($paymentMethod) {
21
            case 'pagseguro':
22
                return $this->processPagseguro($pagamentoConfigModel);
23
                break;
24
    
25
            case 'paypal':
26
                return $this->processPayPal(); 
27
                break;
28
            
29
            default:           
30
                return $pagamentoConfigModel->processPayment(
31-
                    $this,
31+
                    $pedido,
32
                    $carrinho
33
                );
34
                break;
35
        }
36
    }
37
38
    //TEMP: will be moved to Pagseguro class
39
    protected function processPagseguro($configModel)
40
    {        
41
        if ($configModel->isProcessedOutside()) {
42
            return new RedirectResponse(Pagseguro::getCheckoutUrl(
43
                $this->request,
44
                $this->loja,
45
                $this->loja->url,
46
                $this->pedido->id(),
47
                $this->carrinho
48
            ));
49
        }
50
    
51
        //TODO: Atualizar para a versão nova (não depreciada) do PagSeguro quando ela funcionar direito
52
        return $this->processDirectPagseguroOld(
53
            $this->request,
54
            $this->loja,
55
            $this->loja->url,
56
            $this->pedido->id(),
57
            $this->carrinho
58
        );
59
    }
60
    
61
    private function processDirectPagseguroOld($request, $loja, $lojaUrl, $pedidoId, Carrinho $carrinho)
62
    {
63
        $pagseguroTransaction = Pagseguro::processDirectPaymentOld(
64
            $request,
65
            $loja,
66
            $lojaUrl,
67
            $pedidoId,
68
            $carrinho
69
        );
70
    
71
        $paymentLink = $pagseguroTransaction->getPaymentLink();
72
        $status      = $pagseguroTransaction->getStatus()->getTypeFromValue();
73
    
74
        $pedido                   = Pedido::findForUpdateStatus($pedidoId);
75
        $pedido->status_pagamento = $status;
76
        $isSuccessful             = false;
77
    
78
        switch ($status) {
79
            case 'INITIATED':
80
            case 'WAITING_PAYMENT':
81
            case 'IN_ANALYSIS':
82
                $pedido->status = Pedido::STATUS_PENDENTE;
83
                break;
84
            case 'PAID':
85
            case 'AVAILABLE':
86
                $pedido->status = Pedido::STATUS_APROVADO;
87
                $isSuccessful   = true;
88
                break;
89
            case 'REFUNDED':
90
            case 'CANCELLED':
91
                $pedido->status = Pedido::STATUS_CANCELADO;
92
                break;
93
            default:
94
                $pedido->status = Pedido::STATUS_REPROVADO;
95
                break;
96
        }
97
    
98
        $pedido->transacao_id = $pagseguroTransaction->getCode();
99
        $pedido->save();
100
        if ($isSuccessful) {
101
            $this->dispatchNewOrderEvent($pedido);
102
        }
103
        if (empty($paymentLink)) {
104
            return true;
105
        } else {
106
            return new RedirectResponse($paymentLink);
107
        }
108
    }
109
110
    //TEMP: will be moved to Paypal class
111
    protected function processPayPal()
112
    {
113
        $url = Paypal::getCheckoutUrl(
114
            $this->request,
115
            $this->loja,
116
            $this->loja->url,
117
            $this->pedido->id(),
118
            $this->carrinho
119
        );
120
121
        if (empty($url)) {
122
            return null;
123
        }
124
125
        return new RedirectResponse($url);
126
    }
127
}