Skip to content

Commit 884b446

Browse files
Include additional HTTP headers with the API request (#1578)
* Add test and improve README * Update README.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update README.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 05ee44f commit 884b446

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,67 @@ PaymentLinksApi paymentLinksApi = new PaymentLinksApi(client);
143143

144144
...
145145
~~~~
146+
147+
### Making requests with additional headers
148+
149+
You can include additional headers and an [idempotency key](https://docs.adyen.com/development-resources/api-idempotency/) in your API requests using the `RequestOptions` object.
150+
151+
~~~~ java
152+
import com.adyen.Client;
153+
import com.adyen.Config;
154+
import com.adyen.enums.Environment;
155+
import com.adyen.model.RequestOptions;
156+
import com.adyen.service.checkout.PaymentsApi;
157+
import com.adyen.model.checkout.PaymentRequest;
158+
import com.adyen.model.checkout.PaymentResponse;
159+
import com.adyen.model.checkout.Amount;
160+
import com.adyen.model.checkout.CardDetails;
161+
import com.adyen.model.checkout.CheckoutPaymentMethod;
162+
163+
import java.util.HashMap;
164+
import java.util.Map;
165+
166+
// Setup Client using Config object
167+
Config config = new Config()
168+
.environment(Environment.TEST) // Or Environment.LIVE
169+
.apiKey("YOUR_API_KEY");
170+
Client client = new Client(config);
171+
172+
// Create RequestOptions
173+
HashMap<String, String> additionalHeaders = new HashMap<>();
174+
additionalHeaders.put("X-Custom-Header", "custom-value");
175+
176+
RequestOptions requestOptions = new RequestOptions()
177+
.idempotencyKey("YOUR_IDEMPOTENCY_KEY") // use UUID
178+
.additionalServiceHeaders(additionalHeaders);
179+
180+
PaymentsApi paymentsApi = new PaymentsApi(client);
181+
182+
// Create PaymentRequest (example from existing README)
183+
CardDetails cardDetails =
184+
new CardDetails()
185+
.type(CardDetails.TypeEnum.SCHEME)
186+
.encryptedCardNumber("5136333333333335")
187+
.holderName("John Doe")
188+
.cvc("737")
189+
.encryptedExpiryMonth("08")
190+
.encryptedExpiryYear("2018");
191+
PaymentRequest paymentRequest =
192+
new PaymentRequest()
193+
.merchantAccount("YOUR_MERCHANT_ACCOUNT")
194+
.reference("YOUR_REFERENCE")
195+
.amount(new Amount()
196+
.currency("EUR")
197+
.value(1000L))
198+
.returnUrl("https://your-company.example.org/checkout?shopperOrder=12xy..")
199+
.paymentMethod(new CheckoutPaymentMethod(cardDetails));
200+
201+
// Make a call to the /payments endpoint with RequestOptions
202+
PaymentResponse paymentResponse = paymentsApi.payments(paymentRequest, requestOptions);
203+
204+
// Process paymentResponse
205+
System.out.println("Payment response: " + paymentResponse.toJson());
206+
~~~~
146207
### Deserializing JSON Strings
147208
In some setups you might need to deserialize JSON strings to request objects. For example, when using the libraries in combination with [Dropin/Components](https://github.com/Adyen/adyen-web). Please use the built-in deserialization functions:
148209
~~~~ java

src/test/java/com/adyen/httpclient/ClientTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,34 @@ public void testUserAgentWithoutApplicationName() throws Exception {
176176
assertNotNull(userAgent);
177177
assertEquals(Client.LIB_NAME + "/" + Client.LIB_VERSION, userAgent.getValue());
178178
}
179+
180+
@Test
181+
public void testRequestWithHttpHeaders() throws Exception {
182+
AdyenHttpClient client = new AdyenHttpClient();
183+
HashMap<String, String> additionalHeaders = new HashMap<>();
184+
additionalHeaders.put("X-Custom-Header", "custom-value");
185+
186+
RequestOptions requestOptions =
187+
new RequestOptions()
188+
.idempotencyKey("test-idempotency-key")
189+
.additionalServiceHeaders(additionalHeaders);
190+
191+
HttpUriRequestBase request =
192+
client.createRequest(
193+
"https://checkout-test.adyen.com/v68/payments",
194+
"{}",
195+
new Config(),
196+
true,
197+
requestOptions,
198+
ApiConstants.HttpMethod.POST,
199+
Map.of());
200+
201+
Header idempotencyKeyHeader = request.getFirstHeader("Idempotency-Key");
202+
assertNotNull(idempotencyKeyHeader);
203+
assertEquals("test-idempotency-key", idempotencyKeyHeader.getValue());
204+
205+
Header customHeader = request.getFirstHeader("X-Custom-Header");
206+
assertNotNull(customHeader);
207+
assertEquals("custom-value", customHeader.getValue());
208+
}
179209
}

0 commit comments

Comments
 (0)