Skip to content

Commit

Permalink
Implemented new functionality in payment use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Kannan112 committed Feb 13, 2024
1 parent 4d7d43f commit 8ed095f
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions pkg/usecase/paymentUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewPaymentUsecase(paymentrepo interfaces.PaymentRepo, cartRepo interfaces.C
return &PaymentUsecase{
paymentrepo: paymentrepo,
cartrepo: cartRepo,
userrepo: userRepo,
config: config,
}
}
Expand All @@ -48,24 +49,25 @@ func (c *PaymentUsecase) UpdatePaymentMethod(id int, Payment req.PaymentReq) err
return err
}

func (c *PaymentUsecase) RazorPayCheckout(ctx context.Context, userId int) (res.RazorPayResponse, error) {
var RazorPay res.RazorPayResponse
func (c *PaymentUsecase) RazorPayCheckout(ctx context.Context, userId int) (res.RazorpayOrder, error) {
var razorPay res.RazorpayOrder
cart, err := c.cartrepo.FindCart(ctx, userId)
if err != nil {
return RazorPay, errors.New("failed to find cart")
return razorPay, errors.New("failed to find cart")
}
if cart.Sub_total == 0 {
return RazorPay, errors.New("there is no products in your list")
return razorPay, errors.New("there are no products in your list")
}

// check the addresses move to user repo as FIND addres
checkbool, err := c.userrepo.FindAddress(ctx, userId)
// Check the addresses, move to user repo as FindAddress
checkBool, err := c.userrepo.FindAddress(ctx, userId)
if err != nil {
return RazorPay, errors.New("error found at find address checkout usecase")
return razorPay, errors.New("error found at finding address in checkout usecase")
}
if !checkbool {
return RazorPay, errors.New("add addresses")
if !checkBool {
return razorPay, errors.New("add addresses")
}

razorpayKey := config.GetConfig().RazorKey
razorpaySecret := config.GetConfig().RazorSec

Expand All @@ -76,24 +78,28 @@ func (c *PaymentUsecase) RazorPayCheckout(ctx context.Context, userId int) (res.
data := map[string]interface{}{
"amount": razorPayAmount,
"currency": "INR",
"receipt": "reciept_id",
"receipt": "receipt_id",
}
// create an order on razor pay
// Create an order on Razorpay
order, err := client.Order.Create(data, nil)

if err != nil {
return RazorPay, fmt.Errorf("faild to create razorpay order")
}

return res.RazorPayResponse{
Email: "[email protected]",
PhoneNumber: "9846789099",
RazorpayKey: razorpayKey,
PaymentId: uint(1),
OrderId: order["id"],
Total: uint(razorPayAmount),
AmountToPay: uint(cart.Sub_total),
}, nil
return razorPay, fmt.Errorf("failed to create Razorpay order: %v", err)
}

razorpayOrderID := order["id"]

razorPayOrder := res.RazorpayOrder{
ShopOrderID: cart.Id,
AmountToPay: uint(cart.Sub_total),
RazorpayAmount: uint(razorPayAmount),
RazorpayKey: razorpayKey,
RazorpayOrderID: razorpayOrderID,
UserID: uint(userId),
Email: "[email protected]",
Phone: "99999999",
}
return razorPayOrder, nil

}

func (c *PaymentUsecase) VerifyRazorPay(ctx context.Context, body req.RazorPayRequest) error {
Expand Down Expand Up @@ -170,20 +176,25 @@ func (c *PaymentUsecase) MakeStripeOrder(ctx context.Context, userID uint) (res.
ClientSecret: clientSecret,
PublishableKey: stripePublishKey,
}

fmt.Println("->>>>", stripeOrder)
return stripeOrder, nil

}

func (c *PaymentUsecase) VerifyStripeOrder(ctx context.Context, stripePaymentID string) error {
stripe.Key = stripePaymentID
stripe.Key = c.config.StripeKey

// get payment by payment_id
paymentIntent, err := paymentintent.Get(stripePaymentID, nil)

if err != nil {
return errors.New("failed to get payment intent from stripe")
return fmt.Errorf("failed to get payment intent from stripe")
}
if paymentIntent.Status == "succeeded" {
return nil
} else {
return errors.New("payment not approved")

// verify the payment intent
if paymentIntent.Status != stripe.PaymentIntentStatusSucceeded && paymentIntent.Status != stripe.PaymentIntentStatusRequiresCapture {
return ErrPaymentNotApproved
}
return nil
}

0 comments on commit 8ed095f

Please sign in to comment.