Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
payments-vendor-simulator
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
payments
payments-vendor-simulator
Commits
54870371
Commit
54870371
authored
Aug 27, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Created a DataClient for saving authorizations to CosmosDB
parent
6db64144
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
98 additions
and
1 deletion
+98
-1
build.gradle
build.gradle
+1
-0
DataServiceClient.java
...entvendorsimulator/creditcard/data/DataServiceClient.java
+81
-0
CreditCardResponse.java
...orsimulator/creditcard/domain/dto/CreditCardResponse.java
+1
-0
CreditCardService.java
...endorsimulator/creditcard/services/CreditCardService.java
+14
-0
application.properties
src/main/resources/application.properties
+1
-1
No files found.
build.gradle
View file @
54870371
...
...
@@ -21,6 +21,7 @@ repositories {
dependencies
{
implementation
'org.springframework.boot:spring-boot-starter-web:2.5.3'
implementation
'com.fasterxml.jackson.core:jackson-databind:2.12.4'
testImplementation
'org.springframework.boot:spring-boot-starter-test:2.5.3'
compileOnly
'org.projectlombok:lombok'
annotationProcessor
'org.projectlombok:lombok'
...
...
src/main/java/com/nisum/paymentvendorsimulator/creditcard/data/DataServiceClient.java
0 → 100644
View file @
54870371
package
com
.
nisum
.
paymentvendorsimulator
.
creditcard
.
data
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardResponse
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
sun.net.www.http.HttpClient
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.ProtocolException
;
import
java.net.URL
;
import
java.nio.charset.StandardCharsets
;
@Component
public
class
DataServiceClient
{
@Value
(
"${data.endpoint}"
)
private
String
dataServiceEndpoint
;
private
final
ObjectMapper
objectMapper
=
new
ObjectMapper
();
private
CreditCardResponse
convertResponse
(
String
rawResponse
)
throws
JsonProcessingException
{
CreditCardResponse
res
=
objectMapper
.
readValue
(
rawResponse
.
toString
(),
CreditCardResponse
.
class
);
return
res
;
}
public
void
saveAuthorization
(
CreditCardResponse
ccData
)
throws
IOException
{
URL
url
=
new
URL
(
dataServiceEndpoint
);
HttpURLConnection
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
connection
.
setDoOutput
(
true
);
// Attach the body
String
jsonBody
=
objectMapper
.
writeValueAsString
(
ccData
);
try
(
OutputStream
os
=
connection
.
getOutputStream
())
{
byte
[]
input
=
jsonBody
.
getBytes
(
StandardCharsets
.
UTF_8
);
os
.
write
(
input
,
0
,
input
.
length
);
}
// Read the response
try
(
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
(),
StandardCharsets
.
UTF_8
)))
{
StringBuilder
response
=
new
StringBuilder
();
String
responseLine
=
null
;
while
((
responseLine
=
br
.
readLine
())
!=
null
)
{
response
.
append
(
responseLine
.
trim
());
}
System
.
out
.
println
(
response
.
toString
());
}
connection
.
disconnect
();
}
public
CreditCardResponse
getAuthorization
(
String
id
)
throws
IOException
{
URL
url
=
new
URL
(
dataServiceEndpoint
+
"/"
+
id
);
HttpURLConnection
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
int
status
=
connection
.
getResponseCode
();
if
(
status
>=
200
&
status
<
300
)
{
System
.
out
.
println
(
"SUCCESS"
);
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
String
inputLine
;
StringBuffer
content
=
new
StringBuffer
();
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
content
.
append
(
inputLine
);
}
in
.
close
();
System
.
out
.
println
(
content
.
toString
());
CreditCardResponse
res
=
convertResponse
(
content
.
toString
());
return
res
;
}
else
{
System
.
out
.
println
(
"ERROR"
);
}
connection
.
disconnect
();
return
null
;
}
}
src/main/java/com/nisum/paymentvendorsimulator/creditcard/domain/dto/CreditCardResponse.java
View file @
54870371
...
...
@@ -11,6 +11,7 @@ import lombok.Setter;
@NoArgsConstructor
public
class
CreditCardResponse
{
String
id
;
String
cardNumber
;
String
cvv
;
Boolean
address
;
...
...
src/main/java/com/nisum/paymentvendorsimulator/creditcard/services/CreditCardService.java
View file @
54870371
package
com
.
nisum
.
paymentvendorsimulator
.
creditcard
.
services
;
import
com.nisum.paymentvendorsimulator.creditcard.data.DataServiceClient
;
import
com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardAdditionalData
;
import
com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardAuthorizeResponse
;
import
com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardRequest
;
import
com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardResponse
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.io.IOException
;
@Service
public
class
CreditCardService
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
CreditCardService
.
class
);
private
final
DataServiceClient
dataClient
;
public
CreditCardService
(
DataServiceClient
dataClient
)
{
this
.
dataClient
=
dataClient
;
}
public
CreditCardResponse
authorize
(
CreditCardRequest
ccRequest
)
{
logger
.
info
(
String
.
valueOf
(
ccRequest
));
...
...
@@ -31,6 +40,11 @@ public class CreditCardService {
res
.
setCreditCardAuthorizeResponse
(
ccAR
);
try
{
dataClient
.
saveAuthorization
(
res
);
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
);
}
return
res
;
}
}
src/main/resources/application.properties
View file @
54870371
data.endpoint
=
http://localhost:8081/api/authorizations
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment