Issues (13)

src/Controller/ResultController.php (1 issue)

1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Transaction;
6
use App\Service\AlmaApi;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
13
/**
14
 * This controller processes the "Silent POST" requests send back from Payflow Link
15
 * after a payment has been processed.
16
 */
17
class ResultController extends Controller
18
{
19
    private $api;
20
21
    public function __construct(AlmaApi $api)
22
    {
23
        $this->api = $api;
24
    }
25
26
    /**
27
     * Process a "Silent POST" request from Payflow Link and updated the status of
28
     * the transaction within the Payments Application and the fees in Alma.
29
     *
30
     * See "Data Returned by the Post and Silent Post Features" on page 56 of the
31
     * Payflow Link User's Guide (https://www.paypalobjects.com/webstatic/en_US/developer/docs/pdf/pp_payflowlink_guide.pdf)
32
     * for more information.
33
     *
34
     * @Route("/result", name="result")
35
     * @param Request $request
36
     * @return Response
37
     */
38
    public function result(Request $request)
39
    {
40
        //No result code in the request
41
        $resultCode = $request->request->get('RESULT');
42
        if (is_null($resultCode)) {
43
            return new Response('Missing result code', Response::HTTP_BAD_REQUEST);
44
        }
45
46
        //Cannot find the transaction in the database
47
        $invoiceNumber = $request->request->get('INVOICE');
48
        $transaction = $this->getDoctrine()->getRepository(Transaction::class)->findOneBy(['invoice_number' => $invoiceNumber]);
49
        if (!$transaction) {
50
            return new Response('Cannot find the transaction', Response::HTTP_BAD_REQUEST);
51
        }
52
53
        //The transaction is already paid or updated.
54
        $status = $transaction->getStatus();
55
        if ($status === Transaction::STATUS_PAID || $status === Transaction::STATUS_COMPLETED) {
56
            return new Response('The transaction is completed.', Response::HTTP_BAD_REQUEST);
57
        }
58
59
        //Amount does not match.
60
        $entityManager = $this->getDoctrine()->getManager();
61
        if ($transaction->getTotalBalance() != $request->request->get('AMOUNT')) {
62
            $transaction->setStatus(Transaction::STATUS_ERROR);
63
            $entityManager->persist($transaction);
64
            $entityManager->flush();
65
            return new Response('Invalid amount', Response::HTTP_BAD_REQUEST);
66
        }
67
68
        //Communication error
69
        if ($resultCode < 0) {
70
            return new Response('Communication error', Response::HTTP_OK);
71
        }
72
73
        //The transaction is declined on Payflow.
74
        if ($resultCode > 0) {
75
            $transaction->setStatus(Transaction::STATUS_DECLINED);
76
            $entityManager->persist($transaction);
77
            $entityManager->flush();
78
            return new Response('Declined by Payflow', Response::HTTP_OK);
79
        }
80
81
        //The transaction is declined by PayPal due to AVS or CSC check failed.
82
        $responseMessage = $request->request->get('RESPMSG');
83
        if ($resultCode == 0 && ($responseMessage == 'AVSDECLINED' || $responseMessage == 'CSCDECLINED')) {
84
            $transaction->setStatus(Transaction::STATUS_DECLINED);
85
            $entityManager->persist($transaction);
86
            $entityManager->flush();
87
            return new Response('Declined by Payflow', Response::HTTP_OK);
88
        }
89
90
        $transaction->setStatus(Transaction::STATUS_PAID);
91
92
        if ($this->updateFeesOnAlma($transaction)) {
93
            $transaction->setStatus(Transaction::STATUS_COMPLETED);
94
        } else {
95
            $transaction->setStatus(Transaction::STATUS_FAILED);
96
        }
97
98
        $entityManager->persist($transaction);
99
        $entityManager->flush();
100
101
        return new Response("Success", Response::HTTP_OK);
102
    }
103
104
    /**
105
     * Update the fees in a given transaction using the Alma API.
106
     *
107
     * @param Transaction $transaction
108
     * @return $result -- true if the update succeeded, false otherwise.
0 ignored issues
show
Documentation Bug introduced by
The doc comment $result at position 0 could not be parsed: Unknown type name '$result' at position 0 in $result.
Loading history...
109
     */
110
    private function updateFeesOnAlma(Transaction $transaction)
111
    {
112
        $result = false;
113
114
        $fees = $transaction->getFees();
115
        foreach ($fees as $fee) {
116
            try {
117
                $this->api->payUserFee($transaction->getUserId(), $fee->getFeeId(), $fee->getBalance());
118
                $result = true;
119
            } catch (\GuzzleHttp\Exception\GuzzleException $e) {
120
                echo $e->getCode() . $e->getMessage();
121
            }
122
        }
123
124
        return $result;
125
    }
126
}
127