From b578e080fd6b3768fd3917b66003f36b8af0a3af Mon Sep 17 00:00:00 2001 From: abdurrahman <57454378+abdurrahman98@users.noreply.github.com> Date: Fri, 27 Dec 2024 09:12:52 +0300 Subject: [PATCH] DEV-30004 adds refund v2 and threeds auth v2 response signature (#162) * DEV-30004 adds refund v2 and threeds auth v2 response signature * DEV-30004 adds refund v2 and threeds auth v2 response signature * DEV-30004 adds refund v2 and threeds auth v2 response signature * DEV-30004 adds response signature to refund and refund v2 --------- Co-authored-by: Abdurrahman Basgoynuk --- src/main/java/com/iyzipay/model/Refund.java | 30 +++++++++++- .../request/CreateRefundV2Request.java | 48 +++++++++++++++++++ .../com/iyzipay/functional/RefundTest.java | 37 +++++++++++++- .../iyzipay/functional/StandardBaseTest.java | 17 +++++++ .../java/com/iyzipay/sample/RefundSample.java | 28 +++++++++++ .../com/iyzipay/sample/ThreedsSample.java | 1 + 6 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/iyzipay/request/CreateRefundV2Request.java create mode 100644 src/test/java/com/iyzipay/functional/StandardBaseTest.java diff --git a/src/main/java/com/iyzipay/model/Refund.java b/src/main/java/com/iyzipay/model/Refund.java index e1ec2857..a3e3fafc 100644 --- a/src/main/java/com/iyzipay/model/Refund.java +++ b/src/main/java/com/iyzipay/model/Refund.java @@ -1,13 +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; @@ -17,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"; @@ -27,6 +32,21 @@ 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 boolean verifySignature(String secretKey) { + String calculated = generateSignature(secretKey, + Arrays.asList(getPaymentId(), getPrice(), getCurrency(), getConversationId())); + return HashValidator.hashValid(getSignature(), calculated); + } + public String getPaymentId() { return paymentId; } @@ -89,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/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..0f9a84cb 100644 --- a/src/test/java/com/iyzipay/functional/RefundTest.java +++ b/src/test/java/com/iyzipay/functional/RefundTest.java @@ -4,13 +4,14 @@ 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; import static org.junit.Assert.*; -public class RefundTest extends BaseTest { +public class RefundTest extends StandardBaseTest { @Test public void should_refund_payment() { @@ -35,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()); @@ -71,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()); @@ -81,4 +84,36 @@ 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())); + assertTrue(refund.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/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 diff --git a/src/test/java/com/iyzipay/sample/RefundSample.java b/src/test/java/com/iyzipay/sample/RefundSample.java index ae833922..36644601 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; @@ -13,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 { @@ -32,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()); @@ -57,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()); @@ -82,6 +86,30 @@ 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()); + 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()); + assertTrue(refund.verifySignature(options.getSecretKey())); assertEquals("123456789", refund.getConversationId()); assertNotEquals(0, refund.getSystemTime()); assertNull(refund.getErrorCode()); 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());