Skip to content

Commit 22030a5

Browse files
committed
Updated Go implementation
1 parent 20ee798 commit 22030a5

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

api/go/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using Go with RESTful routes to accept your Recurly.js
55
form submissions and use the tokens to create and update customer billing
66
information without having to handle credit card data.
77

8-
This example makes use of the official [Go client library][gp-client-library] for API v3.
8+
This example makes use of the official [Go client library][go-client-library] for API v3.
99

1010
### Routes
1111

@@ -19,7 +19,7 @@ This example makes use of the official [Go client library][gp-client-library] fo
1919

2020
1. If you haven't already, [install docker](https://www.docker.com/get-started).
2121

22-
2. Update the values in docker.env at the (root of the repo)[https://github.com/recurly/recurly-integration-examples/blob/main/docker.env]
22+
2. Update the values in docker.env at the [root of the repo](https://github.com/recurly/recurly-integration-examples/blob/main/docker.env)
2323

2424
3. Run `docker-compose up --build`
2525

@@ -30,8 +30,7 @@ This example makes use of the official [Go client library][gp-client-library] fo
3030
1. Start the server
3131

3232
```bash
33-
$ go run main.go
33+
go run main.go
3434
```
35-
2. Open [http://localhost:9001](http://localhost:9001)
3635

37-
[client]: https://github.com/recurly/recurly-client-go
36+
2. Open [http://localhost:9001](http://localhost:9001)

api/go/main.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
// API usage Dependencies
44
import (
5+
"encoding/json"
56
"fmt"
6-
"github.com/google/uuid"
7-
"github.com/recurly/recurly-client-go/v3"
87
"net/http"
98
"os"
109
"strings"
10+
11+
"github.com/google/uuid"
12+
"github.com/recurly/recurly-client-go/v3"
1113
)
1214

1315
// These are the various configuration values used in this example. They are
@@ -35,6 +37,12 @@ func createSubscription(w http.ResponseWriter, r *http.Request) {
3537
}
3638

3739
tokenId := r.FormValue("recurly-token")
40+
accountFirstName := r.FormValue("first-name")
41+
accountLastName := r.FormValue("last-name")
42+
currency := r.FormValue("currency")
43+
if currency == "" {
44+
currency = "USD"
45+
}
3846

3947
// Build the billing info body
4048
billingInfo := &recurly.BillingInfoCreate{
@@ -57,18 +65,19 @@ func createSubscription(w http.ResponseWriter, r *http.Request) {
5765
// the token we generated on the frontend
5866
purchaseCreate := &recurly.PurchaseCreate{
5967
Subscriptions: []recurly.SubscriptionPurchase{{PlanCode: recurly.String("basic")}},
60-
Currency: recurly.String("USD"),
68+
Currency: recurly.String(currency),
6169
Account: &recurly.AccountPurchase{
6270
Code: recurly.String(accountCode),
71+
FirstName: recurly.String(accountFirstName),
72+
LastName: recurly.String(accountLastName),
6373
BillingInfo: billingInfo,
6474
},
6575
}
6676

67-
_, err := client.CreatePurchase(purchaseCreate)
68-
77+
invoiceCollection, err := client.CreatePurchase(purchaseCreate)
6978
if e, ok := err.(*recurly.Error); ok {
7079
// Handle 3D Secure required error by redirecting to an authentication page
71-
if e.TransactionError.Code == "three_d_secure_action_required" {
80+
if e.TransactionError != nil && e.TransactionError.Code == "three_d_secure_action_required" {
7281
baseUrl := "/3d-secure/authenticate.html"
7382

7483
params := fmt.Sprintf(
@@ -89,7 +98,28 @@ func createSubscription(w http.ResponseWriter, r *http.Request) {
8998
http.Redirect(w, r, errorUrl, 303)
9099
}
91100
} else {
92-
// If no errors occur, redirect to the configured success URL
101+
if len(invoiceCollection.ChargeInvoice.Transactions) > 0 {
102+
103+
// TODO: Change to use invoiceCollection.ChargeInvoice.Transactions[0].ActionResult
104+
actionResult := invoiceCollection.ChargeInvoice.Transactions[0].GatewayResponseValues["action_result"]
105+
106+
if actionResult != nil {
107+
type Result struct {
108+
ActionResult interface{} `json:"action_result"`
109+
}
110+
111+
response := Result{ActionResult: actionResult}
112+
responseStr, err := json.Marshal(response)
113+
if err != nil {
114+
errorUrl := fmt.Sprintf("%s?error=%s", ERROR_URL, e.Message)
115+
http.Redirect(w, r, errorUrl, 303)
116+
} else {
117+
w.Header().Add("Content-Type", "application/json")
118+
fmt.Fprint(w, string(responseStr))
119+
}
120+
return
121+
}
122+
}
93123
http.Redirect(w, r, SUCCESS_URL, 303)
94124
}
95125

@@ -153,9 +183,15 @@ func updateAccount(w http.ResponseWriter, r *http.Request) {
153183
func config(w http.ResponseWriter, req *http.Request) {
154184
req.Header.Add("Content-Type", "application/javascript")
155185

156-
response := fmt.Sprintf("window.recurlyConfig = { publicKey: '%s' }", RECURLY_PUBLIC_KEY)
186+
recurlyConfig := fmt.Sprintf("window.recurlyConfig = { publicKey: '%s', api: \"https://api.%s/js/v1\"}",
187+
RECURLY_PUBLIC_KEY,
188+
os.Getenv("RECURLY_API_HOST"),
189+
)
190+
adyenConfig := fmt.Sprintf("window.adyenConfig = { publicKey: '%s' }", os.Getenv("ADYEN_PUBLIC_KEY"))
191+
192+
response := fmt.Sprintf("%s;\n%s;", recurlyConfig, adyenConfig)
157193

158-
fmt.Fprintf(w, response)
194+
fmt.Fprint(w, response)
159195
}
160196

161197
func main() {

0 commit comments

Comments
 (0)