|
| 1 | +<?php |
| 2 | + |
| 3 | +/** @noinspection PhpUnhandledExceptionInspection */ |
| 4 | +/** @noinspection PhpDocMissingThrowsInspection */ |
| 5 | +/** |
| 6 | + * This class defines integration tests to verify interface and |
| 7 | + * functionality of the authorization transaction type. |
| 8 | + * |
| 9 | + * @link https://docs.unzer.com/ |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +namespace UnzerSDK\test\integration\TransactionTypes; |
| 14 | + |
| 15 | +use UnzerSDK\Constants\RecurrenceTypes; |
| 16 | +use UnzerSDK\Resources\Metadata; |
| 17 | +use UnzerSDK\Resources\PaymentTypes\Card; |
| 18 | +use UnzerSDK\Resources\PaymentTypes\Paypal; |
| 19 | +use UnzerSDK\Resources\TransactionTypes\Authorization; |
| 20 | +use UnzerSDK\Resources\TransactionTypes\PreAuthorization; |
| 21 | +use UnzerSDK\test\BaseIntegrationTest; |
| 22 | + |
| 23 | +/** |
| 24 | + * @group CC-1576 |
| 25 | + */ |
| 26 | +class PreAuthorizationTest extends BaseIntegrationTest |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Verify Unzer object can perform an authorization based on the paymentTypeId. |
| 30 | + * |
| 31 | + * @test |
| 32 | + */ |
| 33 | + public function authorizeWithTypeId(): void |
| 34 | + { |
| 35 | + $paymentType = $this->unzer->createPaymentType($this->createCardObject()); |
| 36 | + $preauth = new PreAuthorization(100.0, 'EUR', self::RETURN_URL); |
| 37 | + $this->unzer->performAuthorization($preauth, $paymentType->getId()); |
| 38 | + $this->assertNotNull($preauth); |
| 39 | + $this->assertNotEmpty($preauth->getId()); |
| 40 | + $this->assertNotEmpty($preauth->getUniqueId()); |
| 41 | + $this->assertNotEmpty($preauth->getShortId()); |
| 42 | + |
| 43 | + $traceId = $preauth->getTraceId(); |
| 44 | + $this->assertNotEmpty($traceId); |
| 45 | + $this->assertSame($traceId, $preauth->getPayment()->getTraceId()); |
| 46 | + $this->assertPending($preauth); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Verify authorization produces Payment and Customer. |
| 51 | + * |
| 52 | + * @test |
| 53 | + */ |
| 54 | + public function authorizationProducesPaymentAndCustomer(): void |
| 55 | + { |
| 56 | + $paymentType = $this->unzer->createPaymentType($this->createCardObject()); |
| 57 | + |
| 58 | + $customer = $this->getMinimalCustomer(); |
| 59 | + $this->assertNull($customer->getId()); |
| 60 | + |
| 61 | + $preauth = new PreAuthorization(100.0, 'EUR', self::RETURN_URL); |
| 62 | + $this->unzer->performAuthorization($preauth, $paymentType, $customer); |
| 63 | + $payment = $preauth->getPayment(); |
| 64 | + $this->assertNotNull($payment); |
| 65 | + $this->assertNotNull($payment->getId()); |
| 66 | + |
| 67 | + $newCustomer = $payment->getCustomer(); |
| 68 | + $this->assertNotNull($newCustomer); |
| 69 | + $this->assertNotNull($newCustomer->getId()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Verify authorization with customer Id. |
| 74 | + * |
| 75 | + * @test |
| 76 | + * |
| 77 | + * @return Authorization |
| 78 | + */ |
| 79 | + public function authorizationWithCustomerId(): Authorization |
| 80 | + { |
| 81 | + $paymentType = $this->unzer->createPaymentType($this->createCardObject()); |
| 82 | + |
| 83 | + $customerId = $this->unzer->createCustomer($this->getMinimalCustomer())->getId(); |
| 84 | + $orderId = microtime(true); |
| 85 | + $preauth = (new PreAuthorization(100.0, 'EUR', self::RETURN_URL))->setOrderId($orderId); |
| 86 | + $this->unzer->performAuthorization($preauth, $paymentType, $customerId); |
| 87 | + $payment = $preauth->getPayment(); |
| 88 | + $this->assertNotNull($payment); |
| 89 | + $this->assertNotNull($payment->getId()); |
| 90 | + |
| 91 | + $newCustomer = $payment->getCustomer(); |
| 92 | + $this->assertNotNull($newCustomer); |
| 93 | + $this->assertNotNull($newCustomer->getId()); |
| 94 | + |
| 95 | + return $preauth; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Verify authorization can be fetched. |
| 100 | + * |
| 101 | + * @depends authorizationWithCustomerId |
| 102 | + * |
| 103 | + * @test |
| 104 | + * |
| 105 | + * @param Authorization $authorization |
| 106 | + */ |
| 107 | + public function authorizationCanBeFetched(Authorization $authorization): void |
| 108 | + { |
| 109 | + $fetchedAuthorization = $this->unzer->fetchAuthorization($authorization->getPaymentId()); |
| 110 | + $this->assertInstanceOf(PreAuthorization::class, $fetchedAuthorization); |
| 111 | + $this->assertEquals($authorization->setCard3ds(true)->expose(), $fetchedAuthorization->expose()); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + /** |
| 116 | + * Verify authorize accepts all parameters. |
| 117 | + * |
| 118 | + * @test |
| 119 | + */ |
| 120 | + public function requestAuthorizationShouldAcceptAllParameters(): void |
| 121 | + { |
| 122 | + /** @var Card $card */ |
| 123 | + $card = $this->unzer->createPaymentType($this->createCardObject()); |
| 124 | + $customer = $this->getMinimalCustomer(); |
| 125 | + $orderId = 'o' . self::generateRandomId(); |
| 126 | + $metadata = (new Metadata())->addMetadata('key', 'value'); |
| 127 | + $basket = $this->createBasket(); |
| 128 | + $invoiceId = 'i' . self::generateRandomId(); |
| 129 | + $paymentReference = 'paymentReference'; |
| 130 | + |
| 131 | + $preauth = new PreAuthorization(119.0, 'EUR', self::RETURN_URL); |
| 132 | + $preauth->setRecurrenceType(RecurrenceTypes::ONE_CLICK, $card) |
| 133 | + ->setOrderId($orderId) |
| 134 | + ->setInvoiceId($invoiceId) |
| 135 | + ->setPaymentReference($paymentReference); |
| 136 | + |
| 137 | + $preauth = $this->unzer->performAuthorization($preauth, $card, $customer, $metadata, $basket); |
| 138 | + $payment = $preauth->getPayment(); |
| 139 | + |
| 140 | + $this->assertSame($card, $payment->getPaymentType()); |
| 141 | + $this->assertEquals(119.0, $preauth->getAmount()); |
| 142 | + $this->assertEquals('EUR', $preauth->getCurrency()); |
| 143 | + $this->assertEquals(self::RETURN_URL, $preauth->getReturnUrl()); |
| 144 | + $this->assertSame($customer, $payment->getCustomer()); |
| 145 | + $this->assertEquals($orderId, $preauth->getOrderId()); |
| 146 | + $this->assertSame($metadata, $payment->getMetadata()); |
| 147 | + $this->assertSame($basket, $payment->getBasket()); |
| 148 | + $this->assertTrue($preauth->isCard3ds()); |
| 149 | + $this->assertEquals($invoiceId, $preauth->getInvoiceId()); |
| 150 | + $this->assertEquals($paymentReference, $preauth->getPaymentReference()); |
| 151 | + |
| 152 | + $fetchedAuthorize = $this->unzer->fetchAuthorization($preauth->getPaymentId()); |
| 153 | + $fetchedPayment = $fetchedAuthorize->getPayment(); |
| 154 | + |
| 155 | + $this->assertEquals($payment->getPaymentType()->expose(), $fetchedPayment->getPaymentType()->expose()); |
| 156 | + $this->assertEquals($preauth->getAmount(), $fetchedAuthorize->getAmount()); |
| 157 | + $this->assertEquals($preauth->getCurrency(), $fetchedAuthorize->getCurrency()); |
| 158 | + $this->assertEquals($preauth->getReturnUrl(), $fetchedAuthorize->getReturnUrl()); |
| 159 | + $this->assertEquals($payment->getCustomer()->expose(), $fetchedPayment->getCustomer()->expose()); |
| 160 | + $this->assertEquals($preauth->getOrderId(), $fetchedAuthorize->getOrderId()); |
| 161 | + $this->assertEquals($payment->getMetadata()->expose(), $fetchedPayment->getMetadata()->expose()); |
| 162 | + $this->assertEquals($payment->getBasket()->expose(), $fetchedPayment->getBasket()->expose()); |
| 163 | + $this->assertEquals($preauth->isCard3ds(), $fetchedAuthorize->isCard3ds()); |
| 164 | + $this->assertEquals($preauth->getInvoiceId(), $fetchedAuthorize->getInvoiceId()); |
| 165 | + $this->assertEquals($preauth->getPaymentReference(), $fetchedAuthorize->getPaymentReference()); |
| 166 | + } |
| 167 | + |
| 168 | + //<editor-fold desc="Data Providers"> |
| 169 | + |
| 170 | + /** |
| 171 | + * @return array |
| 172 | + */ |
| 173 | + public function authorizeHasExpectedStatesDP(): array |
| 174 | + { |
| 175 | + return [ |
| 176 | + 'card' => [$this->createCardObject(), 'pending'], |
| 177 | + 'paypal' => [new Paypal(), 'pending'] |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | + //</editor-fold> |
| 182 | +} |
0 commit comments