Passed
Pull Request — master (#9)
by Volodymyr
05:48
created

mapSuccessResponseToPaypalExpressSuccessResponseTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
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
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
108
        $shippingAddressTransfer = $quoteTransfer->getShippingAddress() ?? new AddressTransfer();
109
        $billingAddressTransfer = $quoteTransfer->getBillingAddress() ?? new AddressTransfer();
110
        $countryTransfer = $this->getCountryTransfer($paypalExpressSuccessResponseTransfer->getCountryCode());
111
112
        $quoteTransfer->setCustomer($customerTransfer);
113
        $quoteTransfer->setShippingAddress($shippingAddressTransfer);
114
        $quoteTransfer->setBillingAddress($billingAddressTransfer);
115
116
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
117
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
118
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
119
120
        $quoteTransfer->getShippingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
121
        $quoteTransfer->getShippingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
122
        $quoteTransfer->getShippingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
123
        $quoteTransfer->getShippingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
124
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
125
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
126
        $quoteTransfer->getShippingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
127
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
128
        $quoteTransfer->getShippingAddress()->setCountry($countryTransfer);
129
        $quoteTransfer->getShippingAddress()->setIso2Code($countryTransfer->getIso2Code());
130
131
        $quoteTransfer->getBillingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
132
        $quoteTransfer->getBillingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
133
        $quoteTransfer->getBillingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
134
        $quoteTransfer->getBillingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
135
        $quoteTransfer->getBillingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
136
        $quoteTransfer->getBillingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
137
        $quoteTransfer->getBillingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
138
        $quoteTransfer->getBillingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
139
        $quoteTransfer->getBillingAddress()->setCountry($countryTransfer);
140
        $quoteTransfer->getBillingAddress()->setIso2Code($countryTransfer->getIso2Code());
141
142
        $this->addPaymentTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
143
144
        return $quoteTransfer;
145
    }
146
147
    /**
148
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
149
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
150
     *
151
     * @return void
152
     */
153
    protected function addPaymentTransfer(
154
        QuoteTransfer $quoteTransfer,
155
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
156
    ): void {
157
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
158
        $brainTreePaymentTransfer->setNonce($paypalExpressSuccessResponseTransfer->getNonce());
159
        $brainTreePaymentTransfer->setBillingAddress($quoteTransfer->getBillingAddress());
160
        $brainTreePaymentTransfer->setLanguageIso2Code(strtolower($quoteTransfer->getBillingAddress()->getIso2Code()));
161
        $brainTreePaymentTransfer->setCurrencyIso3Code($paypalExpressSuccessResponseTransfer->getCurrency());
162
        $brainTreePaymentTransfer->setEmail($quoteTransfer->getShippingAddress()->getEmail());
163
164
        $paymentTransfer = new PaymentTransfer();
165
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
166
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
167
        $paymentTransfer->setBraintree($brainTreePaymentTransfer);
168
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
169
        $paymentTransfer->setAmount($this->moneyPlugin->convertDecimalToInteger($paypalExpressSuccessResponseTransfer->getAmount()));
170
171
        $quoteTransfer->setPayment($paymentTransfer);
172
    }
173
174
    /**
175
     * @param string $iso2Code
176
     *
177
     * @return \Generated\Shared\Transfer\CountryTransfer|null
178
     */
179
    protected function getCountryTransfer(string $iso2Code): ?CountryTransfer
180
    {
181
        $countryTransfer = (new CountryTransfer())
182
            ->setIso2Code($iso2Code);
183
184
        $countryCollectionTransfer = new CountryCollectionTransfer();
185
        $countryCollectionTransfer->addCountries($countryTransfer);
186
187
        $countries = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
188
189
        foreach ($countries->getCountries() as $country) {
190
            return $country;
191
        }
192
193
        return null;
194
    }
195
}
196