|
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
|
|
|
class ResultController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $almaApi; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(AlmaApi $api) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->almaApi = $api; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @Route("/result", name="result") |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* @return Response |
|
26
|
|
|
*/ |
|
27
|
|
|
public function result(Request $request) |
|
28
|
|
|
{ |
|
29
|
|
|
//No result code in the request |
|
30
|
|
|
$resultCode = $request->request->get('RESULT'); |
|
31
|
|
|
if (is_null($resultCode)) { |
|
32
|
|
|
return new Response('Missing result code', Response::HTTP_BAD_REQUEST); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
//Cannot find the transaction in the database |
|
36
|
|
|
$invoiceNumber = $request->request->get('INVOICE'); |
|
37
|
|
|
$transaction = $this->getDoctrine()->getRepository(Transaction::class)->findOneBy(['invoice_number' => $invoiceNumber]); |
|
38
|
|
|
if (!$transaction) { |
|
39
|
|
|
return new Response('Cannot find the transaction', Response::HTTP_BAD_REQUEST); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
//The transaction is already paid or updated. |
|
43
|
|
|
$status = $transaction->getStatus(); |
|
44
|
|
|
if ($status === Transaction::STATUS_PAID || $status === Transaction::STATUS_COMPLETED) { |
|
45
|
|
|
return new Response('The transaction is completed.', Response::HTTP_BAD_REQUEST); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//Amount does not match. |
|
49
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
50
|
|
|
if ($transaction->getTotalBalance() != $request->request->get('AMOUNT')) { |
|
51
|
|
|
$transaction->setStatus(Transaction::STATUS_ERROR); |
|
52
|
|
|
$entityManager->persist($transaction); |
|
53
|
|
|
$entityManager->flush(); |
|
54
|
|
|
return new Response('Invalid amount', Response::HTTP_BAD_REQUEST); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
//Communication error |
|
58
|
|
|
if ($resultCode < 0) { |
|
59
|
|
|
return new Response('Communication error', Response::HTTP_OK); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//The transaction is declined on Payflow. |
|
63
|
|
|
if ($resultCode > 0) { |
|
64
|
|
|
$transaction->setStatus(Transaction::STATUS_DECLINED); |
|
65
|
|
|
$entityManager->persist($transaction); |
|
66
|
|
|
$entityManager->flush(); |
|
67
|
|
|
return new Response('Declined by Payflow', Response::HTTP_OK); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$transaction->setStatus(Transaction::STATUS_PAID); |
|
71
|
|
|
|
|
72
|
|
|
if ($this->updateFeesOnAlma($transaction)) { |
|
73
|
|
|
$transaction->setStatus(Transaction::STATUS_COMPLETED); |
|
74
|
|
|
} else { |
|
75
|
|
|
$transaction->setStatus(Transaction::STATUS_FAILED); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$entityManager->persist($transaction); |
|
79
|
|
|
$entityManager->flush(); |
|
80
|
|
|
|
|
81
|
|
|
return new Response("Success", Response::HTTP_OK); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function updateFeesOnAlma(Transaction $transaction) |
|
85
|
|
|
{ |
|
86
|
|
|
$result = false; |
|
87
|
|
|
|
|
88
|
|
|
$fees = $transaction->getFees(); |
|
89
|
|
|
foreach ($fees as $fee) { |
|
90
|
|
|
try { |
|
91
|
|
|
$this->almaApi->payUserFee($transaction->getUserId(), $fee->getFeeId(), $fee->getBalance()); |
|
92
|
|
|
$result = true; |
|
93
|
|
|
} catch (\GuzzleHttp\Exception\GuzzleException $e) { |
|
94
|
|
|
echo $e->getCode() . $e->getMessage(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|