Skip to content

Commit

Permalink
DEV-30004 adds response signature to refund and refund v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdurrahman Basgoynuk committed Dec 26, 2024
1 parent 81ea57e commit 9905017
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/java/com/iyzipay/model/Refund.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions src/test/java/com/iyzipay/functional/RefundTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/iyzipay/sample/RefundSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 9905017

Please sign in to comment.