-
Notifications
You must be signed in to change notification settings - Fork 1
/
PaymentsApiResource.java
148 lines (127 loc) · 6.49 KB
/
PaymentsApiResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.mifosplatform.billing.payments.api;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.mifosplatform.billing.payments.data.PaymentData;
import org.mifosplatform.billing.paymode.data.McodeData;
import org.mifosplatform.billing.paymode.service.PaymodeReadPlatformService;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.commands.service.CommandWrapperBuilder;
import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService;
import org.mifosplatform.infrastructure.codes.data.CodeData;
import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.google.gson.JsonObject;
@Path("/payments")
@Component
@Scope("singleton")
public class PaymentsApiResource {
/**
* The set of parameters that are supported in response for {@link CodeData}
*/
private static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(
Arrays.asList("id", "clientId", "paymentDate", "paymentCode",
"amountPaid", "statmentId", "externalId", "Remarks"));
private final String resourceNameForPermissions = "GETPAYMENT";
private final PlatformSecurityContext context;
private final PaymodeReadPlatformService readPlatformService;
private final DefaultToApiJsonSerializer<PaymentData> toApiJsonSerializer;
private final ApiRequestParameterHelper apiRequestParameterHelper;
private final PortfolioCommandSourceWritePlatformService writePlatformService;
@Autowired
public PaymentsApiResource(final PlatformSecurityContext context,final PaymodeReadPlatformService readPlatformService,
final DefaultToApiJsonSerializer<PaymentData> toApiJsonSerializer,final ApiRequestParameterHelper apiRequestParameterHelper,
final PortfolioCommandSourceWritePlatformService writePlatformService) {
this.context = context;
this.readPlatformService = readPlatformService;
this.toApiJsonSerializer = toApiJsonSerializer;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.writePlatformService = writePlatformService;
}
@POST
@Path("{clientId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String createPayment(@PathParam("clientId") final Long clientId, final String apiRequestBodyAsJson) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().createPayment(clientId).withJson(apiRequestBodyAsJson).build();
final CommandProcessingResult result = this.writePlatformService.logCommandSource(commandRequest);
return this.toApiJsonSerializer.serialize(result);
}
@GET
@Path("template")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveDetailsForPayments(@QueryParam("clientId") final Long clientId,@Context final UriInfo uriInfo) {
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
Collection<McodeData> data = this.readPlatformService.retrievemCodeDetails("Payment Mode");
PaymentData paymentData=new PaymentData(data);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, paymentData,RESPONSE_DATA_PARAMETERS);
}
@GET
@Path("{clientId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAllDetailsForPayments(@PathParam("clientId") final Long clientId,@Context final UriInfo uriInfo) {
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
List<PaymentData> paymentData = readPlatformService.retrivePaymentsData(clientId);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, paymentData,RESPONSE_DATA_PARAMETERS);
}
@POST
@Path("paypal")
@Consumes("application/x-www-form-urlencoded")
//@Consumes({ MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON ,MediaType.APPLICATION_OCTET_STREAM,MediaType.TEXT_PLAIN,MediaType.WILDCARD })
@Produces({ MediaType.APPLICATION_JSON })
public String Checkout(@FormParam("txn_id") String txnId,@FormParam("payment_date") Date Date,@FormParam("mc_gross") BigDecimal amount,@FormParam("address_name") String name,@FormParam("payer_email") String payerEmail){
try {
// ResourceBundle bundle = ResourceBundle.getBundle("usercredentials",Locale.getDefault());
SimpleDateFormat daformat=new SimpleDateFormat("dd MMMM yyyy");
String date=daformat.format(Date);
JsonObject object=new JsonObject();
object.addProperty("txn_id", txnId);
object.addProperty("dateFormat","dd MMMM yyyy");
object.addProperty("locale","en");
object.addProperty("paymentDate",date);
object.addProperty("amountPaid",amount);
object.addProperty("isChequeSelected","no");
object.addProperty("receiptNo","1234");
object.addProperty("paypalemail", payerEmail);
object.addProperty("remarks",payerEmail);
object.addProperty("paymentCode",27);
final CommandWrapper commandRequest = new CommandWrapperBuilder().createPaypal(payerEmail).withJson(object.toString()).build();
final CommandProcessingResult result1 = this.writePlatformService.logCommandSource(commandRequest);
return this.toApiJsonSerializer.serialize(result1);
} /*catch (PayPalException e) {
// TODO Auto-generated catch block
return e.getMessage();
}*/
catch(Exception e){
return e.getMessage();
}
}
}