-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentGateway.java
More file actions
176 lines (144 loc) · 6.91 KB
/
PaymentGateway.java
File metadata and controls
176 lines (144 loc) · 6.91 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.intuit.developer.sampleapp.ecommerce.qbo;
import com.taxcloud.api.*;
import com.intuit.developer.sampleapp.ecommerce.domain.ShoppingCart;
import com.intuit.ipp.data.payment.Capture;
import com.intuit.ipp.data.payment.Charge;
import com.intuit.ipp.data.payment.Charge.ChargeStatus;
import com.intuit.ipp.exception.FMSException;
import com.intuit.ipp.services.payment.ChargeService;
import com.intuit.ipp.services.payment.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
/**
* This class contains methods to interface with the Payments API via the payments SDK.
* Created by akuchta on 8/28/14.
*/
public class PaymentGateway {
@Autowired
QBOServiceFactory qboServiceFactory;
static public final String CHARGE_DESCRIPTION = "TaxCloud Enhanced E-Commerce Sample App";
private TaxCloud taxCloud = new TaxCloud("C1988C0", "D35B1C5F-D6D8-43C0-B804-10EDFCA52796");
/**
* Authorizes a charge for to a credit card who information has been tokenized
* Then captures the funds previously authorized. This is done in two calls to demonstrate the API,
* It is possible to do this with one SDK method, but it is a common use case to authorize and capture as
* separate steps in the transaction,
* @param cart - the cart to create an order for
* @param paymentToken - - the tokenized credit card information
*/
public String chargeCustomerForOrder(ShoppingCart cart, String paymentToken) {
ChargeService chargeService = qboServiceFactory.getChargeService(cart.getCustomer().getCompany());
/**
* Authorizing the charge will verify the credit card account has the funds available and
* subtract the amount from the card holder's balance.
*/
Charge charge = authorizeForOrder(cart, paymentToken, chargeService);
/**
* In a typcial use case, there would be time and other operations between these two calls.
* For example, capture of funds may wait until the order has bee fulfilled.
* It is possible to perform both actions in one step if that its more relevant:
*/
// Authorize and Capture at the same time
// authorizeAndCaptureChargeForOrder(charge, cart, paymentToken, chargeService);
if (charge.getStatus() != ChargeStatus.AUTHORIZED) {
throw new RuntimeException("The credit card charge was not successfully authorized");
}
/**
* Let TaxCloud know we have been authorized
*/
String customerId = cart.getCustomer().getId().toString();
String cartId = Long.toString(cart.getInstanceId());
try {
taxCloud.Authorized(customerId, cartId, charge.getId(), charge.getCreated());
}
catch (Exception ex) {
System.out.println(ex.toString());
}
/**
* Capturing the charge will actually transfer the funds from the card holder's account
* and credit it to the merchants account.
*/
charge = captureFundsForCharge(charge, chargeService);
taxCloud.Captured(cart.getCustomer().getId().toString(), charge.getId());
return charge.getId();
// if (charge.getStatus() != ChargeStatus.SETTLED) {
// throw new RuntimeException("The credit card charge was not successfully settled.");
// }
}
/**
* Authorize and capture funds in one call. - Not actively used but included for reference.
* @param cart - the cart to make a charge for - used to determine amount to charge
* @param paymentToken - the tokenized credit card information
* @param chargeService - the charge service to use for the transaction
*/
private Charge authorizeAndCaptureChargeForOrder(ShoppingCart cart, String paymentToken, ChargeService chargeService) {
Charge charge = new Charge();
charge.setCreated(new Date());
charge.setDescription(CHARGE_DESCRIPTION);
charge.setCurrency("USD");
charge.setAmount(cart.getTotal().getAmount());
charge.setToken(paymentToken);
charge.setCapture(true);
// Create request id
RequestContext requestContext = new RequestContext();
try {
return chargeService.charge(requestContext, charge);
} catch (FMSException e) {
throw new RuntimeException(e);
}
}
/**
* Authorize a charge for the order amount
* @param cart - the cart to make a charge for - used to determine amount to charge
* @param paymentToken - the tokenized credit card information
* @param chargeService - the charge service to use for the transaction
*/
private Charge authorizeForOrder(ShoppingCart cart, String paymentToken, ChargeService chargeService) {
Charge charge = new Charge();
// Only Authorize funds in this stage - set to 'true' to authorize and capture in one step
charge.setCapture(false);
// We created the charge today
charge.setCreated(new Date());
// We also need to set currency
charge.setCurrency("USD");
// Set Description - what will the customer see on their statement?
charge.setDescription(CHARGE_DESCRIPTION);
// Set the charge amount
charge.setAmount(cart.getTotal().getAmount());
// Supply the credit card information in the form of a payment token
charge.setToken(paymentToken);
// Create request id
RequestContext requestContext = new RequestContext();
// Try to authorize the charge
try {
return chargeService.charge(requestContext,charge);
} catch (FMSException e) {
throw new RuntimeException(e);
}
}
/**
* Capture funds for a previously authorized charge.
* @param charge - a charge object to use, will be updated with return value from service
* @param chargeService - the charge service to use for the transaction
*/
private Charge captureFundsForCharge(Charge charge, ChargeService chargeService) {
Capture capture = new Capture();
// Capture the same amount as previously authorized - this may not be true in every application
// The capture amount can be more or less than the authorization amount
// But in this example they are the same
capture.setAmount(charge.getAmount());
// Set the date
capture.setCreated(new Date());
// Keep the description from the authorization. They do not have to be the same,
// but it makes sense in this situation
capture.setDescription(charge.getDescription());
// Create request id
RequestContext requestContext = new RequestContext();
// Try to capture the funds
try {
return chargeService.capture(requestContext, charge.getId(), capture);
} catch (FMSException e) {
throw new RuntimeException(e);
}
}
}