Added paypal callbacks
This commit is contained in:
parent
649672429d
commit
d33b93d115
4
main.go
4
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
|
||||
|
15
pages/paypal/cancel.go
Normal file
15
pages/paypal/cancel.go
Normal file
@ -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")
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
37
pages/paypal/success.go
Normal file
37
pages/paypal/success.go
Normal file
@ -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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user