|
| 1 | +package com.adyen.service.clouddevice; |
| 2 | + |
| 3 | +import com.adyen.BaseIntegrationTest; |
| 4 | +import com.adyen.model.clouddevice.*; |
| 5 | +import com.adyen.model.tapi.*; |
| 6 | +import com.adyen.security.clouddevice.EncryptionCredentialDetails; |
| 7 | +import java.math.BigDecimal; |
| 8 | +import java.time.OffsetDateTime; |
| 9 | +import java.time.ZoneOffset; |
| 10 | +import java.util.UUID; |
| 11 | +import org.junit.jupiter.api.Assertions; |
| 12 | +import org.junit.jupiter.api.Disabled; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +/** |
| 16 | + * Verify Terminal integration: tests to send API requests to the Cloud Device API and test the |
| 17 | + * Terminal responds as expected. |
| 18 | + * |
| 19 | + * <p>Don't forget to: |
| 20 | + * |
| 21 | + * <ul> |
| 22 | + * <li>Enable the terminal |
| 23 | + * <li>Enable the test to run (by removing/commenting the {@code @Disabled} annotation) |
| 24 | + * <li>Set required variables by creating {@code src/test/resources/config.properties}: |
| 25 | + * </ul> |
| 26 | + * |
| 27 | + * <pre>{@code |
| 28 | + * # Example of config.properties |
| 29 | + * ADYEN_API_KEY= |
| 30 | + * ADYEN_MERCHANT_ACCOUNT=MyMerchantAccount |
| 31 | + * ADYEN_TERMINAL_DEVICE_ID=V400m-1234567890 |
| 32 | + * ADYEN_TERMINAL_DEVICE_KEY_IDENTIFIER= |
| 33 | + * ADYEN_TERMINAL_DEVICE_PASSPHRASE= |
| 34 | + * }</pre> |
| 35 | + * |
| 36 | + * <ul> |
| 37 | + * <li>Run one test at a time with {@code mvn test -Dtest=CloudDeviceApiTerminalIT#sendSync} |
| 38 | + * <li>Disable the test again |
| 39 | + * </ul> |
| 40 | + */ |
| 41 | +public class CloudDeviceApiTerminalIT extends BaseIntegrationTest { |
| 42 | + |
| 43 | + @Disabled("Enable when you want to test with the Terminal") |
| 44 | + @Test |
| 45 | + public void sendSync() throws Exception { |
| 46 | + |
| 47 | + CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(getClient()); |
| 48 | + |
| 49 | + CloudDeviceApiRequest cloudDeviceApiRequest = |
| 50 | + createCloudDeviceAPIPaymentRequest(getTerminalDeviceId()); |
| 51 | + |
| 52 | + var response = |
| 53 | + cloudDeviceApi.sync(getMerchantAccount(), getTerminalDeviceId(), cloudDeviceApiRequest); |
| 54 | + |
| 55 | + Assertions.assertNotNull(response); |
| 56 | + Assertions.assertNotNull(response.getSaleToPOIResponse()); |
| 57 | + Assertions.assertEquals(getTerminalDeviceId(), response.getSaleToPOIResponse().getMessageHeader().getPOIID()); |
| 58 | + } |
| 59 | + |
| 60 | + @Disabled("Enable when you want to test with the Terminal") |
| 61 | + @Test |
| 62 | + public void sendAsync() throws Exception { |
| 63 | + |
| 64 | + CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(getClient()); |
| 65 | + |
| 66 | + CloudDeviceApiRequest cloudDeviceApiRequest = |
| 67 | + createCloudDeviceAPIPaymentRequest(getTerminalDeviceId()); |
| 68 | + |
| 69 | + var response = |
| 70 | + cloudDeviceApi.async(getMerchantAccount(), getTerminalDeviceId(), cloudDeviceApiRequest); |
| 71 | + |
| 72 | + Assertions.assertNotNull(response); |
| 73 | + Assertions.assertEquals("ok", response.getResult()); |
| 74 | + } |
| 75 | + |
| 76 | + @Disabled("Enable when you want to test with the Terminal") |
| 77 | + @Test |
| 78 | + public void sendEncryptedSync() throws Exception { |
| 79 | + |
| 80 | + CloudDeviceApiRequest cloudDeviceApiRequest = |
| 81 | + createCloudDeviceAPIPaymentRequest(getTerminalDeviceId()); |
| 82 | + |
| 83 | + EncryptionCredentialDetails encryptionCredentialDetails = |
| 84 | + new EncryptionCredentialDetails() |
| 85 | + .adyenCryptoVersion(1) |
| 86 | + .keyIdentifier(getTerminalDeviceKeyIdentifier()) |
| 87 | + .keyVersion(1) |
| 88 | + .passphrase(getTerminalDevicePassphrase()); |
| 89 | + |
| 90 | + EncryptedCloudDeviceApi encryptedCloudDeviceApi = |
| 91 | + new EncryptedCloudDeviceApi(getClient(), encryptionCredentialDetails); |
| 92 | + |
| 93 | + var response = |
| 94 | + encryptedCloudDeviceApi.sync( |
| 95 | + getMerchantAccount(), getTerminalDeviceId(), cloudDeviceApiRequest); |
| 96 | + |
| 97 | + Assertions.assertNotNull(response); |
| 98 | + Assertions.assertNotNull(response.getSaleToPOIResponse()); |
| 99 | + Assertions.assertEquals(getTerminalDeviceId(), response.getSaleToPOIResponse().getMessageHeader().getPOIID()); |
| 100 | + } |
| 101 | + |
| 102 | + @Disabled("Enable when you want to test with the Terminal") |
| 103 | + @Test |
| 104 | + public void getConnectedDevices() throws Exception { |
| 105 | + |
| 106 | + CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(getClient()); |
| 107 | + |
| 108 | + var response = cloudDeviceApi.getConnectedDevices(getMerchantAccount()); |
| 109 | + |
| 110 | + Assertions.assertNotNull(response); |
| 111 | + Assertions.assertNotNull(response.getUniqueDeviceIds()); |
| 112 | + Assertions.assertTrue(response.getUniqueDeviceIds().contains(getTerminalDeviceId())); |
| 113 | + } |
| 114 | + |
| 115 | + @Disabled("Enable when you want to test with the Terminal") |
| 116 | + @Test |
| 117 | + public void sendEncryptedAsync() throws Exception { |
| 118 | + |
| 119 | + CloudDeviceApiRequest cloudDeviceApiRequest = |
| 120 | + createCloudDeviceAPIPaymentRequest(getTerminalDeviceId()); |
| 121 | + |
| 122 | + EncryptionCredentialDetails encryptionCredentialDetails = |
| 123 | + new EncryptionCredentialDetails() |
| 124 | + .adyenCryptoVersion(1) |
| 125 | + .keyIdentifier(getTerminalDeviceKeyIdentifier()) |
| 126 | + .keyVersion(1) |
| 127 | + .passphrase(getTerminalDevicePassphrase()); |
| 128 | + |
| 129 | + EncryptedCloudDeviceApi encryptedCloudDeviceApi = |
| 130 | + new EncryptedCloudDeviceApi(getClient(), encryptionCredentialDetails); |
| 131 | + |
| 132 | + var response = |
| 133 | + encryptedCloudDeviceApi.async( |
| 134 | + getMerchantAccount(), getTerminalDeviceId(), cloudDeviceApiRequest); |
| 135 | + |
| 136 | + Assertions.assertNotNull(response); |
| 137 | + Assertions.assertEquals("ok", response.getResult()); |
| 138 | + } |
| 139 | + |
| 140 | + protected CloudDeviceApiRequest createCloudDeviceAPIPaymentRequest(String deviceId) { |
| 141 | + SaleToPOIRequest saleToPOIRequest = new SaleToPOIRequest(); |
| 142 | + |
| 143 | + var randomId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10); |
| 144 | + |
| 145 | + MessageHeader messageHeader = new MessageHeader(); |
| 146 | + messageHeader.setProtocolVersion("3.0"); |
| 147 | + messageHeader.setMessageClass(MessageClass.SERVICE); |
| 148 | + messageHeader.setMessageCategory(MessageCategory.PAYMENT); |
| 149 | + messageHeader.setMessageType(MessageType.REQUEST); |
| 150 | + messageHeader.setSaleID(randomId); |
| 151 | + messageHeader.setServiceID(randomId); |
| 152 | + messageHeader.setPOIID(deviceId); |
| 153 | + |
| 154 | + saleToPOIRequest.setMessageHeader(messageHeader); |
| 155 | + |
| 156 | + PaymentRequest paymentRequest = new PaymentRequest(); |
| 157 | + |
| 158 | + SaleData saleData = new SaleData(); |
| 159 | + TransactionIDType transactionIdentification = new TransactionIDType(); |
| 160 | + transactionIdentification.setTransactionID(randomId); |
| 161 | + OffsetDateTime timestamp = OffsetDateTime.now(ZoneOffset.UTC); |
| 162 | + transactionIdentification.setTimeStamp(timestamp); |
| 163 | + saleData.setSaleTransactionID(transactionIdentification); |
| 164 | + |
| 165 | + PaymentTransaction paymentTransaction = new PaymentTransaction(); |
| 166 | + AmountsReq amountsReq = new AmountsReq(); |
| 167 | + amountsReq.setCurrency("EUR"); |
| 168 | + amountsReq.setRequestedAmount(BigDecimal.TEN); |
| 169 | + paymentTransaction.setAmountsReq(amountsReq); |
| 170 | + |
| 171 | + paymentRequest.setSaleData(saleData); |
| 172 | + paymentRequest.setPaymentTransaction(paymentTransaction); |
| 173 | + |
| 174 | + saleToPOIRequest.setPaymentRequest(paymentRequest); |
| 175 | + |
| 176 | + CloudDeviceApiRequest cloudDeviceApiRequest = new CloudDeviceApiRequest(); |
| 177 | + cloudDeviceApiRequest.setSaleToPOIRequest(saleToPOIRequest); |
| 178 | + |
| 179 | + return cloudDeviceApiRequest; |
| 180 | + } |
| 181 | +} |
0 commit comments