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

Approach file structure #264

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions documents/addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

### Create an addon

#### Old Method
```java

String subscriptionId = "sub_I55auG9GnbsR8u";

JSONObject addonRequest = new JSONObject();
Expand All @@ -16,7 +18,26 @@ addonRequest.put("quantity",2);

Addon addon = instance.subscriptions.createAddon(subscriptionId,addonRequest);
```
#### New Method

```java
Addon addon = Addon.builder()
.items(
AddOnItem.builder()
.name("Test")
.amount(3000)
.currency("INR")
.description("Test description")
.build()
)
.quantity(2)
.build();

if(addon.validateAddOn()){
Addon createdAddOn = instance.subscriptions.createAddon(subscriptionId,addonRequest.toJson());
}

```
**Parameters:**

| Name | Type | Description |
Expand Down
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@
<version>2.13.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>


<dependency>
<groupId>org.json</groupId>
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/razorpay/Addon.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/razorpay/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import okhttp3.HttpUrl;
import okhttp3.Response;

class ApiClient {
public class ApiClient {

String auth;

Expand All @@ -31,7 +31,7 @@ class ApiClient {

private final int STATUS_MULTIPLE_CHOICE = 300;

ApiClient(String auth) {
public ApiClient(String auth) {
this.auth = auth;
}

Expand All @@ -56,7 +56,7 @@ public <T extends Entity> T patch(String path, JSONObject requestObject) throws
}


<T extends Entity> ArrayList<T> getCollection(String path, JSONObject requestObject)
public <T extends Entity> ArrayList<T> getCollection(String path, JSONObject requestObject)
throws RazorpayException {
Response response = ApiUtils.getRequest(path, requestObject, auth);
return processCollectionResponse(response);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/razorpay/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Constants {
static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

// API URI
static final String PAYMENT_GET = "payments/%s";
public static final String PAYMENT_GET = "payments/%s";
static final String PAYMENT_LIST = "payments";
static final String PAYMENT_CAPTURE = "payments/%s/capture";
static final String PAYMENT_REFUND = "payments/%s/refund";
Expand Down Expand Up @@ -120,9 +120,9 @@ public class Constants {
static final String SUBSCRIPTION_OFFER = "subscriptions/%s/%s";
static final String SUBSCRIPTION_REGISTRATION_LINK = "subscription_registration/auth_links";

static final String ADDON_GET = "addons/%s";
static final String ADDON_DELETE = "addons/%s";
static final String ADDON_LIST = "addons";
public static final String ADDON_GET = "addons/%s";
public static final String ADDON_DELETE = "addons/%s";
public static final String ADDON_LIST = "addons";

static final String VIRTUAL_ACCOUNT_CREATE = "virtual_accounts";
static final String VIRTUAL_ACCOUNT_GET = "virtual_accounts/%s";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/razorpay/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class Entity {
private final String CREATED_AT = "created_at";
private final String CAPTURED_AT = "captured_at";

Entity(JSONObject jsonObject) {
public Entity(JSONObject jsonObject) {
this.modelJson = jsonObject;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/razorpay/RazorpayClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import com.razorpay.addon.AddonClient;
import okhttp3.Credentials;

public class RazorpayClient {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/razorpay/RefundClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class RefundClient extends ApiClient {

RefundClient(String auth) {
public RefundClient(String auth) {
super(auth);
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/razorpay/SubscriptionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.razorpay.addon.Addon;
import org.json.JSONObject;

public class SubscriptionClient extends ApiClient {
Expand Down Expand Up @@ -38,6 +39,10 @@ public Addon createAddon(String id, JSONObject request) throws RazorpayException
return post(String.format(Constants.SUBSCRIPTION_ADDON_CREATE, id), request);
}

public Addon createAddon(String id, Addon addon) throws RazorpayException {
return post(String.format(Constants.SUBSCRIPTION_ADDON_CREATE, id), addon.toJson());
}

public Subscription update(String id, JSONObject request) throws RazorpayException {
return patch(String.format(Constants.SUBSCRIPTION, id), request);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/razorpay/addon/AddOnItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.razorpay.addon;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class AddOnItem {
String name;
Integer amount;
String currency;
String description;

}
48 changes: 48 additions & 0 deletions src/main/java/com/razorpay/addon/Addon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.razorpay.addon;

import com.google.gson.Gson;
import com.razorpay.Entity;
import lombok.Builder;
import org.json.JSONObject;

@Builder
public class Addon extends Entity {

public Addon(JSONObject jsonObject) {
super(jsonObject);
}

AddOnItem items;
Integer quantity;

public JSONObject toJson(){

JSONObject addonRequest = new JSONObject();
JSONObject itemsJson = new JSONObject();
itemsJson.put("name",items.getName());
itemsJson.put("amount",items.getAmount());
itemsJson.put("currency",items.getCurrency());
itemsJson.put("description",items.getDescription());
addonRequest.put("item",itemsJson);
addonRequest.put("quantity",this.quantity);

return addonRequest;
}

public static Addon getAddOnFromJson(JSONObject jsonObject) throws Exception {
Gson gson = new Gson();
try {
Addon addon= gson.fromJson(String.valueOf(jsonObject), Addon.class);
return addon;
} catch (Exception e){
throw new Exception("Error parsing. Please check the input string");
}
}

public boolean validateAddOn(){
if(this.items.getName() == null || this.items.getName().isEmpty()) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.razorpay;
package com.razorpay.addon;

import java.util.List;

import com.razorpay.ApiClient;
import com.razorpay.Constants;
import com.razorpay.RazorpayException;
import org.json.JSONObject;

public class AddonClient extends ApiClient {

AddonClient(String auth) {
public AddonClient(String auth) {
super(auth);
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/razorpay/AddonClientTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.razorpay;

import com.razorpay.addon.AddOnItem;
import com.razorpay.addon.Addon;
import com.razorpay.addon.AddonClient;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.InjectMocks;
Expand All @@ -23,6 +26,7 @@ public class AddonClientTest extends BaseTest{
@Test
public void fetch() throws RazorpayException {


String json = "{\n \"id\":"+ADDON_ID+",\n" +
"\"entity\":\"addon\",\n" +
"\"item\":{\n" +
Expand Down Expand Up @@ -117,4 +121,21 @@ public void fetchAll() throws RazorpayException {
assertTrue(false);
}
}

public void testCreate(){
Addon addon = Addon.builder()
.items(
AddOnItem.builder()
.name("Test")
.amount(3000)
.currency("INR")
.description("Test description")
.build()
)
.quantity(2)
.build();
if(addon.validateAddOn()){

}
}
}
5 changes: 1 addition & 4 deletions src/test/java/com/razorpay/SubscriptionClientTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.razorpay;

import okhttp3.Request;
import okio.Buffer;
import com.razorpay.addon.Addon;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Arrays;
Expand Down