-
Notifications
You must be signed in to change notification settings - Fork 0
/
order-paid-webhook.go
72 lines (62 loc) · 2.05 KB
/
order-paid-webhook.go
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
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dbryla/shoper-to-bioplanet/netlify/functions/order-paid-webhook/bioplanet"
"github.com/dbryla/shoper-to-bioplanet/netlify/functions/order-paid-webhook/checksum"
"github.com/dbryla/shoper-to-bioplanet/netlify/functions/order-paid-webhook/transformer"
"os"
"time"
)
var shoperApiKey = os.Getenv("SHOPER_API_KEY")
var bioPlanetApiKey = os.Getenv("BIO_PLANET_API_KEY")
var bioPlanetClientId = os.Getenv("BIO_PLANET_CLIENT_ID")
const TimestampFormat = "2006-01-02 15:04:05"
func handler(request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
if isNotAuthenticated(request) {
return &events.APIGatewayProxyResponse{
StatusCode: 401,
}, nil
}
_, err := createOrderInBioPlanet(request)
if err != nil {
return &events.APIGatewayProxyResponse{
StatusCode: 500,
}, err
}
return &events.APIGatewayProxyResponse{
StatusCode: 200,
}, nil
}
func isNotAuthenticated(request events.APIGatewayProxyRequest) bool {
return checksum.CalculateWebhookChecksum(request, shoperApiKey) != request.Headers["x-webhook-sha1"]
}
func createOrderInBioPlanet(request events.APIGatewayProxyRequest) (*bioplanet.OrderConfirmation, error) {
order, err := transformer.ToBioPlanetOrder(request)
if err != nil {
return nil, err
}
token, err := getBioPlanetApiToken()
if err != nil {
return nil, err
}
orderConfirmation, err := bioplanet.CreateOrder(*token, *order)
if err != nil {
fmt.Printf("Failed order creation: %+v\n", order)
return nil, err
}
fmt.Printf("Order created no. %d\n", orderConfirmation.OrderId)
return orderConfirmation, nil
}
func getBioPlanetApiToken() (*bioplanet.ApiToken, error) {
utcTimeNow := time.Now().UTC().Format(TimestampFormat)
return bioplanet.GetApiToken(bioplanet.ApiTokenPost{
Hash: checksum.CalculateTokenPostChecksum(bioPlanetApiKey, utcTimeNow, bioPlanetClientId),
ClientId: transformer.ToInt(bioPlanetClientId),
Timestamp: utcTimeNow,
})
}
func main() {
lambda.Start(handler)
}