This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
/
InvoiceSample.java
241 lines (221 loc) · 7.52 KB
/
InvoiceSample.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.paypal.api.sample;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.paypal.api.payments.CancelNotification;
import com.paypal.api.payments.Image;
import com.paypal.api.payments.Invoice;
import com.paypal.api.payments.Invoices;
import com.paypal.api.payments.Notification;
import com.paypal.api.payments.Search;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
/**
* This class shows code samples for invoicing.
* l
* https://developer.paypal.com/webapps/developer/docs/api/#invoicing
*
*/
public class InvoiceSample extends SampleBase<Invoice> {
public static final String clientID = "AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS";
public static final String clientSecret = "EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL";
/**
* Initialize and instantiate an Invoice object
* @throws PayPalRESTException
* @throws JsonSyntaxException
* @throws JsonIOException
* @throws FileNotFoundException
*/
public InvoiceSample() throws PayPalRESTException, JsonSyntaxException,
JsonIOException, FileNotFoundException {
super(new Invoice());
}
/**
* Create an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#create-an-invoice
*
* @return newly created Invoice instance
* @throws PayPalRESTException
*/
public Invoice create(APIContext context) throws PayPalRESTException, IOException {
// populate Invoice object that we are going to play with
super.instance = load("invoice_create.json", Invoice.class);
super.instance = super.instance.create(context);
return super.instance;
}
/**
* Send an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#send-an-invoice
*
* @throws PayPalRESTException
*/
public void send(APIContext context) throws PayPalRESTException {
super.instance.send(context);
}
/**
* Update an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#update-an-invoice
*
* @return updated Invoice instance
* @throws PayPalRESTException
*/
public Invoice update(APIContext context) throws PayPalRESTException, IOException {
String id = super.instance.getId();
super.instance = load("invoice_update.json", Invoice.class);
super.instance.setId(id);
// Changing the number of invoice to some random value
super.instance.setNumber(UUID.randomUUID().toString().substring(0, 5));
super.instance = super.instance.update(context);
return super.instance;
}
/**
* Retrieve an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#retrieve-an-invoice
*
* @return retrieved Invoice instance
* @throws PayPalRESTException
*/
public Invoice retrieve(APIContext context) throws PayPalRESTException {
super.instance = Invoice.get(context, super.instance.getId());
return super.instance;
}
/**
* Get invoices of an merchant.
*
* https://developer.paypal.com/webapps/developer/docs/api/#get-invoices-of-a-merchant
*
* @return Invoices instance that contains invoices for merchant
* @throws PayPalRESTException
*/
public Invoices getMerchantInvoices(APIContext context) throws PayPalRESTException {
// Lets add some options.
Map<String, String> options = new HashMap<String, String>() {{
put("total_count_required", "true");
}};
return Invoice.getAll(context, options);
}
/**
* Search for invoices.
*
* https://developer.paypal.com/webapps/developer/docs/api/#search-for-invoices
*
* @return Invoices instance that contains found invoices
* @throws PayPalRESTException
*/
public Invoices search(APIContext context) throws PayPalRESTException {
Search search = new Search();
search.setStartInvoiceDate("2010-05-10 PST");
search.setEndInvoiceDate("2014-04-10 PST");
search.setPage(1);
search.setPageSize(20);
search.setTotalCountRequired(true);
return super.instance.search(context, search);
}
/**
* Send an invoice reminder.
*
* https://developer.paypal.com/webapps/developer/docs/api/#send-an-invoice-reminder
*
* @throws PayPalRESTException
*/
public void sendReminder(APIContext context) throws PayPalRESTException {
Notification notification = new Notification();
notification.setSubject("Past due");
notification.setNote("Please pay soon");
notification.setSendToMerchant(true);
super.instance.remind(context, notification);
}
/**
* Cancel an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#cancel-an-invoice
*
* @throws PayPalRESTException
*/
public void cancel(APIContext context) throws PayPalRESTException {
CancelNotification cancelNotification = new CancelNotification();
cancelNotification.setSubject("Past due");
cancelNotification.setNote("Canceling invoice");
cancelNotification.setSendToMerchant(true);
cancelNotification.setSendToPayer(true);
super.instance.cancel(context, cancelNotification);
}
/**
* Delete an invoice.
*
* https://developer.paypal.com/webapps/developer/docs/api/#delete-an-invoice
*
* @throws PayPalRESTException
*/
public void delete(APIContext context) throws PayPalRESTException, IOException {
Invoice newInvoice = this.create(context);
newInvoice.delete(context);
}
/**
* Gets a QR Code Image object.
* @param context {@link APIContext}
* @return Image
* @throws PayPalRESTException
* @throws IOException
*/
public Image getQRCode(APIContext context) throws PayPalRESTException, IOException {
//Lets add some options. These are optional, but added for demo purposes.
Map<String, String> options = new HashMap<String, String>() {{
put("width", "400");
put("height", "350");
}};
// Replace the given Id with your invoice Id.
Image image = Invoice.qrCode(context, super.instance.getId(), options);
// You can also save the image to file as shown here.
image.saveToFile("invoice.png");
return image;
}
/**
* Main method that calls all methods above in a flow.
*
* @param args
*/
public static void main(String[] args) {
try {
InvoiceSample invoiceSample = new InvoiceSample();
APIContext context = new APIContext(clientID, clientSecret, "sandbox");
invoiceSample.create(context);
System.out.println("create response:\n" + Invoice.getLastResponse());
invoiceSample.update(context);
System.out.println("update response:\n" + Invoice.getLastResponse());
invoiceSample.send(context);
System.out.println("send response:\n" + Invoice.getLastResponse());
invoiceSample.getQRCode(context);
invoiceSample.retrieve(context);
System.out.println("retrieve response:\n" + Invoice.getLastResponse());
invoiceSample.getMerchantInvoices(context);
System.out.println("getall response:\n" + Invoice.getLastResponse());
invoiceSample.search(context);
System.out.println("search response:\n" + Invoice.getLastResponse());
invoiceSample.sendReminder(context);
System.out.println("remind response:\n" + Invoice.getLastResponse());
invoiceSample.cancel(context);
System.out.println("cancel response:\n" + Invoice.getLastResponse());
invoiceSample.delete(context);
System.out.println("delete response:\n" + Invoice.getLastResponse());
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (JsonIOException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (PayPalRESTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}