98 lines
2.4 KiB
Go
Raw Normal View History

2017-07-16 18:29:10 +00:00
package paypal
import (
2017-07-17 01:14:05 +00:00
"errors"
2017-07-16 18:29:10 +00:00
"net/http"
2017-07-17 01:14:05 +00:00
"strconv"
2017-07-16 18:29:10 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
2017-07-17 01:14:05 +00:00
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
2017-07-16 18:29:10 +00:00
)
2017-07-17 01:14:05 +00:00
const adminID = "4J6qpK1ve"
2017-07-16 18:29:10 +00:00
// Success ...
func Success(ctx *aero.Context) string {
2017-07-17 01:14:05 +00:00
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
}
2017-07-16 18:29:10 +00:00
paymentID := ctx.Query("paymentId")
2017-07-17 01:14:05 +00:00
token := ctx.Query("token")
payerID := ctx.Query("PayerID")
if paymentID == "" || payerID == "" || token == "" {
return ctx.Error(http.StatusBadRequest, "Invalid parameters", errors.New("paymentId, token and PayerID are required"))
}
2017-07-16 18:29:10 +00:00
c, err := arn.PayPal()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not initiate PayPal client", err)
}
2017-07-17 01:14:05 +00:00
c.SetAccessToken(token)
execute, err := c.ExecuteApprovedPayment(paymentID, payerID)
2017-07-16 18:29:10 +00:00
if err != nil {
2017-07-17 01:14:05 +00:00
return ctx.Error(http.StatusInternalServerError, "Error executing PayPal payment", err)
2017-07-16 18:29:10 +00:00
}
2017-07-17 01:14:05 +00:00
if execute.State != "approved" {
return ctx.Error(http.StatusInternalServerError, "PayPal payment has not been approved", err)
}
sdkPayment, err := c.GetPayment(paymentID)
2017-07-16 18:29:10 +00:00
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not retrieve payment information", err)
}
2017-07-17 01:14:05 +00:00
arn.PrettyPrint(sdkPayment)
transaction := sdkPayment.Transactions[0]
payment := &arn.PayPalPayment{
ID: paymentID,
PayerID: payerID,
UserID: user.ID,
Method: sdkPayment.Payer.PaymentMethod,
Amount: transaction.Amount.Total,
Currency: transaction.Amount.Currency,
Created: arn.DateTimeUTC(),
}
err = payment.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not save payment in the database", err)
}
// Increase user's balance
2017-10-05 08:26:07 +00:00
user.Balance += payment.Gems()
2017-07-17 01:14:05 +00:00
// Save in DB
err = user.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not save new balance", err)
}
// Notify admin
go func() {
admin, _ := arn.GetUser(adminID)
admin.SendNotification(&arn.Notification{
2017-10-06 11:58:08 +00:00
Title: user.Nick + " bought " + strconv.Itoa(payment.Gems()) + " gems",
2017-07-17 01:14:05 +00:00
Message: user.Nick + " paid " + payment.Amount + " " + payment.Currency + " making his new balance " + strconv.Itoa(user.Balance),
Icon: user.LargeAvatar(),
Link: "https://" + ctx.App.Config.Domain + "/api/paypalpayment/" + payment.ID,
})
}()
2017-07-16 18:29:10 +00:00
2017-07-17 01:14:05 +00:00
return ctx.HTML(components.PayPalSuccess(payment))
2017-07-16 18:29:10 +00:00
}