MollieDriver::execute()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 4
nop 3
1
<?php
2
/*
3
* (c) Wessel Strengholt <[email protected]>
4
*
5
* For the full copyright and license information, please view the LICENSE
6
* file that was distributed with this source code.
7
*/
8
9
namespace Usoft\IDealBundle\Driver;
10
11
use Mollie_API_Client;
12
use Mollie_API_Object_Method;
13
use Usoft\IDealBundle\Model\Bank;
14
use Usoft\IDealBundle\Exceptions\BankLoaderException;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Usoft\IDealBundle\Exceptions\IDealExecuteException;
17
use Usoft\IDealBundle\Exceptions\InvalidPaymentKeyException;
18
19
/**
20
 * Class MollieDriver
21
 *
22
 * @author Wessel Strengholt <[email protected]>
23
 */
24
class MollieDriver implements IDealInterface
25
{
26
    /** @var string */
27
    private $description;
28
29
    /** @var Mollie_API_Client */
30
    private $mollie;
31
32
    /**
33
     * @param Mollie_API_Client $client
34
     * @param string            $key
35
     * @param string            $description
36
     */
37
    public function __construct(Mollie_API_Client $client, $key, $description)
38
    {
39
        $this->description = $description;
40
        $client->setApiKey($key);
41
        $this->mollie = $client;
42
    }
43
44
    /**
45
     * @return Bank[]
46
     */
47
    public function getBanks()
48
    {
49
        $bankList = [];
50
        try {
51
            $issuers = $this->mollie->issuers->all();
52
            foreach ($issuers as $issuer) {
53
                $bankList[] = new Bank($issuer->id, $issuer->name);
54
            }
55
56
            return $bankList;
57
        } catch (\Exception $exception) {
58
            throw new BankLoaderException('Cannot load issuers from mollie');
59
        }
60
    }
61
62
    /**
63
     * @param Bank   $bank
64
     * @param float  $amount
65
     * @param string $returnUrl
66
     *
67
     * @return RedirectResponse
68
     *
69
     * @throws IDealExecuteException
70
     */
71
    public function execute(Bank $bank, $amount, $returnUrl)
72
    {
73
        try {
74
            $payment = $this->mollie->payments->create([
75
                "amount"      => $amount,
76
                "description" => $this->description,
77
                "redirectUrl" => $returnUrl,
78
                "method"      => Mollie_API_Object_Method::IDEAL,
79
                "issuer"      => $bank->getId(),
80
            ]);
81
82
            file_put_contents($this->getFile(), $payment->id);
83
84
            return new RedirectResponse($payment->getPaymentUrl());
85
        } catch (\Exception $exception) {
86
            throw new IDealExecuteException($exception->getMessage());
87
        }
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function confirm()
94
    {
95
        try {
96
            $key = file_get_contents($this->getFile());
97
98
            return $this->mollie->payments->get($key)->isPaid();
99
        } catch (\Exception $exception) {
100
            return false;
101
        }
102
    }
103
104
    /**
105
     * @return string
106
     *
107
     * @throws InvalidPaymentKeyException
108
     */
109
    private function getFile()
110
    {
111
        try {
112
            return sys_get_temp_dir() . DIRECTORY_SEPARATOR . session_id();
113
        } catch (\Exception $exception) {
114
            throw new InvalidPaymentKeyException('Cannot resolve payment key');
115
        }
116
    }
117
}
118