Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-30004 adds refund v2 and threeds auth v2 response signature #162

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/main/java/com/iyzipay/model/Refund.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/iyzipay/request/CreateRefundV2Request.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
37 changes: 36 additions & 1 deletion src/test/java/com/iyzipay/functional/RefundTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/iyzipay/functional/StandardBaseTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/iyzipay/sample/RefundSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
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;

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 @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/iyzipay/sample/ThreedsSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading