|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright © Magento, Inc. All rights reserved. |
4 | | - * See COPYING.txt for license details. |
| 3 | + * Copyright 2020 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */ |
6 | 6 | declare(strict_types=1); |
7 | 7 |
|
8 | 8 | namespace Magento\Paypal\Model\Payflow; |
9 | 9 |
|
| 10 | +use Laminas\Http\Response; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
10 | 12 | use Magento\Checkout\Api\PaymentInformationManagementInterface; |
| 13 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 14 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 17 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 18 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
11 | 19 | use Magento\Framework\Api\SearchCriteriaBuilder; |
| 20 | +use Magento\Framework\HTTP\LaminasClient; |
| 21 | +use Magento\Framework\HTTP\LaminasClientFactory; |
| 22 | +use Magento\Payment\Model\MethodInterface; |
12 | 23 | use Magento\Paypal\Model\Config; |
| 24 | +use Magento\Paypal\Model\Payflow\Service\Gateway; |
13 | 25 | use Magento\Paypal\Model\Payflowpro; |
| 26 | +use Magento\Quote\Api\CartManagementInterface; |
14 | 27 | use Magento\Quote\Api\CartRepositoryInterface; |
15 | 28 | use Magento\Quote\Api\Data\CartInterface; |
| 29 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 30 | +use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture; |
16 | 31 | use Magento\Sales\Api\Data\TransactionInterface; |
17 | 32 | use Magento\Sales\Api\OrderManagementInterface; |
18 | 33 | use Magento\Sales\Api\OrderRepositoryInterface; |
19 | 34 | use Magento\Sales\Api\TransactionRepositoryInterface; |
20 | 35 | use Magento\Sales\Model\Order; |
| 36 | +use Magento\Store\Model\ScopeInterface; |
| 37 | +use Magento\Store\Test\Fixture\Store as StoreFixture; |
| 38 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 39 | +use Magento\TestFramework\Fixture\DataFixture; |
| 40 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 41 | +use Magento\TestFramework\Fixture\DbIsolation; |
21 | 42 | use Magento\TestFramework\Helper\Bootstrap; |
22 | 43 | use Magento\TestFramework\ObjectManager; |
| 44 | +use Magento\Vault\Model\Method\Vault; |
| 45 | +use Magento\Vault\Test\Fixture\PaymentToken as PaymentTokenFixture; |
23 | 46 | use PHPUnit\Framework\TestCase; |
24 | 47 |
|
25 | 48 | /** |
@@ -88,6 +111,78 @@ public function testPlaceOrderSuspectedFraud(): void |
88 | 111 | ); |
89 | 112 | } |
90 | 113 |
|
| 114 | + #[ |
| 115 | + DbIsolation(false), |
| 116 | + DataFixture(StoreFixture::class, ['code' => 'test_vault_store'], as: 'store2'), |
| 117 | + ConfigFixture('payment/payflowpro/active', '1', ScopeInterface::SCOPE_STORE, 'default'), |
| 118 | + ConfigFixture('payment/payflowpro/cctypes', '1', ScopeInterface::SCOPE_STORE, 'default'), |
| 119 | + ConfigFixture('payment/payflowpro/payment_action', 'Authorization', ScopeInterface::SCOPE_STORE, 'default'), |
| 120 | + ConfigFixture('payment/payflowpro/allowspecific', '1', ScopeInterface::SCOPE_STORE, 'default'), |
| 121 | + ConfigFixture('payment/payflowpro/specificcountry', 'US', ScopeInterface::SCOPE_STORE, 'default'), |
| 122 | + ConfigFixture('payment/payflowpro_cc_vault/active', '1', ScopeInterface::SCOPE_STORE, 'default'), |
| 123 | + // second store config |
| 124 | + ConfigFixture('payment/payflowpro/active', '1', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 125 | + ConfigFixture('payment/payflowpro/cctypes', '1', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 126 | + ConfigFixture('payment/payflowpro/payment_action', 'Sale', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 127 | + ConfigFixture('payment/payflowpro/allowspecific', '1', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 128 | + ConfigFixture('payment/payflowpro/specificcountry', 'UK', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 129 | + ConfigFixture('payment/payflowpro_cc_vault/active', '1', ScopeInterface::SCOPE_STORE, 'test_vault_store'), |
| 130 | + |
| 131 | + DataFixture(CustomerFixture::class, as: 'customer'), |
| 132 | + DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 133 | + DataFixture(ProductFixture::class, as:'product'), |
| 134 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']), |
| 135 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 136 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 137 | + DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']), |
| 138 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']), |
| 139 | + DataFixture( |
| 140 | + PaymentTokenFixture::class, |
| 141 | + ['customer_id' => '$customer.id$', 'payment_method_code' => 'payflowpro'], |
| 142 | + as: 'token' |
| 143 | + ), |
| 144 | + DataFixture( |
| 145 | + SetPaymentMethodFixture::class, |
| 146 | + [ |
| 147 | + 'cart_id' => '$cart.id$', |
| 148 | + 'method' => [ |
| 149 | + 'method' => 'payflowpro_cc_vault', |
| 150 | + 'additional_data' => [ |
| 151 | + 'public_hash' => '$token.public_hash$', |
| 152 | + 'customer_id' => '$customer.id$', |
| 153 | + ] |
| 154 | + ] |
| 155 | + ] |
| 156 | + ), |
| 157 | + ] |
| 158 | + public function testStoreConfiguration(): void |
| 159 | + { |
| 160 | + $fixtures = DataFixtureStorageManager::getStorage(); |
| 161 | + $cartRepository = $this->objectManager->get(CartRepositoryInterface::class); |
| 162 | + $cart = $fixtures->get('cart'); |
| 163 | + $store = $fixtures->get('store2'); |
| 164 | + $cart = $cartRepository->get($cart->getId()); |
| 165 | + $cart->setStoreId($store->getId()); |
| 166 | + $this->assertNotEmpty($cart->getPayment()->getMethod()); |
| 167 | + $this->assertEquals('payflowpro_cc_vault', $cart->getPayment()->getMethod()); |
| 168 | + $paymentMethodInstance = $cart->getPayment()->getMethodInstance(); |
| 169 | + $this->assertInstanceOf(Vault::class, $paymentMethodInstance); |
| 170 | + $this->assertTrue( |
| 171 | + $paymentMethodInstance->canUseForCountry('UK') |
| 172 | + ); |
| 173 | + $this->assertEquals( |
| 174 | + MethodInterface::ACTION_AUTHORIZE_CAPTURE, |
| 175 | + $paymentMethodInstance->getConfigPaymentAction() |
| 176 | + ); |
| 177 | + $this->mockPaymentGateway($cart); |
| 178 | + $cartManagement = $this->objectManager->create(CartManagementInterface::class); |
| 179 | + $orderId = $cartManagement->placeOrder($cart->getId()); |
| 180 | + $transactions = $this->getPaymentTransactionList((int) $orderId); |
| 181 | + $this->assertCount(1, $transactions); |
| 182 | + $transaction = array_pop($transactions); |
| 183 | + $this->assertEquals(TransactionInterface::TYPE_CAPTURE, $transaction->getTxnType()); |
| 184 | + } |
| 185 | + |
91 | 186 | /** |
92 | 187 | * Retrieves quote by provided order ID. |
93 | 188 | * |
@@ -164,4 +259,42 @@ private function getOrderComment(int $orderId): string |
164 | 259 |
|
165 | 260 | return $comment ? $comment->getComment() : ''; |
166 | 261 | } |
| 262 | + |
| 263 | + private function mockPaymentGateway(CartInterface $cart): Gateway |
| 264 | + { |
| 265 | + $responseArray = [ |
| 266 | + 'RESULT' => '0', |
| 267 | + 'pnref' => 'A30A3E5DDE34', |
| 268 | + 'respmsg' => 'Approved', |
| 269 | + 'authcode' => '421PNI', |
| 270 | + 'avsaddr' => 'N', |
| 271 | + 'avszip' => 'N', |
| 272 | + 'txid' => '011144228158206', |
| 273 | + 'hostcode' => 'A', |
| 274 | + 'procavs' => 'N', |
| 275 | + 'visacardlevel' => '12', |
| 276 | + 'transtime' => date('Y-m-d H:i:s'), |
| 277 | + 'firstname' => $cart->getBillingAddress()->getFirstname(), |
| 278 | + 'lastname' => $cart->getBillingAddress()->getLastname(), |
| 279 | + 'amt' => $cart->getShippingAddress()->getGrandTotal(), |
| 280 | + 'acct' => '1234', |
| 281 | + 'expdate' => date('Y-m-d H:i:s', strtotime('+1 hour')), |
| 282 | + 'cardtype' => '0', |
| 283 | + 'iavs' => 'N', |
| 284 | + 'result_code' => '0', |
| 285 | + ]; |
| 286 | + $clientFactory = $this->createMock(LaminasClientFactory::class); |
| 287 | + $client = $this->createMock(LaminasClient::class); |
| 288 | + $clientResponse = $this->createMock(Response::class); |
| 289 | + $clientFactory->method('create')->willReturn($client); |
| 290 | + $client->method('send')->willReturn($clientResponse); |
| 291 | + $clientResponse->method('getBody')->willReturn(http_build_query($responseArray)); |
| 292 | + $gatewayMock = $this->objectManager->get(Gateway::class); |
| 293 | + $reflection = new \ReflectionClass($gatewayMock); |
| 294 | + $property = $reflection->getProperty('httpClientFactory'); |
| 295 | + $property->setAccessible(true); |
| 296 | + $property->setValue($gatewayMock, $clientFactory); |
| 297 | + |
| 298 | + return $gatewayMock; |
| 299 | + } |
167 | 300 | } |
0 commit comments