Issues (23)

src/Bridge/IyzicoBridge.php (5 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eres\SyliusIyzicoPlugin\Bridge;
6
7
use GuzzleHttp\ClientInterface;
8
use Symfony\Contracts\Translation\TranslatorInterface;
9
10
final class IyzicoBridge implements IyzicoBridgeInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $apiKey;
16
17
    /**
18
     * @var string
19
     */
20
    private $secretKey;
21
22
    /**
23
     * @var string
24
     */
25
    private $environment = self::SANDBOX_ENVIRONMENT;
26
27
    /**
28
     * @var ClientInterface
29
     */
30
    private $client;
31
32
33
    public function setAuthorizationData(
34
        string $apiKey,
35
        string $secretKey,
36
        string $environment = self::SANDBOX_ENVIRONMENT
37
    ): void
38
    {
39
        $this->apiKey = $apiKey;
40
        $this->secretKey = $secretKey;
41
        $this->environment = $environment;
42
43
        $this->client = new \Iyzipay\Options();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Iyzipay\Options() of type Iyzipay\Options is incompatible with the declared type GuzzleHttp\ClientInterface of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        $this->client->setApiKey($this->apiKey);
45
        $this->client->setSecretKey($this->secretKey);
46
        $this->client->setBaseUrl($this->getHostForEnvironment());
47
    }
48
49
    public function create($data)
50
    {
51
        $card = $data['iyzico_card'];
52
        $customer = $data['iyzico_customer'];
53
        $shippingAddress = $data['iyzico_shipping_address'];
54
        $billingAddress = $data['iyzico_billing_address'];
55
        $basketItems = $data['iyzico_items'];
56
        $total = $data['iyzico_total'];
57
        $shipment = $data['iyzico_shipment'];
58
        $basketItems = array_merge($basketItems, $shipment);
59
        $orderNumber = $data['iyzico_order_number'];
60
        $orderId = $data['iyzico_id'];
61
        $localCode = $data['iyzico_local_code'];
62
        $currencyCode = $data['iyzico_currency_code'];
63
        $threeds = $data['iyzico_threeds'];
64
65
        $request = new \Iyzipay\Request\CreatePaymentRequest();
66
        $request->setLocale($localCode);
67
        $request->setConversationId($orderId);
68
        $request->setPrice($total);
69
        $request->setPaidPrice($total);
70
        $request->setCurrency($currencyCode);
71
        $request->setInstallment(1);
72
        $request->setBasketId($orderNumber);
73
        $request->setPaymentChannel(\Iyzipay\Model\PaymentChannel::WEB);
74
        $request->setPaymentGroup(\Iyzipay\Model\PaymentGroup::PRODUCT);
75
        $request->setPaymentCard($this->setPaymentCard($card));
76
        $request->setBuyer($this->setBuyer($customer));
77
        $request->setShippingAddress($this->setShippingAddress($shippingAddress));
78
        $request->setBillingAddress($this->setBillingAddress($billingAddress));
79
        $request->setBasketItems($this->setBasketItems($basketItems));
80
81
        $payment = null;
82
83
        if ($threeds) {
84
            $request->setCallbackUrl($data['iyzico_target_url']);
85
            $payment = \Iyzipay\Model\ThreedsInitialize::create($request, $this->client);
0 ignored issues
show
$this->client of type GuzzleHttp\ClientInterface is incompatible with the type Iyzipay\Options expected by parameter $options of Iyzipay\Model\ThreedsInitialize::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            $payment = \Iyzipay\Model\ThreedsInitialize::create($request, /** @scrutinizer ignore-type */ $this->client);
Loading history...
86
        } else {
87
            $payment = \Iyzipay\Model\Payment::create($request, $this->client);
0 ignored issues
show
$this->client of type GuzzleHttp\ClientInterface is incompatible with the type Iyzipay\Options expected by parameter $options of Iyzipay\Model\Payment::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            $payment = \Iyzipay\Model\Payment::create($request, /** @scrutinizer ignore-type */ $this->client);
Loading history...
88
        }
89
90
        $result = [
91
            'status' => $payment->getStatus(),
92
            'error_code' => $payment->getErrorCode(),
93
            'error_message' => $payment->getErrorMessage(),
94
        ];
95
        if ($threeds) {
96
            $result['html_content'] = $payment->getHtmlContent();
0 ignored issues
show
The method getHtmlContent() does not exist on Iyzipay\Model\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            /** @scrutinizer ignore-call */ 
97
            $result['html_content'] = $payment->getHtmlContent();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        }
98
        return $result;
99
100
    }
101
102
    private function setPaymentCard($card)
103
    {
104
        $paymentCard = new \Iyzipay\Model\PaymentCard();
105
        $paymentCard->setCardHolderName($card->getHolder());
106
        $paymentCard->setCardNumber($card->getNumber());
107
        $paymentCard->setExpireMonth($card->getExpireAt()->format('m'));
108
        $paymentCard->setExpireYear($card->getExpireAt()->format('y'));
109
        $paymentCard->setCvc($card->getSecurityCode());
110
        $paymentCard->setRegisterCard(0);
111
        return $paymentCard;
112
    }
113
114
    private function setBuyer($customer)
115
    {
116
        $buyer = new \Iyzipay\Model\Buyer();
117
        $buyer->setId($customer['id']);
118
        $buyer->setName($customer['firstname']);
119
        $buyer->setSurname($customer['lastname']);
120
        $buyer->setGsmNumber($customer['phone_number']);
121
        $buyer->setEmail($customer['email']);
122
        $buyer->setIdentityNumber($customer['identity_number']);
123
        $buyer->setLastLoginDate($customer['last_login_date'] ? $customer['last_login_date']->format('Y-m-d H:i:s') : null);
124
        $buyer->setRegistrationDate($customer['registration_date']->format('Y-m-d H:i:s'));
125
        $buyer->setRegistrationAddress($customer['street']);
126
        $buyer->setIp($customer['ip']);
127
        $buyer->setCity($customer['city']);
128
        $buyer->setCountry($customer['country_code']);
129
        $buyer->setZipCode($customer['postcode']);
130
131
        return $buyer;
132
    }
133
134
    private function setShippingAddress($address)
135
    {
136
        $shippingAddress = new \Iyzipay\Model\Address();
137
        $shippingAddress->setContactName($address['firstname'] . " " . $address['lastname']);
138
        $shippingAddress->setCity($address['city']);
139
        $shippingAddress->setCountry($address['country_code']);
140
        $shippingAddress->setAddress($address['street']);
141
        $shippingAddress->setZipCode($address['postcode']);
142
        return $shippingAddress;
143
    }
144
145
    private function setBillingAddress($address)
146
    {
147
        $billingAddress = new \Iyzipay\Model\Address();
148
        $billingAddress->setContactName($address['firstname'] . " " . $address['lastname']);
149
        $billingAddress->setCity($address['city']);
150
        $billingAddress->setCountry($address['country_code']);
151
        $billingAddress->setAddress($address['street']);
152
        $billingAddress->setZipCode($address['postcode']);
153
        return $billingAddress;
154
    }
155
156
    private function setBasketItems($items)
157
    {
158
        $basketItems = [];
159
        foreach ($items as $item) {
160
            if ($item['total'] > 0) {
161
                $basketItem = new \Iyzipay\Model\BasketItem();
162
                $basketItem->setId($item['id']);
163
                $basketItem->setName($item['name']);
164
                $basketItem->setCategory1($item['category']);
165
                $basketItem->setItemType(\Iyzipay\Model\BasketItemType::PHYSICAL);
166
                $basketItem->setPrice($item['total']);
167
                $basketItems[] = $basketItem;
168
            }
169
        }
170
        return $basketItems;
171
    }
172
173
    public function getHostForEnvironment(): string
174
    {
175
        return
176
            self::SANDBOX_ENVIRONMENT === $this->environment
177
                ? self::SANDBOX_HOST : self::PRODUCTION_HOST;
178
    }
179
180
    public function createThreeds($data)
181
    {
182
        $request = new \Iyzipay\Request\CreateThreedsPaymentRequest();
183
        $request->setLocale($data['iyzico_local_code']);
184
        $request->setConversationId($data['conversationId']);
185
        $request->setPaymentId($data['paymentId']);
186
        $request->setConversationData($data['conversationData']);
187
188
        $payment = \Iyzipay\Model\ThreedsPayment::create($request, $this->client);
0 ignored issues
show
$this->client of type GuzzleHttp\ClientInterface is incompatible with the type Iyzipay\Options expected by parameter $options of Iyzipay\Model\ThreedsPayment::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
        $payment = \Iyzipay\Model\ThreedsPayment::create($request, /** @scrutinizer ignore-type */ $this->client);
Loading history...
189
190
        return [
191
            'status' => $payment->getStatus(),
192
            'error_code' => $payment->getErrorCode(),
193
            'error_message' => $payment->getErrorMessage(),
194
        ];
195
    }
196
197
}
198