|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\AuthorizenetAcceptjs\Customer; |
| 9 | + |
| 10 | +use Magento\Framework\Registry; |
| 11 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 14 | +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test setting payment method and placing order with AuthorizenetAcceptjs |
| 20 | + */ |
| 21 | +class SetPaymentMethodTest extends GraphQlAbstract |
| 22 | +{ |
| 23 | + private const VALID_DESCRIPTOR = 'COMMON.ACCEPT.INAPP.PAYMENT'; |
| 24 | + private const VALID_NONCE = 'fake-nonce'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var CustomerTokenServiceInterface |
| 28 | + */ |
| 29 | + private $customerTokenService; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 33 | + */ |
| 34 | + private $getMaskedQuoteIdByReservedOrderId; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CollectionFactory |
| 38 | + */ |
| 39 | + private $orderCollectionFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var OrderRepositoryInterface |
| 43 | + */ |
| 44 | + private $orderRepository; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Registry |
| 48 | + */ |
| 49 | + private $registry; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + protected function setUp() |
| 55 | + { |
| 56 | + $objectManager = Bootstrap::getObjectManager(); |
| 57 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 58 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 59 | + $this->orderCollectionFactory = $objectManager->get(CollectionFactory::class); |
| 60 | + $this->orderRepository = $objectManager->get(OrderRepositoryInterface::class); |
| 61 | + $this->registry = Bootstrap::getObjectManager()->get(Registry::class); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 66 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 67 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php |
| 68 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 69 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 70 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 71 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 72 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php |
| 73 | + * @magentoApiDataFixture Magento/Graphql/AuthorizenetAcceptjs/_files/enable_authorizenetacceptjs.php |
| 74 | + * @param string $nonce |
| 75 | + * @param string $descriptor |
| 76 | + * @param bool $expectSuccess |
| 77 | + * @dataProvider dataProviderTestPlaceOrder |
| 78 | + */ |
| 79 | + public function testPlaceOrder(string $nonce, string $descriptor, bool $expectSuccess) |
| 80 | + { |
| 81 | + $reservedOrderId = 'test_quote'; |
| 82 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId); |
| 83 | + |
| 84 | + $setPaymentMutation = $this->getSetPaymentMutation($maskedQuoteId, $descriptor, $nonce); |
| 85 | + $setPaymentResponse = $this->graphQlMutation($setPaymentMutation, [], '', $this->getHeaderMap()); |
| 86 | + |
| 87 | + $this->assertSetPaymentMethodResponse($setPaymentResponse, 'authorizenet_acceptjs'); |
| 88 | + |
| 89 | + $placeOrderQuery = $this->getPlaceOrderMutation($maskedQuoteId); |
| 90 | + |
| 91 | + if (!$expectSuccess) { |
| 92 | + $this->expectException(\Exception::class); |
| 93 | + $this->expectExceptionMessage('Transaction has been declined. Please try again later.'); |
| 94 | + } |
| 95 | + |
| 96 | + $placeOrderResponse = $this->graphQlMutation($placeOrderQuery, [], '', $this->getHeaderMap()); |
| 97 | + |
| 98 | + $this->assertPlaceOrderResponse($placeOrderResponse, $reservedOrderId); |
| 99 | + } |
| 100 | + |
| 101 | + public function dataProviderTestPlaceOrder(): array |
| 102 | + { |
| 103 | + return [ |
| 104 | + [static::VALID_NONCE, static::VALID_DESCRIPTOR, true], |
| 105 | + ['nonce', static::VALID_DESCRIPTOR, false], |
| 106 | + [static::VALID_NONCE, 'descriptor', false], |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + private function assertPlaceOrderResponse(array $response, string $reservedOrderId): void |
| 111 | + { |
| 112 | + self::assertArrayHasKey('placeOrder', $response); |
| 113 | + self::assertArrayHasKey('order', $response['placeOrder']); |
| 114 | + self::assertArrayHasKey('order_id', $response['placeOrder']['order']); |
| 115 | + self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_id']); |
| 116 | + } |
| 117 | + |
| 118 | + private function assertSetPaymentMethodResponse(array $response, string $methodCode): void |
| 119 | + { |
| 120 | + self::assertArrayHasKey('setPaymentMethodOnCart', $response); |
| 121 | + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); |
| 122 | + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); |
| 123 | + self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']); |
| 124 | + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Create setPaymentMethodOnCart mutation |
| 129 | + * |
| 130 | + * @param string $maskedQuoteId |
| 131 | + * @param string $descriptor |
| 132 | + * @param string $nonce |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + private function getSetPaymentMutation(string $maskedQuoteId, string $descriptor, string $nonce): string |
| 136 | + { |
| 137 | + return <<<QUERY |
| 138 | +mutation { |
| 139 | + setPaymentMethodOnCart(input:{ |
| 140 | + cart_id:"{$maskedQuoteId}" |
| 141 | + payment_method:{ |
| 142 | + code:"authorizenet_acceptjs" |
| 143 | + authorizenet_acceptjs:{ |
| 144 | + opaque_data_descriptor: "{$descriptor}" |
| 145 | + opaque_data_value: "{$nonce}" |
| 146 | + cc_last_4: 1111 |
| 147 | + } |
| 148 | + } |
| 149 | + }) { |
| 150 | + cart { |
| 151 | + selected_payment_method { |
| 152 | + code |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | +QUERY; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Create placeOrder mutation |
| 162 | + * |
| 163 | + * @param string $maskedQuoteId |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + private function getPlaceOrderMutation(string $maskedQuoteId): string |
| 167 | + { |
| 168 | + return <<<QUERY |
| 169 | +mutation { |
| 170 | + placeOrder(input: {cart_id: "{$maskedQuoteId}"}) { |
| 171 | + order { |
| 172 | + order_id |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +QUERY; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Get authorization headers for requests |
| 181 | + * |
| 182 | + * @param string $username |
| 183 | + * @param string $password |
| 184 | + * @return array |
| 185 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 186 | + */ |
| 187 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 188 | + { |
| 189 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 190 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 191 | + return $headerMap; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @inheritdoc |
| 196 | + */ |
| 197 | + public function tearDown() |
| 198 | + { |
| 199 | + $this->registry->unregister('isSecureArea'); |
| 200 | + $this->registry->register('isSecureArea', true); |
| 201 | + |
| 202 | + $orderCollection = $this->orderCollectionFactory->create(); |
| 203 | + foreach ($orderCollection as $order) { |
| 204 | + $this->orderRepository->delete($order); |
| 205 | + } |
| 206 | + $this->registry->unregister('isSecureArea'); |
| 207 | + $this->registry->register('isSecureArea', false); |
| 208 | + |
| 209 | + parent::tearDown(); |
| 210 | + } |
| 211 | +} |
0 commit comments