From d33b93d115685ba8ed7c5f9d6fdcc4febf1014eb Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 16 Jul 2017 20:29:10 +0200 Subject: [PATCH] Added paypal callbacks --- main.go | 4 +++ pages/paypal/cancel.go | 15 +++++++++++ pages/paypal/paypal.go | 59 ++++++++++++++++++++++++++++++++--------- pages/paypal/success.go | 37 ++++++++++++++++++++++++++ tests.go | 2 ++ 5 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 pages/paypal/cancel.go create mode 100644 pages/paypal/success.go diff --git a/main.go b/main.go index d847bbf9..5f2a2019 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,10 @@ func configure(app *aero.Application) *aero.Application { // API app.Get("/api/test/notification", notifications.Test) + + // PayPal + app.Ajax("/paypal/success", paypal.Success) + app.Ajax("/paypal/cancel", paypal.Cancel) app.Get("/api/paypal/payment/create", paypal.CreatePayment) // Middleware diff --git a/pages/paypal/cancel.go b/pages/paypal/cancel.go new file mode 100644 index 00000000..d607bf5c --- /dev/null +++ b/pages/paypal/cancel.go @@ -0,0 +1,15 @@ +package paypal + +import ( + "fmt" + + "github.com/aerogo/aero" +) + +// Cancel ... +func Cancel(ctx *aero.Context) string { + token := ctx.Query("token") + fmt.Println("cancel", token) + + return ctx.HTML("cancel") +} diff --git a/pages/paypal/paypal.go b/pages/paypal/paypal.go index 6e1c1846..94adce8e 100644 --- a/pages/paypal/paypal.go +++ b/pages/paypal/paypal.go @@ -2,17 +2,15 @@ package paypal import ( "net/http" - "os" "github.com/aerogo/aero" + "github.com/animenotifier/arn" "github.com/logpacker/PayPal-Go-SDK" ) // CreatePayment ... func CreatePayment(ctx *aero.Context) string { - // Create a client instance - c, err := paypalsdk.NewClient("clientID", "secretID", paypalsdk.APIBaseSandBox) - c.SetLog(os.Stdout) // Set log to terminal stdout + c, err := arn.PayPal() if err != nil { return ctx.Error(http.StatusInternalServerError, "Could not initiate PayPal client", err) @@ -24,18 +22,55 @@ func CreatePayment(ctx *aero.Context) string { return ctx.Error(http.StatusInternalServerError, "Could not get PayPal access token", err) } - amount := paypalsdk.Amount{ - Total: "7.00", - Currency: "USD", + // webprofile := paypalsdk.WebProfile{ + // Name: "Anime Notifier", + // Presentation: paypalsdk.Presentation{ + // BrandName: "Anime Notifier", + // LogoImage: "https://notify.moe/brand/300", + // LocaleCode: "US", + // }, + + // InputFields: paypalsdk.InputFields{ + // AllowNote: true, + // NoShipping: paypalsdk.NoShippingDisplay, + // AddressOverride: paypalsdk.AddrOverrideFromCall, + // }, + + // FlowConfig: paypalsdk.FlowConfig{ + // LandingPageType: paypalsdk.LandingPageTypeBilling, + // }, + // } + + // result, err := c.CreateWebProfile(webprofile) + // c.SetWebProfile(*result) + + // if err != nil { + // return ctx.Error(http.StatusInternalServerError, "Could not create PayPal web profile", err) + // } + + p := paypalsdk.Payment{ + Intent: "sale", + Payer: &paypalsdk.Payer{ + PaymentMethod: "paypal", + }, + Transactions: []paypalsdk.Transaction{paypalsdk.Transaction{ + Amount: &paypalsdk.Amount{ + Currency: "USD", + Total: "7.00", + }, + Description: "Pro Account", + }}, + RedirectURLs: &paypalsdk.RedirectURLs{ + ReturnURL: "https://" + ctx.App.Config.Domain + "/paypal/success", + CancelURL: "https://" + ctx.App.Config.Domain + "/paypal/cancel", + }, } - redirectURI := "http://example.com/redirect-uri" - cancelURI := "http://example.com/cancel-uri" - description := "Description for this payment" - paymentResult, err := c.CreateDirectPaypalPayment(amount, redirectURI, cancelURI, description) + + paymentResponse, err := c.CreatePayment(p) if err != nil { return ctx.Error(http.StatusInternalServerError, "Could not create PayPal payment", err) } - return ctx.JSON(paymentResult) + return ctx.JSON(paymentResponse) } diff --git a/pages/paypal/success.go b/pages/paypal/success.go new file mode 100644 index 00000000..35512c4d --- /dev/null +++ b/pages/paypal/success.go @@ -0,0 +1,37 @@ +package paypal + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +// Success ... +func Success(ctx *aero.Context) string { + paymentID := ctx.Query("paymentId") + // token := ctx.Query("token") + // payerID := ctx.Query("PayerID") + + c, err := arn.PayPal() + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Could not initiate PayPal client", err) + } + + _, err = c.GetAccessToken() + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Could not get PayPal access token", err) + } + + payment, err := c.GetPayment(paymentID) + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Could not retrieve payment information", err) + } + + arn.PrettyPrint(payment) + + return ctx.HTML("success") +} diff --git a/tests.go b/tests.go index c0a6ef7f..e6176bb8 100644 --- a/tests.go +++ b/tests.go @@ -187,6 +187,8 @@ var routeTests = map[string][]string{ "/import/kitsu/animelist/finish": nil, "/api/test/notification": nil, "/api/paypal/payment/create": nil, + "/paypal/success": nil, + "/paypal/cancel": nil, "/anime/:id/edit": nil, "/new/thread": nil, "/new/soundtrack": nil,