Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetaGer Keymanager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
MetaGer Keymanager
Commits
8e1e1496
Commit
8e1e1496
authored
2 years ago
by
Dominik Hebeler
Browse files
Options
Downloads
Patches
Plain Diff
saving order details in redis
parent
ff9c9707
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pass/app/Order.js
+91
-0
91 additions, 0 deletions
pass/app/Order.js
pass/routes/checkout/checkout.js
+24
-1
24 additions, 1 deletion
pass/routes/checkout/checkout.js
pass/routes/checkout/paypal.js
+23
-30
23 additions, 30 deletions
pass/routes/checkout/paypal.js
with
138 additions
and
31 deletions
pass/app/Order.js
0 → 100644
+
91
−
0
View file @
8e1e1496
const
config
=
require
(
"
config
"
);
const
dayjs
=
require
(
"
dayjs
"
);
class
Order
{
static
get
STORAGE_KEY_PREFIX
()
{
return
"
order_
"
;
}
static
get
PURCHASE_STORAGE_TIME_MONTHS
()
{
return
6
;
}
// How many minutes is a user allowed to take for finishing the payment
static
get
PURCHASE_TIME_LIMIT_MINUTES
()
{
return
15
;
}
/**
* Data stored in Redis Database
*/
#order_id
;
#amount
;
#price
;
#encrypted_sales_receipts
;
#payment_completed
;
/**
* Data populated by context
*/
#create_mode
;
#redis_client
;
constructor
(
order_id
,
amount
,
price
,
encrypted_sales_receipts
)
{
this
.
#order_id
=
order_id
;
this
.
#amount
=
amount
;
this
.
#price
=
price
;
this
.
#encrypted_sales_receipts
=
encrypted_sales_receipts
;
this
.
#payment_completed
=
false
;
this
.
#create_mode
=
true
;
let
Redis
=
require
(
"
ioredis
"
);
this
.
#redis_client
=
new
Redis
({
host
:
config
.
get
(
"
redis.host
"
),
});
}
static
async
LOAD_ORDER_FROM_ID
(
order_id
)
{
let
Redis
=
require
(
"
ioredis
"
);
let
redis_client
=
new
Redis
({
host
:
config
.
get
(
"
redis.host
"
),
});
let
redis_key
=
Order
.
STORAGE_KEY_PREFIX
+
order_id
;
let
order_data
=
await
redis_client
.
hgetall
(
redis_key
);
console
.
log
(
order_data
);
}
async
save
()
{
let
redis_key
=
Order
.
STORAGE_KEY_PREFIX
+
this
.
#order_id
;
let
expiration
=
new
dayjs
();
if
(
this
.
#create_mode
)
{
expiration
=
expiration
.
add
(
Order
.
PURCHASE_TIME_LIMIT_MINUTES
,
"
minute
"
);
}
else
{
expiration
=
expiration
.
add
(
Order
.
PURCHASE_STORAGE_TIME_MONTHS
,
"
month
"
);
}
expiration
=
Math
.
round
(
expiration
.
diff
()
/
1000
);
let
key_exists
=
await
this
.
#redis_client
.
exists
(
redis_key
);
if
(
this
.
#create_mode
&&
key_exists
)
{
return
Promise
.
reject
(
"
Cannot create a Order that already exists!
"
);
}
let
storage_promise
=
this
.
#redis_client
.
hmset
(
redis_key
,
{
order_id
:
this
.
#order_id
,
amount
:
this
.
#amount
,
price
:
this
.
#price
,
encrypted_sales_receipts
:
this
.
#encrypted_sales_receipts
,
payment_completed
:
this
.
#payment_completed
,
})
.
then
((
result
)
=>
{
this
.
#create_mode
=
false
;
})
.
then
((
result
)
=>
{
this
.
#redis_client
.
expire
(
redis_key
,
expiration
);
});
await
storage_promise
;
}
}
module
.
exports
=
Order
;
This diff is collapsed.
Click to expand it.
pass/routes/checkout/checkout.js
+
24
−
1
View file @
8e1e1496
const
BlindSignature
=
require
(
"
blind-signatures
"
);
const
BlindSignature
=
require
(
"
blind-signatures
"
);
const
Crypto
=
require
(
"
../../app/Crypto.js
"
);
const
Crypto
=
require
(
"
../../app/Crypto.js
"
);
const
Order
=
require
(
"
../../app/Order.js
"
);
const
config
=
require
(
"
config
"
);
const
config
=
require
(
"
config
"
);
const
price_for_100
=
1
;
const
price_for_100
=
1
;
...
@@ -115,7 +116,29 @@ router.use(
...
@@ -115,7 +116,29 @@ router.use(
if
(
!
errors
.
isEmpty
())
{
if
(
!
errors
.
isEmpty
())
{
return
res
.
status
(
400
).
json
({
errors
:
errors
.
array
()
});
return
res
.
status
(
400
).
json
({
errors
:
errors
.
array
()
});
}
}
next
(
"
route
"
);
// Order data is validated: Create and store the order in the redis database
let
order
=
new
Order
(
req
.
body
.
order_id
,
req
.
body
.
amount
,
req
.
body
.
price
,
req
.
body
.
encrypted_sales_receipts
);
order
.
save
()
.
then
(()
=>
{
// Order created on our side. Continue the payment with the selected provider
next
(
"
route
"
);
})
.
catch
((
reason
)
=>
{
return
res
.
status
(
400
).
json
({
errors
:
[
{
msg
:
reason
,
},
],
});
});
}
}
);
);
...
...
This diff is collapsed.
Click to expand it.
pass/routes/checkout/paypal.js
+
23
−
30
View file @
8e1e1496
...
@@ -9,10 +9,8 @@ const base = "https://api-m.sandbox.paypal.com";
...
@@ -9,10 +9,8 @@ const base = "https://api-m.sandbox.paypal.com";
/* Client initiates payment */
/* Client initiates payment */
router
.
post
(
"
/
"
,
async
(
req
,
res
,
next
)
=>
{
router
.
post
(
"
/
"
,
async
(
req
,
res
,
next
)
=>
{
res
.
status
(
500
);
// All Submitted data is verified at this point
res
.
json
([
"
Disabling Order creating temporarily
"
]);
const
order
=
await
createOrder
(
req
);
return
;
const
order
=
await
createOrder
();
res
.
json
(
order
);
res
.
json
(
order
);
});
});
...
@@ -26,11 +24,30 @@ module.exports = router;
...
@@ -26,11 +24,30 @@ module.exports = router;
// use the orders api to create an order
// use the orders api to create an order
async
function
createOrder
()
{
async
function
createOrder
(
req
)
{
const
accessToken
=
await
generateAccessToken
();
const
accessToken
=
await
generateAccessToken
();
const
url
=
`
${
base
}
/v2/checkout/orders`
;
const
url
=
`
${
base
}
/v2/checkout/orders`
;
let
order
=
{
intent
:
"
CAPTURE
"
,
purchase_units
:
[
{
description
:
"
MetaGer Pass Einkauf
"
,
amount
:
{
currency_code
:
"
EUR
"
,
value
:
req
.
body
.
price
.
toFixed
(
2
),
},
},
],
application_context
:
{
brand_name
:
"
SUMA-EV
"
,
shipping_preference
:
"
NO_SHIPPING
"
,
},
};
console
.
log
(
order
.
purchase_units
[
0
].
amount
);
const
response
=
await
fetch
(
url
,
{
const
response
=
await
fetch
(
url
,
{
method
:
"
post
"
,
method
:
"
post
"
,
...
@@ -39,31 +56,7 @@ async function createOrder() {
...
@@ -39,31 +56,7 @@ async function createOrder() {
Authorization
:
`Bearer
${
accessToken
}
`
,
Authorization
:
`Bearer
${
accessToken
}
`
,
},
},
body
:
JSON
.
stringify
({
body
:
JSON
.
stringify
(
order
),
intent
:
"
CAPTURE
"
,
purchase_units
:
[
{
description
:
"
Test Payment
"
,
amount
:
{
currency_code
:
"
EUR
"
,
value
:
"
4.00
"
,
},
},
],
payment_source
:
{
paypal
:
{
experience_context
:
{
payment_method_preference
:
"
IMMEDIATE_PAYMENT_REQUIRED
"
,
payment_method_selected
:
"
PAYPAL
"
,
user_action
:
"
PAY_NOW
"
,
brand_name
:
"
EXAMPLE INC
"
,
shipping_preference
:
"
NO_SHIPPING
"
,
},
},
},
}),
});
});
const
data
=
await
response
.
json
();
const
data
=
await
response
.
json
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment