-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaymentExample.java
More file actions
155 lines (120 loc) · 4.48 KB
/
PaymentExample.java
File metadata and controls
155 lines (120 loc) · 4.48 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
/**
*
* @category bluevia
* @package com.bluevia.examples
* @copyright Copyright (c) 2010 Telefonica I+D (http://www.tid.es)
* @author Bluevia <support@bluevia.com>
*
*
* BlueVia is a global iniciative of Telefonica delivered by Movistar
* and O2. Please, check out http://www.bluevia.com and if you need
* more information contact us at support@bluevia.com
*/
import java.io.IOException;
import java.util.Scanner;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import com.bluevia.commons.client.BVBaseClient.Mode;
import com.bluevia.commons.connector.http.oauth.OAuthToken;
import com.bluevia.commons.connector.http.oauth.RequestToken;
import com.bluevia.commons.exception.BlueviaException;
import com.bluevia.payment.client.BVPayment;
import com.bluevia.payment.data.PaymentResult;
import com.bluevia.payment.data.PaymentStatus;
import com.bluevia.payment.data.PaymentStatus.TransactionStatusType;
/**
* Advertising API Class Examples
*
* @package com.bluevia.examples
*/
public class PaymentExample {
// Logger
private static Logger log = Logger.getLogger(PaymentExample.class.getName());
// API path (Mode Live/Sandbox)
public static Mode mode = Mode.SANDBOX;
// CREDENTIALS:
// User must DEFINE VALID VALUES FOR consumer token
// Consumer Key - Consumer Token
public static OAuthToken consumerToken = new OAuthToken("vw12012654505986", "WpOl66570544");
public static void main(String[] args) {
// Logger
BasicConfigurator.configure();
payment();
//cancel();
}
/**
* Payment API - Payment example
*
*/
public static void payment() {
System.out.println("***** PaymentExample payment operation");
BVPayment client = null;
try {
RequestToken response = null;
OAuthToken access = null;
String serviceName = "bluevia";
String serviceId = "service_id";
String callback = "http://www.example.com";
int amount = 177;
String currency = "EUR";
client = new BVPayment(mode,
consumerToken.getToken(), consumerToken.getSecret());
// Get credentials
response = client.getPaymentRequestToken(amount, currency, serviceName, serviceId, callback);
System.out.println("Token: " + response.getToken());
System.out.println("Secret: " + response.getSecret());
System.out.println("Url: " + response.getVerificationUrl());
// Waiting for verifier
System.out.println("oauth_verifier: ");
Scanner in = new Scanner(System.in);
String oauth_verifier = in.nextLine();
// Get Access Token
access = client.getPaymentAccessToken(oauth_verifier, response.getToken(), response.getSecret());
System.out.println("Access Token:" + access.getToken());
System.out.println("Access Token Secret:" + access.getSecret());
// Make payment
PaymentResult res = client.payment(amount, currency);
System.out.println("transaction : " + res.getTransactionId());
System.out.println("status : " + res.getTransactionStatus());
System.out.println("description : " + res.getTransactionStatusDescription());
// Get Payment Status
System.out.println("***** PaymentExample get payment status");
String transactionId = res.getTransactionId();
PaymentStatus resStatus = client.getPaymentStatus(transactionId);
TransactionStatusType transactionStatus = resStatus.getTransactionStatus();
System.out.println("transaction status : " + transactionStatus.toString());
} catch (BlueviaException e){
log.error(e.getMessage());
} catch (NumberFormatException e){
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} finally {
// Close the client
if (client != null) client.close();
}
}
/**
* Payment API - Cancel example
* @throws JAXBException
* @throws BlueviaException
*
*/
public static void cancel() {
System.out.println("***** PaymentExample cancel authorization");
BVPayment client = null;
try {
client = new BVPayment(mode,
consumerToken.getToken(), consumerToken.getSecret());
client.cancelAuthorization();
System.out.println("Authorization cancelled");
} catch (BlueviaException ex) {
log.error("BlueviaException:" + ex.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} finally {
// Close the client
if (client != null) client.close();
}
}
}