Skip to content

Commit

Permalink
Updated Go implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaslise committed Aug 1, 2023
1 parent 20ee798 commit 17ce51b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
9 changes: 4 additions & 5 deletions api/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Go with RESTful routes to accept your Recurly.js
form submissions and use the tokens to create and update customer billing
information without having to handle credit card data.

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

### Routes

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

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

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

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

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

```bash
$ go run main.go
go run main.go
```
2. Open [http://localhost:9001](http://localhost:9001)

[client]: https://github.com/recurly/recurly-client-go
2. Open [http://localhost:9001](http://localhost:9001)
54 changes: 45 additions & 9 deletions api/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

// API usage Dependencies
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/recurly/recurly-client-go/v3"
"net/http"
"os"
"strings"

"github.com/google/uuid"
"github.com/recurly/recurly-client-go/v3"
)

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

tokenId := r.FormValue("recurly-token")
accountFirstName := r.FormValue("first-name")
accountLastName := r.FormValue("last-name")
currency := r.FormValue("currency")
if currency == "" {
currency = "USD"
}

// Build the billing info body
billingInfo := &recurly.BillingInfoCreate{
Expand All @@ -57,18 +65,19 @@ func createSubscription(w http.ResponseWriter, r *http.Request) {
// the token we generated on the frontend
purchaseCreate := &recurly.PurchaseCreate{
Subscriptions: []recurly.SubscriptionPurchase{{PlanCode: recurly.String("basic")}},
Currency: recurly.String("USD"),
Currency: recurly.String(currency),
Account: &recurly.AccountPurchase{
Code: recurly.String(accountCode),
FirstName: recurly.String(accountFirstName),
LastName: recurly.String(accountLastName),
BillingInfo: billingInfo,
},
}

_, err := client.CreatePurchase(purchaseCreate)

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

params := fmt.Sprintf(
Expand All @@ -89,7 +98,28 @@ func createSubscription(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, errorUrl, 303)
}
} else {
// If no errors occur, redirect to the configured success URL
if len(invoiceCollection.ChargeInvoice.Transactions) > 0 {

// TODO: Change to use invoiceCollection.ChargeInvoice.Transactions[0].ActionResult
actionResult := invoiceCollection.ChargeInvoice.Transactions[0].GatewayResponseValues["action_result"]

if actionResult != nil {
type Result struct {
ActionResult interface{} `json:"action_result"`
}

response := Result{ActionResult: actionResult}
responseStr, err := json.Marshal(response)
if err != nil {
errorUrl := fmt.Sprintf("%s?error=%s", ERROR_URL, e.Message)
http.Redirect(w, r, errorUrl, 303)
} else {
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(responseStr))
}
return
}
}
http.Redirect(w, r, SUCCESS_URL, 303)
}

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

response := fmt.Sprintf("window.recurlyConfig = { publicKey: '%s' }", RECURLY_PUBLIC_KEY)
recurlyConfig := fmt.Sprintf("window.recurlyConfig = { publicKey: '%s', api: 'https://api.%s/js/v1'}",
RECURLY_PUBLIC_KEY,
os.Getenv("RECURLY_API_HOST"),
)
adyenConfig := fmt.Sprintf("window.adyenConfig = { publicKey: '%s' }", os.Getenv("ADYEN_PUBLIC_KEY"))

response := fmt.Sprintf("%s;\n%s;", recurlyConfig, adyenConfig)

fmt.Fprintf(w, response)
fmt.Fprint(w, response)
}

func main() {
Expand Down

0 comments on commit 17ce51b

Please sign in to comment.