PaypalResponseMapper::addPaymentTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 21
rs 9.7998
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Braintree\Model\Mapper\PaypalResponse;
9
10
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\BraintreePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\CountryCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...untryCollectionTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\CountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CountryTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...SuccessResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
19
use SprykerEco\Shared\Braintree\BraintreeConfig;
20
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface;
21
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface;
22
23
class PaypalResponseMapper implements PaypalResponseMapperInterface
24
{
25
    protected const KEY_NONCE = 'nonce';
26
    protected const KEY_DETAILS = 'details';
27
    protected const KEY_EMAIL = 'email';
28
    protected const KEY_FIRST_NAME = 'firstName';
29
    protected const KEY_LAST_NAME = 'lastName';
30
    protected const KEY_PAYER_ID = 'payerId';
31
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
32
    protected const KEY_RECIPIENT_NAME = 'recipientName';
33
    protected const KEY_LINE1 = 'line1';
34
    protected const KEY_CITY = 'city';
35
    protected const KEY_STATE = 'state';
36
    protected const KEY_POSTAL_CODE = 'postalCode';
37
    protected const KEY_COUNTRY_CODE = 'countryCode';
38
    protected const KEY_CURRENCY = 'currency';
39
    protected const KEY_AMOUNT = 'amount';
40
41
    /**
42
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface
43
     */
44
    protected $paymentClient;
45
46
    /**
47
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface
48
     */
49
    protected $countryClient;
50
51
    /**
52
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
53
     */
54
    protected $moneyPlugin;
55
56
    /**
57
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface $paymentClient
58
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface $countryClient
59
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
60
     */
61
    public function __construct(
62
        BraintreeToPaymentClientInterface $paymentClient,
63
        BraintreeToCountryClientInterface $countryClient,
64
        MoneyPluginInterface $moneyPlugin
65
    ) {
66
        $this->paymentClient = $paymentClient;
67
        $this->countryClient = $countryClient;
68
        $this->moneyPlugin = $moneyPlugin;
69
    }
70
71
    /**
72
     * @param array $payload
73
     *
74
     * @return \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer
75
     */
76
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
77
    {
78
        $transfer = new PaypalExpressSuccessResponseTransfer();
79
80
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
81
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
82
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
83
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
84
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
85
        $transfer->setRecipientName($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
86
        $transfer->setLine1($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
87
        $transfer->setCity($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
88
        $transfer->setState($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
89
        $transfer->setPostalCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
90
        $transfer->setCountryCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
91
        $transfer->setCurrency($payload[static::KEY_CURRENCY] ?? null);
92
        $transfer->setAmount($payload[static::KEY_AMOUNT] ?? null);
93
94
        return $transfer;
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
99
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
100
     *
101
     * @return \Generated\Shared\Transfer\QuoteTransfer
102
     */
103
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
104
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
105
        QuoteTransfer $quoteTransfer
106
    ): QuoteTransfer {
107
        $quoteTransfer = $this->addCustomer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
108
        $quoteTransfer = $this->addShippingAddress($quoteTransfer, $paypalExpressSuccessResponseTransfer);
109
        $quoteTransfer = $this->addBillingTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
110
        $quoteTransfer = $this->addPaymentTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
111
112
        return $quoteTransfer;
113
    }
114
115
    /**
116
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
117
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
118
     *
119
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
120
     */
121
    protected function addPaymentTransfer(
122
        QuoteTransfer $quoteTransfer,
123
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
124
    ): QuoteTransfer {
125
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
126
        $brainTreePaymentTransfer->setNonce($paypalExpressSuccessResponseTransfer->getNonce());
127
        $brainTreePaymentTransfer->setBillingAddress($quoteTransfer->getBillingAddress());
128
        $brainTreePaymentTransfer->setLanguageIso2Code(mb_strtolower($quoteTransfer->getBillingAddress()->getIso2Code()));
129
        $brainTreePaymentTransfer->setCurrencyIso3Code($paypalExpressSuccessResponseTransfer->getCurrency());
130
        $brainTreePaymentTransfer->setEmail($quoteTransfer->getShippingAddress()->getEmail());
131
132
        $paymentTransfer = new PaymentTransfer();
133
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
134
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
135
        $paymentTransfer->setBraintree($brainTreePaymentTransfer);
136
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
137
        $paymentTransfer->setAmount($this->moneyPlugin->convertDecimalToInteger($paypalExpressSuccessResponseTransfer->getAmount()));
138
139
        $quoteTransfer->setPayment($paymentTransfer);
140
141
        return $quoteTransfer;
142
    }
143
144
    /**
145
     * @param string $iso2Code
146
     *
147
     * @return \Generated\Shared\Transfer\CountryTransfer|null
148
     */
149
    protected function findCountryTransfer(string $iso2Code): ?CountryTransfer
150
    {
151
        $countryTransfer = (new CountryTransfer())
152
            ->setIso2Code($iso2Code);
153
154
        $countryCollectionTransfer = new CountryCollectionTransfer();
155
        $countryCollectionTransfer->addCountries($countryTransfer);
156
        $countryCollectionTransfer = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
157
158
        $countryTransfers = $countryCollectionTransfer->getCountries();
159
160
        if ($countryTransfers === null) {
161
            return null;
162
        }
163
164
        $countryTransfersArrayCopy = $countryTransfers->getArrayCopy();
165
        $countryTransfer = array_shift($countryTransfersArrayCopy);
166
167
        return $countryTransfer;
168
    }
169
170
    /**
171
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
172
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
173
     *
174
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
175
     */
176
    protected function addShippingAddress(
177
        QuoteTransfer $quoteTransfer,
178
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
179
    ): QuoteTransfer {
180
        $quoteTransfer->setShippingAddress($this->getAddressTransfer($paypalExpressSuccessResponseTransfer));
181
182
        return $quoteTransfer;
183
    }
184
185
    /**
186
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
187
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
188
     *
189
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
190
     */
191
    protected function addBillingTransfer(
192
        QuoteTransfer $quoteTransfer,
193
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
194
    ): QuoteTransfer {
195
        $quoteTransfer->setBillingAddress($this->getAddressTransfer($paypalExpressSuccessResponseTransfer));
196
197
        return $quoteTransfer;
198
    }
199
200
    /**
201
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
202
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
203
     *
204
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
205
     */
206
    protected function addCustomer(
207
        QuoteTransfer $quoteTransfer,
208
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
209
    ): QuoteTransfer {
210
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
211
212
        $customerTransfer->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
213
        $customerTransfer->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
214
        $customerTransfer->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
215
216
        if ($quoteTransfer->getCustomer()->getCustomerReference()) {
217
            $customerTransfer->setCustomerReference($quoteTransfer->getCustomer()->getCustomerReference());
218
        }
219
220
        if (!$customerTransfer->getIdCustomer()) {
221
            $customerTransfer->setIsGuest(true);
222
        }
223
224
        $quoteTransfer->setCustomer($customerTransfer);
225
226
        return $quoteTransfer;
227
    }
228
229
    /**
230
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
231
     *
232
     * @return \Generated\Shared\Transfer\AddressTransfer
233
     */
234
    protected function getAddressTransfer(
235
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
236
    ): AddressTransfer {
237
        $addressTransfer = new AddressTransfer();
238
        $addressTransfer->fromArray($paypalExpressSuccessResponseTransfer->toArray(), true);
239
        $addressTransfer->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
240
        $addressTransfer->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
241
242
        $countryTransfer = $this->findCountryTransfer($paypalExpressSuccessResponseTransfer->getCountryCode());
243
        if ($countryTransfer) {
244
            $addressTransfer->setCountry($countryTransfer);
245
            $addressTransfer->setIso2Code($countryTransfer->getIso2Code());
246
        }
247
248
        return $addressTransfer;
249
    }
250
}
251
252