Skip to content
This repository was archived by the owner on Apr 19, 2022. It is now read-only.

Commit 57552c0

Browse files
committed
Widgets adapted to work without TPA credentials.
1 parent 802dd6a commit 57552c0

File tree

5 files changed

+312
-271
lines changed

5 files changed

+312
-271
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Micro payment widgets allow to dynamically get a HTML snippet pre-configured and
5353
- **Pay Object's Id:** `[mandatory]` any unique identifier in the context of the TPA distinguishing the object of the payment.
5454
- **Pay type:** `[optional]` any of Donate | Pay | Tip | Deposit.
5555

56+
Be aware that micro payments could be optionally configured with your own application id and secret (`app_id`/`app_secret`). Configuring the micro payment with your application credentials allows you to charge a transaction fee for example.
57+
5658
### IFrame Widget
5759
```java
5860
import com.xapo.tools.widgets.MicroPayment;

java-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.xapo</groupId>
77
<artifactId>java-api</artifactId>
8-
<version>0.1.0</version>
8+
<version>0.2.0</version>
99
<packaging>jar</packaging>
1010

1111

java-api/src/main/java/com/xapo/tools/widgets/MicroPayment.java

+150-141
Original file line numberDiff line numberDiff line change
@@ -7,146 +7,155 @@
77

88
public class MicroPayment {
99

10-
private ServiceParameters serviceParameters;
11-
private String appID;
12-
private String appSecret;
13-
private JsonMarshaller jsonMarshaller = new JsonMarshaller();
14-
private MCrypt aesEncrypt = new MCrypt();
15-
16-
/**
17-
* Constructor
18-
*
19-
* @param serviceURL
20-
* The service URL. e.g.
21-
* "http://dev.xapo.com:8089/pay_button/show"
22-
* @param appID
23-
* The ID of the application that uses this SDK
24-
* @param appSecret
25-
* the encryption secret key of the application that uses this
26-
* SDK
27-
*/
28-
public MicroPayment(String serviceURL, String appID, String appSecret) {
29-
30-
this.appID = appID;
31-
this.appSecret = appSecret;
32-
this.serviceParameters = new ServiceParameters(serviceURL);
33-
34-
}
35-
36-
/**
37-
* Encrypts the data to be sent to the server
38-
*
39-
* @param data
40-
* the String to be encrypted
41-
* @return the encrypted data
42-
*/
43-
protected String encrypt(String data) {
44-
try {
45-
return aesEncrypt.encrypt(appSecret, data);
46-
} catch (Exception e) {
47-
throw new RuntimeException("Can't encrypt the data", e);
48-
49-
}
50-
}
51-
52-
/**
53-
* Build the URL based in the request data
54-
*
55-
* @param request
56-
* the request to be sent to the server
57-
* @return the URL to send the request data
58-
*/
59-
protected String buildWidgetUrl(MicroPaymentConfig request) {
60-
61-
long timestamp = System.currentTimeMillis();
62-
String buttonRequestJson = jsonMarshaller.getJson(request, timestamp);
63-
String buttonRequestEnc = encrypt(buttonRequestJson);
64-
URI widgetUrl = null;
65-
String widgetStr = "";
66-
67-
StringBuilder query = new StringBuilder();
68-
query.append("customization={\"button_text\":\"");
69-
query.append(request.getPayType());
70-
query.append("\"}");
71-
72-
query.append("&");
73-
query.append("app_id=");
74-
query.append(appID);
75-
76-
query.append("&");
77-
query.append("button_request=");
78-
buttonRequestEnc = buttonRequestEnc.replace("=", "%3D");
79-
query.append(buttonRequestEnc);
80-
81-
String queryStr = query.toString();
82-
83-
queryStr = queryStr.replace("/", "%2F");
84-
queryStr = queryStr.replace("\"", "%22");
85-
queryStr = queryStr.replace("{", "%7B");
86-
queryStr = queryStr.replace("}", "%7D");
87-
queryStr = queryStr.replace(" ", "%20");
88-
queryStr = queryStr.replace("+", "%2B");
89-
queryStr = queryStr.replace(":", "%3A");
90-
queryStr = queryStr.replace("\n", "%0A");
91-
92-
widgetStr = serviceParameters.getScheme() + "://" +
93-
serviceParameters.getHost() + ":" + serviceParameters.getPort() +
94-
serviceParameters.getPath() + "?" + queryStr;
95-
96-
// fix : encode
97-
return widgetStr;
98-
}
99-
100-
protected URI createURI(String query) {
101-
try {
102-
return new URI(serviceParameters.getScheme(), null /*userInfo*/,
103-
serviceParameters.getHost(), serviceParameters.getPort(), serviceParameters.getPath(), query, null /* fragment */);
104-
105-
} catch (Exception e) {
106-
throw new RuntimeException("Can't create URL", e);
107-
}
108-
109-
}
110-
111-
/**
112-
* Builds a Div HTML tag including the request data
113-
*
114-
* @param microPaymentConfig
115-
* the data to sent to the server
116-
* @return the HTML tag string
117-
*/
118-
public String buildDivWidget(MicroPaymentConfig microPaymentConfig) {
119-
String widgetUrl = buildWidgetUrl(microPaymentConfig);
120-
StringBuffer res = new StringBuffer();
121-
res.append("<div id='tipButtonDiv' class='tipButtonDiv'></div>\n");
122-
res.append("<div id='tipButtonPopup' class='tipButtonPopup'></div>\n");
123-
res.append("<script>\n");
124-
res.append("$(document).ready(function() {");
125-
res.append("$('#tipButtonDiv').load('");
126-
res.append(widgetUrl);
127-
res.append("');");
128-
res.append("});\n");
129-
res.append("</script>");
130-
131-
return res.toString();
132-
}
133-
134-
/**
135-
* Builds an iFrame HTML tag including the request data
136-
*
137-
* @param microPaymentConfig
138-
* the data to configure de payment button
139-
* @return the HTML tag string
140-
*/
141-
public String buildIframeWidget(MicroPaymentConfig microPaymentConfig) {
142-
143-
String widgetUrl = buildWidgetUrl(microPaymentConfig);
144-
StringBuffer res = new StringBuffer();
145-
res.append("<iframe id='tipButtonFrame' scrolling='no' frameborder='0' style='border:none; overflow:hidden; height:22px;' allowTransparency='true' src='");
146-
147-
res.append(widgetUrl);
148-
res.append("'></iframe>");
149-
return res.toString();
150-
}
10+
private ServiceParameters serviceParameters;
11+
private String appID;
12+
private String appSecret;
13+
private JsonMarshaller jsonMarshaller = new JsonMarshaller();
14+
private MCrypt aesEncrypt = new MCrypt();
15+
16+
/**
17+
* Constructor. Configure a payment button widget with TPA credentials.
18+
*
19+
* @param serviceURL The service URL. e.g.
20+
* "http://dev.xapo.com:8089/pay_button/show"
21+
* @param appID The ID of the application that uses this SDK
22+
* @param appSecret the encryption secret key of the application that uses this
23+
* SDK
24+
*/
25+
public MicroPayment(String serviceURL, String appID, String appSecret) {
26+
this.appID = appID;
27+
this.appSecret = appSecret;
28+
this.serviceParameters = new ServiceParameters(serviceURL);
29+
}
30+
31+
/**
32+
* Constructor. Configure a payment button without a TPA.
33+
*
34+
* @param serviceURL The service URL. e.g.
35+
* "http://dev.xapo.com:8089/pay_button/show"
36+
*/
37+
public MicroPayment(String serviceURL) {
38+
this.serviceParameters = new ServiceParameters(serviceURL);
39+
}
40+
41+
42+
/**
43+
* Encrypts the data to be sent to the server
44+
*
45+
* @param data the String to be encrypted
46+
* @return the encrypted data
47+
*/
48+
protected String encrypt(String data) {
49+
try {
50+
return aesEncrypt.encrypt(appSecret, data);
51+
} catch (Exception e) {
52+
throw new RuntimeException("Can't encrypt the data", e);
53+
54+
}
55+
}
56+
57+
/**
58+
* Build the URL based in the request data
59+
*
60+
* @param request the request to be sent to the server
61+
* @return the URL to send the request data
62+
*/
63+
protected String buildWidgetUrl(MicroPaymentConfig request) {
64+
65+
long timestamp = System.currentTimeMillis();
66+
String buttonRequestJson = jsonMarshaller.getJson(request, timestamp);
67+
URI widgetUrl = null;
68+
String widgetStr = "";
69+
70+
StringBuilder query = new StringBuilder();
71+
query.append("customization={\"button_text\":\"");
72+
query.append(request.getPayType());
73+
query.append("\"}");
74+
75+
if (this.appID == null || this.appSecret == null) {
76+
query.append("&");
77+
query.append("payload=");
78+
query.append(buttonRequestJson);
79+
} else {
80+
String buttonRequestEnc = encrypt(buttonRequestJson);
81+
82+
query.append("&");
83+
query.append("app_id=");
84+
query.append(appID);
85+
86+
query.append("&");
87+
query.append("button_request=");
88+
buttonRequestEnc = buttonRequestEnc.replace("=", "%3D");
89+
query.append(buttonRequestEnc);
90+
}
91+
92+
String queryStr = query.toString();
93+
94+
queryStr = queryStr.replace("/", "%2F");
95+
queryStr = queryStr.replace("\"", "%22");
96+
queryStr = queryStr.replace("{", "%7B");
97+
queryStr = queryStr.replace("}", "%7D");
98+
queryStr = queryStr.replace(" ", "%20");
99+
queryStr = queryStr.replace("+", "%2B");
100+
queryStr = queryStr.replace(":", "%3A");
101+
queryStr = queryStr.replace("\n", "%0A");
102+
103+
widgetStr = serviceParameters.getScheme() + "://" +
104+
serviceParameters.getHost() + ":" + serviceParameters.getPort() +
105+
serviceParameters.getPath() + "?" + queryStr;
106+
107+
// fix : encode
108+
return widgetStr;
109+
}
110+
111+
protected URI createURI(String query) {
112+
try {
113+
return new URI(serviceParameters.getScheme(), null /*userInfo*/,
114+
serviceParameters.getHost(), serviceParameters.getPort(), serviceParameters.getPath(), query, null /* fragment */);
115+
116+
} catch (Exception e) {
117+
throw new RuntimeException("Can't create URL", e);
118+
}
119+
120+
}
121+
122+
/**
123+
* Builds a Div HTML tag including the request data
124+
*
125+
* @param microPaymentConfig the data to sent to the server
126+
* @return the HTML tag string
127+
*/
128+
public String buildDivWidget(MicroPaymentConfig microPaymentConfig) {
129+
String widgetUrl = buildWidgetUrl(microPaymentConfig);
130+
StringBuffer res = new StringBuffer();
131+
res.append("<div id='tipButtonDiv' class='tipButtonDiv'></div>\n");
132+
res.append("<div id='tipButtonPopup' class='tipButtonPopup'></div>\n");
133+
res.append("<script>\n");
134+
res.append("$(document).ready(function() {");
135+
res.append("$('#tipButtonDiv').load('");
136+
res.append(widgetUrl);
137+
res.append("');");
138+
res.append("});\n");
139+
res.append("</script>");
140+
141+
return res.toString();
142+
}
143+
144+
/**
145+
* Builds an iFrame HTML tag including the request data
146+
*
147+
* @param microPaymentConfig the data to configure de payment button
148+
* @return the HTML tag string
149+
*/
150+
public String buildIframeWidget(MicroPaymentConfig microPaymentConfig) {
151+
152+
String widgetUrl = buildWidgetUrl(microPaymentConfig);
153+
StringBuffer res = new StringBuffer();
154+
res.append("<iframe id='tipButtonFrame' scrolling='no' frameborder='0' style='border:none; overflow:hidden; height:22px;' allowTransparency='true' src='");
155+
156+
res.append(widgetUrl);
157+
res.append("'></iframe>");
158+
return res.toString();
159+
}
151160

152161
}

0 commit comments

Comments
 (0)