A small project that collects various nice things to get started with Go Web Development.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.8 KiB

package features_paypal
import (
"github.com/gofiber/fiber/v2"
"github.com/plutov/paypal/v4"
"os"
"context"
. "MY/webapp/common"
config "MY/webapp/config"
"fmt"
)
func CreatePaypal() (*paypal.Client, error) {
return paypal.NewClient(
config.Settings.Paypal.ClientID,
config.Settings.Paypal.SecretID,
config.Settings.Paypal.URL) // or paypal.APIBaseLive
}
func PostApiOrder(c *fiber.Ctx) error {
pay, err := CreatePaypal()
if err != nil { return IfErrNil(err, c) }
pay.SetLog(os.Stdout)
units := []paypal.PurchaseUnitRequest{
{
ReferenceID: "myinternalid1",
Amount: &paypal.PurchaseUnitAmount{
Currency: "USD",
Value: "10.99",
},
Description: "Product description",
Items: []paypal.Item{
{
Name: "Learn Go the Hard Way",
UnitAmount: &paypal.Money{
Currency: "USD",
Value: "10.99",
},
Quantity: "1",
},
},
},
}
source := &paypal.PaymentSource{}
appCtx := &paypal.ApplicationContext{}
order, err := pay.CreateOrder(context.TODO(), paypal.OrderIntentCapture, units, source, appCtx)
fmt.Println("ORDER", order)
return c.JSON(order)
}
func PostApiOrderCapture(c *fiber.Ctx) error {
orderID := c.Params("orderID")
fmt.Println("POST ORDER CAPTURE", orderID)
pay, err := CreatePaypal()
if err != nil { return IfErrNil(err, c) }
capture, err := pay.CaptureOrder(context.TODO(), orderID, paypal.CaptureOrderRequest{})
if err != nil { return IfErrNil(err, c) }
fmt.Println("CAPTURE", capture)
return c.JSON(fiber.Map{"status": "ok"})
}
func SetupApi(app *fiber.App) {
app.Post("/api/paypal/order", PostApiOrder)
app.Post("/api/paypal/order/:orderID/capture", PostApiOrderCapture)
}