Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify Alternative Payment Methods examples #122

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
175 changes: 0 additions & 175 deletions public/alternative-payment-methods/boleto.html

This file was deleted.

2 changes: 1 addition & 1 deletion public/alternative-payment-methods/completed.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h2>Thank you</h2>

<p>Transaction status in Recurly was completed with status "<span id="recurly-status"></span>".</p>

<a href="/">Back to home</a>
<a href="/alternative-payment-methods/index.html">Back to Alternative Payment Methods</a>
</main>

<script type="text/javascript">
Expand Down
Loading