@@ -2,12 +2,14 @@ package main
22
33// API usage Dependencies
44import (
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) {
153183func 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
161197func main () {
0 commit comments