99 lines
2.6 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"
2018-02-24 09:29:20 +00:00
// Success is called once the payment has been confirmed by the user on the PayPal website.
// However, the actual payment still needs to be executed and can fail.
2017-07-16 18:29:10 +00:00
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)
}
2018-02-24 08:40:42 +00:00
// Get access token
_, err = c.GetAccessToken()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not get PayPal access token", err)
}
// Execute payment
2017-07-17 01:14:05 +00:00
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(),
}
2017-10-27 07:11:56 +00:00
payment.Save()
2017-07-17 01:14:05 +00:00
// 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
2017-10-27 07:11:56 +00:00
user.Save()
2017-07-17 01:14:05 +00:00
// Notify admin
go func() {
admin, _ := arn.GetUser(adminID)
2018-02-27 12:29:47 +00:00
admin.SendNotification(&arn.PushNotification{
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,
2018-02-28 10:30:47 +00:00
Type: arn.NotificationTypePurchase,
2017-07-17 01:14:05 +00:00
})
}()
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
}