Generate a Payment response on /v1/payments

parent b74c930c
package payments
import (
"errors"
"fmt"
"math/big"
"net/http"
"os"
"github.com/davecgh/go-spew/spew"
......@@ -36,26 +38,27 @@ func (r *PaymentResource) GetParams() []*restful.Parameter {
return params
}
func paypal(context smolder.APIContext, id string) {
func paypal(context smolder.APIContext, id string) (db.Payment, error) {
ctx := context.(*db.APIContext)
p := db.Payment{}
// Create a client instance
c, err := paypalsdk.NewClient(ctx.Config.PaymentProviders.PayPal.ClientID,
ctx.Config.PaymentProviders.PayPal.Secret,
paypalsdk.APIBaseSandBox)
if err != nil {
panic(err)
return p, err
}
c.SetLog(os.Stdout)
_, err = c.GetAccessToken()
if err != nil {
panic(err)
return p, err
}
payment, err := c.GetPayment(id)
if err != nil {
panic(err)
return p, err
}
if len(payment.Transactions) == 0 {
......@@ -70,15 +73,15 @@ func paypal(context smolder.APIContext, id string) {
value, ab, err := big.ParseFloat(payment.Transactions[0].Amount.Total, 10, 2, big.ToNearestEven)
if err != nil {
spew.Dump(payment)
panic("Can't parse value of this transaction")
return p, errors.New("Can't parse value of this transaction")
}
if ab != 10 {
spew.Dump(payment)
panic("Actual base of transaction value is not 10")
return p, errors.New("Actual base of transaction value is not 10")
}
// spew.Dump(payment)
p := mq.Payment{
mp := mq.Payment{
Name: fmt.Sprintf("%s %s", payment.Payer.PayerInfo.FirstName, payment.Payer.PayerInfo.LastName),
Address: []string{
payment.Payer.PayerInfo.ShippingAddress.RecipientName,
......@@ -104,8 +107,18 @@ func paypal(context smolder.APIContext, id string) {
SourcePayerID: payment.Payer.PayerInfo.PayerID,
SourceTransactionID: payment.Transactions[0].RelatedResources[0].Sale.ID,
}
pretty.Println(p)
pretty.Println(mp)
p.Amount, _ = value.Float64()
p.Currency = payment.Transactions[0].Amount.Currency
p.Code = payment.Transactions[0].Custom
p.Description = payment.Transactions[0].Description
p.Source = "paypal"
p.SourceID = payment.ID
p.SourceTransactionID = payment.Transactions[0].RelatedResources[0].Sale.ID
p.CreatedAt = *payment.Transactions[0].RelatedResources[0].Sale.CreateTime
return p, nil
}
// GetByIDs sends out all items matching a set of IDs
......@@ -123,9 +136,17 @@ func (r *PaymentResource) GetByIDs(context smolder.APIContext, request *restful.
for _, id := range ids {
fmt.Println("ID", id)
paypal(context, id)
// resp.AddPayment(&payment)
payment, err := paypal(context, id)
if err != nil {
smolder.ErrorResponseHandler(request, response, smolder.NewErrorResponse(
http.StatusBadRequest,
false,
"Unknown payment ID",
"PaymentResource POST"))
return
}
resp.AddPayment(&payment)
}
resp.Send(response)
......
package payments
import (
"time"
"gitlab.techcultivation.org/sangha/sangha/db"
"github.com/muesli/smolder"
......@@ -15,8 +17,17 @@ type PaymentResponse struct {
}
type paymentInfoResponse struct {
ID int64 `json:"id"`
Payment string `json:"payment"`
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
Code string `json:"code"`
Description string `json:"description"`
Source string `json:"source"`
SourceID string `json:"source_id"`
SourcePayerID string `json:"source_payer_id"`
SourceTransactionID string `json:"source_transaction_id"`
CreatedAt time.Time `json:"created_at"`
}
// Init a new response
......@@ -47,8 +58,16 @@ func (r *PaymentResponse) EmptyResponse() interface{} {
func preparePaymentResponse(context smolder.APIContext, payment *db.Payment) paymentInfoResponse {
resp := paymentInfoResponse{
ID: payment.ID,
Payment: "",
ID: payment.ID,
UserID: payment.UserID,
Amount: payment.Amount,
Currency: payment.Currency,
Description: payment.Description,
Source: payment.Source,
SourceID: payment.SourceID,
SourcePayerID: payment.SourcePayerID,
SourceTransactionID: payment.SourceTransactionID,
CreatedAt: payment.CreatedAt,
}
return resp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment