From a89e4bbd85b87e9553ca105188ff4e8d433d87a8 Mon Sep 17 00:00:00 2001 From: Abdurrahman Basgoynuk Date: Fri, 13 Dec 2024 13:02:17 +0300 Subject: [PATCH 1/4] DEV-30004 adds refund v2 and threeds auth v2 response signature --- src/main/java/com/iyzipay/model/Refund.java | 10 ++++ .../request/CreateRefundV2Request.java | 48 +++++++++++++++++++ .../com/iyzipay/functional/RefundTest.java | 32 +++++++++++++ .../java/com/iyzipay/sample/RefundSample.java | 23 +++++++++ .../com/iyzipay/sample/ThreedsSample.java | 1 + 5 files changed, 114 insertions(+) create mode 100644 src/main/java/com/iyzipay/request/CreateRefundV2Request.java diff --git a/src/main/java/com/iyzipay/model/Refund.java b/src/main/java/com/iyzipay/model/Refund.java index e1ec2857..1ea8f160 100644 --- a/src/main/java/com/iyzipay/model/Refund.java +++ b/src/main/java/com/iyzipay/model/Refund.java @@ -6,6 +6,7 @@ import com.iyzipay.IyzipayResource; import com.iyzipay.Options; import com.iyzipay.request.CreateRefundRequest; +import com.iyzipay.request.CreateRefundV2Request; public class Refund extends IyzipayResource { @@ -27,6 +28,15 @@ public static Refund create(CreateRefundRequest request, Options options) { Refund.class); } + public static Refund createV2(CreateRefundV2Request request, Options options) { + String path = "/v2/payment/refund"; + return HttpClient.create().post(options.getBaseUrl() + path, + getHttpProxy(options), + getHttpHeadersV2(path, request, options), + request, + Refund.class); + } + public String getPaymentId() { return paymentId; } diff --git a/src/main/java/com/iyzipay/request/CreateRefundV2Request.java b/src/main/java/com/iyzipay/request/CreateRefundV2Request.java new file mode 100644 index 00000000..aae555c2 --- /dev/null +++ b/src/main/java/com/iyzipay/request/CreateRefundV2Request.java @@ -0,0 +1,48 @@ +package com.iyzipay.request; + +import com.iyzipay.Request; +import com.iyzipay.ToStringRequestBuilder; + +import java.math.BigDecimal; + +public class CreateRefundV2Request extends Request { + + private String paymentId; + private BigDecimal price; + private String ip; + + + public String getPaymentId() { + return paymentId; + } + + public void setPaymentId(String paymentId) { + this.paymentId = paymentId; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + @Override + public String toString() { + return new ToStringRequestBuilder(this) + .appendSuper(super.toString()) + .append("paymentId", paymentId) + .append("price", price) + .append("ip", ip) + .toString(); + } +} \ No newline at end of file diff --git a/src/test/java/com/iyzipay/functional/RefundTest.java b/src/test/java/com/iyzipay/functional/RefundTest.java index 41d0c15f..df2f27b0 100644 --- a/src/test/java/com/iyzipay/functional/RefundTest.java +++ b/src/test/java/com/iyzipay/functional/RefundTest.java @@ -4,6 +4,7 @@ import com.iyzipay.model.*; import com.iyzipay.request.CreatePaymentRequest; import com.iyzipay.request.CreateRefundRequest; +import com.iyzipay.request.CreateRefundV2Request; import org.junit.Test; import java.math.BigDecimal; @@ -81,4 +82,35 @@ public void should_refund_fraudulent_payment() { assertNull(refund.getErrorMessage()); assertNull(refund.getErrorGroup()); } + + @Test + public void should_refund_v2_payment() { + CreatePaymentRequest paymentRequest = CreatePaymentRequestBuilder.create() + .standardListingPayment() + .build(); + + Payment payment = Payment.create(paymentRequest, options); + + CreateRefundV2Request request = new CreateRefundV2Request(); + request.setLocale(Locale.TR.getValue()); + request.setConversationId("123456789"); + request.setPaymentId(payment.getPaymentId()); + request.setPrice(new BigDecimal("1.1")); + request.setIp("85.34.78.112"); + + Refund refund = Refund.createV2(request, options); + + System.out.println(refund); + + assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); + assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertTrue(payment.verifySignature(options.getSecretKey())); + assertEquals("123456789", refund.getConversationId()); + assertEquals(payment.getPaymentId(), refund.getPaymentId()); + assertNotEquals(0, refund.getSystemTime()); + assertNotNull(refund.getHostReference()); + assertNull(refund.getErrorCode()); + assertNull(refund.getErrorMessage()); + assertNull(refund.getErrorGroup()); + } } \ No newline at end of file diff --git a/src/test/java/com/iyzipay/sample/RefundSample.java b/src/test/java/com/iyzipay/sample/RefundSample.java index ae833922..aacb2fc1 100644 --- a/src/test/java/com/iyzipay/sample/RefundSample.java +++ b/src/test/java/com/iyzipay/sample/RefundSample.java @@ -6,6 +6,7 @@ import com.iyzipay.model.RefundReason; import com.iyzipay.model.Status; import com.iyzipay.request.CreateRefundRequest; +import com.iyzipay.request.CreateRefundV2Request; import org.junit.Test; import java.math.BigDecimal; @@ -88,4 +89,26 @@ public void should_refund_fraudulent_payment() { assertNull(refund.getErrorMessage()); assertNull(refund.getErrorGroup()); } + + @Test + public void should_refund_v2_payment() { + CreateRefundV2Request request = new CreateRefundV2Request(); + request.setLocale(Locale.TR.getValue()); + request.setConversationId("123456789"); + request.setPaymentId("1"); + request.setPrice(new BigDecimal("1.1")); + request.setIp("85.34.78.112"); + + Refund refund = Refund.createV2(request, options); + + System.out.println(refund); + + assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); + assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertEquals("123456789", refund.getConversationId()); + assertNotEquals(0, refund.getSystemTime()); + assertNull(refund.getErrorCode()); + assertNull(refund.getErrorMessage()); + assertNull(refund.getErrorGroup()); + } } diff --git a/src/test/java/com/iyzipay/sample/ThreedsSample.java b/src/test/java/com/iyzipay/sample/ThreedsSample.java index d3119ea1..1d8c679c 100644 --- a/src/test/java/com/iyzipay/sample/ThreedsSample.java +++ b/src/test/java/com/iyzipay/sample/ThreedsSample.java @@ -168,6 +168,7 @@ public void should_create_threeds_payment_v2() { assertEquals(Status.SUCCESS.getValue(), threedsPayment.getStatus()); assertEquals(Locale.TR.getValue(), threedsPayment.getLocale()); + assertTrue(threedsPayment.verifySignature(options.getSecretKey())); assertEquals("123456789", threedsPayment.getConversationId()); assertNotEquals(0, threedsPayment.getSystemTime()); assertNull(threedsPayment.getErrorCode()); From 5ec40bd336a15d3a23d063f500d7e4613523a075 Mon Sep 17 00:00:00 2001 From: Abdurrahman Basgoynuk Date: Thu, 19 Dec 2024 09:31:03 +0300 Subject: [PATCH 2/4] DEV-30004 adds refund v2 and threeds auth v2 response signature --- src/test/java/com/iyzipay/functional/RefundTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/iyzipay/functional/RefundTest.java b/src/test/java/com/iyzipay/functional/RefundTest.java index df2f27b0..c1382ce2 100644 --- a/src/test/java/com/iyzipay/functional/RefundTest.java +++ b/src/test/java/com/iyzipay/functional/RefundTest.java @@ -11,7 +11,7 @@ import static org.junit.Assert.*; -public class RefundTest extends BaseTest { +public class RefundTest extends IyziupBaseTest { @Test public void should_refund_payment() { From 81ea57e77052df53c1eec23c0e8c92449b9a7b3d Mon Sep 17 00:00:00 2001 From: Abdurrahman Basgoynuk Date: Thu, 19 Dec 2024 09:41:27 +0300 Subject: [PATCH 3/4] DEV-30004 adds refund v2 and threeds auth v2 response signature --- .../java/com/iyzipay/functional/RefundTest.java | 2 +- .../iyzipay/functional/StandardBaseTest.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/iyzipay/functional/StandardBaseTest.java diff --git a/src/test/java/com/iyzipay/functional/RefundTest.java b/src/test/java/com/iyzipay/functional/RefundTest.java index c1382ce2..b5e93c59 100644 --- a/src/test/java/com/iyzipay/functional/RefundTest.java +++ b/src/test/java/com/iyzipay/functional/RefundTest.java @@ -11,7 +11,7 @@ import static org.junit.Assert.*; -public class RefundTest extends IyziupBaseTest { +public class RefundTest extends StandardBaseTest { @Test public void should_refund_payment() { diff --git a/src/test/java/com/iyzipay/functional/StandardBaseTest.java b/src/test/java/com/iyzipay/functional/StandardBaseTest.java new file mode 100644 index 00000000..b1ffdf19 --- /dev/null +++ b/src/test/java/com/iyzipay/functional/StandardBaseTest.java @@ -0,0 +1,17 @@ +package com.iyzipay.functional; + +import com.iyzipay.Options; +import org.junit.Before; + +public abstract class StandardBaseTest { + + Options options; + + @Before + public void setUp() { + options = new Options(); + options.setApiKey("sandbox-qBDJ5ttcxbXNNzLZ02WmkiKtHH3ADONj"); + options.setSecretKey("sandbox-HfB5nGM5CRAGdtAijxZ8xHlqYkvN1B0p"); + options.setBaseUrl("https://sandbox-api.iyzipay.com"); + } +} \ No newline at end of file From 99050179a9d04cb5d89ebdc818b8b253f3071fd4 Mon Sep 17 00:00:00 2001 From: Abdurrahman Basgoynuk Date: Thu, 26 Dec 2024 16:33:06 +0300 Subject: [PATCH 4/4] DEV-30004 adds response signature to refund and refund v2 --- src/main/java/com/iyzipay/model/Refund.java | 20 ++++++++++++++++++- .../com/iyzipay/functional/RefundTest.java | 3 +++ .../java/com/iyzipay/sample/RefundSample.java | 5 +++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/iyzipay/model/Refund.java b/src/main/java/com/iyzipay/model/Refund.java index 1ea8f160..a3e3fafc 100644 --- a/src/main/java/com/iyzipay/model/Refund.java +++ b/src/main/java/com/iyzipay/model/Refund.java @@ -1,14 +1,17 @@ package com.iyzipay.model; import java.math.BigDecimal; +import java.util.Arrays; +import com.iyzipay.HashValidator; import com.iyzipay.HttpClient; import com.iyzipay.IyzipayResource; import com.iyzipay.Options; +import com.iyzipay.ResponseSignatureGenerator; import com.iyzipay.request.CreateRefundRequest; import com.iyzipay.request.CreateRefundV2Request; -public class Refund extends IyzipayResource { +public class Refund extends IyzipayResource implements ResponseSignatureGenerator { private String paymentId; private String paymentTransactionId; @@ -18,6 +21,7 @@ public class Refund extends IyzipayResource { private String authCode; private String hostReference; private String refundHostReference; + private String signature; public static Refund create(CreateRefundRequest request, Options options) { String path = "/payment/refund"; @@ -37,6 +41,12 @@ public static Refund createV2(CreateRefundV2Request request, Options options) { Refund.class); } + public boolean verifySignature(String secretKey) { + String calculated = generateSignature(secretKey, + Arrays.asList(getPaymentId(), getPrice(), getCurrency(), getConversationId())); + return HashValidator.hashValid(getSignature(), calculated); + } + public String getPaymentId() { return paymentId; } @@ -99,4 +109,12 @@ public String getRefundHostReference() { public void setRefundHostReference(String refundHostReference) { this.refundHostReference = refundHostReference; } + + public String getSignature() { + return signature; + } + + public void setSignature(String signature) { + this.signature = signature; + } } diff --git a/src/test/java/com/iyzipay/functional/RefundTest.java b/src/test/java/com/iyzipay/functional/RefundTest.java index b5e93c59..0f9a84cb 100644 --- a/src/test/java/com/iyzipay/functional/RefundTest.java +++ b/src/test/java/com/iyzipay/functional/RefundTest.java @@ -36,6 +36,7 @@ public void should_refund_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); assertTrue(payment.verifySignature(options.getSecretKey())); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertEquals(payment.getPaymentId(), refund.getPaymentId()); assertEquals(payment.getPaymentItems().get(0).getPaymentTransactionId(), refund.getPaymentTransactionId()); @@ -72,6 +73,7 @@ public void should_refund_fraudulent_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); assertTrue(payment.verifySignature(options.getSecretKey())); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertEquals(payment.getPaymentId(), refund.getPaymentId()); assertEquals(payment.getPaymentItems().get(0).getPaymentTransactionId(), refund.getPaymentTransactionId()); @@ -105,6 +107,7 @@ public void should_refund_v2_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); assertTrue(payment.verifySignature(options.getSecretKey())); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertEquals(payment.getPaymentId(), refund.getPaymentId()); assertNotEquals(0, refund.getSystemTime()); diff --git a/src/test/java/com/iyzipay/sample/RefundSample.java b/src/test/java/com/iyzipay/sample/RefundSample.java index aacb2fc1..36644601 100644 --- a/src/test/java/com/iyzipay/sample/RefundSample.java +++ b/src/test/java/com/iyzipay/sample/RefundSample.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class RefundSample extends Sample { @@ -33,6 +34,7 @@ public void should_refund_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertNotEquals(0, refund.getSystemTime()); assertNull(refund.getErrorCode()); @@ -58,6 +60,7 @@ public void should_refund_payment_with_reason() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertNotEquals(0, refund.getSystemTime()); assertNull(refund.getErrorCode()); @@ -83,6 +86,7 @@ public void should_refund_fraudulent_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertNotEquals(0, refund.getSystemTime()); assertNull(refund.getErrorCode()); @@ -105,6 +109,7 @@ public void should_refund_v2_payment() { assertEquals(Status.SUCCESS.getValue(), refund.getStatus()); assertEquals(Locale.TR.getValue(), refund.getLocale()); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertNotEquals(0, refund.getSystemTime()); assertNull(refund.getErrorCode());