76 lines
1.8 KiB
Go
Raw Normal View History

2017-07-16 07:50:57 +02:00
package paypal
import (
"net/http"
"github.com/aerogo/aero"
2017-07-16 20:29:10 +02:00
"github.com/animenotifier/arn"
2017-07-17 03:14:05 +02:00
"github.com/animenotifier/notify.moe/utils"
2017-07-16 07:50:57 +02:00
"github.com/logpacker/PayPal-Go-SDK"
)
2018-02-24 10:29:20 +01:00
// CreatePayment creates the PayPal payment, typically via a JSON API route.
2017-07-16 07:50:57 +02:00
func CreatePayment(ctx *aero.Context) string {
2018-02-24 10:29:20 +01:00
// Make sure the user is logged in
2017-07-17 03:14:05 +02:00
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
}
2018-02-24 10:29:20 +01:00
// Verify amount
2017-10-14 15:41:31 +02:00
amount, err := ctx.Request().Body().String()
if err != nil {
return ctx.Error(http.StatusBadRequest, "Could not read amount", err)
}
2017-10-05 12:36:26 +02:00
switch amount {
2018-03-04 19:26:15 +01:00
case "1000", "2000", "3000", "6000", "12000", "25000", "50000", "75000":
2017-10-05 12:36:26 +02:00
// OK
default:
return ctx.Error(http.StatusBadRequest, "Incorrect amount", nil)
}
// Initiate PayPal client
2017-07-16 20:29:10 +02:00
c, err := arn.PayPal()
2017-07-16 07:50:57 +02:00
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not initiate PayPal client", err)
}
2017-10-05 12:36:26 +02:00
// Get access token
2017-07-16 07:50:57 +02:00
_, err = c.GetAccessToken()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not get PayPal access token", err)
}
2017-10-05 12:36:26 +02:00
// Create payment
2017-07-16 20:29:10 +02:00
p := paypalsdk.Payment{
Intent: "sale",
Payer: &paypalsdk.Payer{
PaymentMethod: "paypal",
},
Transactions: []paypalsdk.Transaction{paypalsdk.Transaction{
Amount: &paypalsdk.Amount{
2017-10-06 05:53:49 +02:00
Currency: "JPY",
Total: amount,
2017-07-16 20:29:10 +02:00
},
2017-07-17 03:14:05 +02:00
Description: "Top Up Balance",
2017-07-16 20:29:10 +02:00
}},
RedirectURLs: &paypalsdk.RedirectURLs{
ReturnURL: "https://" + ctx.App.Config.Domain + "/paypal/success",
CancelURL: "https://" + ctx.App.Config.Domain + "/paypal/cancel",
},
2017-07-16 07:50:57 +02:00
}
2017-07-16 20:29:10 +02:00
paymentResponse, err := c.CreatePayment(p)
2017-07-16 07:50:57 +02:00
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not create PayPal payment", err)
}
2017-07-16 20:29:10 +02:00
return ctx.JSON(paymentResponse)
2017-07-16 07:50:57 +02:00
}