sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Having trouble manipulating stripe api data 'price' looking for an ID for the price object #232

Closed jawknee59 closed 1 year ago

jawknee59 commented 1 year ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

DR

What's the problem you're trying to solve?

having trouble understanding how to manipulate the price object in the stripe api

Post any code you think might be relevant (one fenced block per file)

@login_required
def checkout(request):
  cart = Cart.objects.filter(user=request.user).first()
  stripe.api_key = settings.STRIPE_SECRET_KEY
  total_price = cart.get_total_price()
  total_items = cart.get_total_items()
  if request.method == 'POST':
    checkout_session = stripe.checkout.Session.create(
      payment_method_types = ['card'],
      line_items=[
        {
          'price': total_price,
          'quantity': total_items,
        },
      ],
      mode='payment',
      success_url = settings.REDIRECT_DOMAIN + '/payment_successful?session_id={CHECKOUT_SESSION_ID}',
      cancel_url = settings.REDIRECT_DOMAIN + '/payment_cancelled',
    )
    return render(checkout_session.url, code=303)
  return render(request, 'user_payment/checkout.html', {'cart': cart, 'total_price': total_price, 'total_items': total_items})

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

Request req_nVxfN5uu0hdnRO: The price parameter should be the ID of a price object, rather than the literal numerical price. Please see https://stripe.com/docs/billing/prices-guide#create-prices for more information about how to set up price objects.

What is your best guess as to the source of the problem?

It is looking for a price ID object, uncertain how to manipulate the price object data in stripe

What things have you already tried to solve the problem?

I created a product in stripe dashboard and use that price object to see if i can access the stripe payment dashboard, which it was able to work, not really sure how to manipulate that data with the data i created in django

Paste a link to your repository here https://github.com/jawknee59/project4-ecommerce

jawknee59 commented 1 year ago

I have a function that gets the total price, but not a field for it

jawknee59 commented 1 year ago

Screenshot 2023-03-13 at 12 48 46 PM

jawknee59 commented 1 year ago
# Cart Model
class Cart(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.user

    def get_total_price(self):
        total_price = 0
        for item in self.cartitem_set.all():
            total_price += item.total_price()
        return total_price

    def get_total_items(self):
        total_qty = 0
        for item in self.cartitem_set.all():
            total_qty += item.quantity
        return total_qty
jawknee59 commented 1 year ago

Was able to resolve the issue by adding a stripe_price_id to my item's model, adding products through the stripe's dashboard to generate a stripe priceId and manually putting the priceId on the admin side for my item's model, and mapping through the user's item in the cart, and render onto stripe's pre-built checkout page

# Item Model
class Item(models.Model):
    title = models.TextField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    description = models.TextField(max_length=1000)
    stripe_price_id = models.CharField(max_length=50)
    category = models.CharField(
        max_length=2,
        choices = CATEGORIES,
        default = CATEGORIES[0][0]
    )
    image_url = models.CharField(max_length=200)

    def __str__(self):
        return self.title
@login_required(login_url='login')
def product_page(request):
  stripe.api_key = settings.STRIPE_SECRET_KEY
  cart = Cart.objects.filter(user=request.user).first()
  if request.method == 'POST':
      line_items = []
      for cart_item in cart.cartitem_set.all():
          line_items.append({
              'price': cart_item.item.stripe_price_id,
              'quantity': cart_item.quantity,
          })
      checkout_session = stripe.checkout.Session.create(
          payment_method_types = ['card'],
          line_items = line_items,
          mode = 'payment',
          customer_creation = 'always',
          success_url = settings.REDIRECT_DOMAIN + '/payment_successful?session_id={CHECKOUT_SESSION_ID}',
          cancel_url = settings.REDIRECT_DOMAIN + '/payment_cancelled',
      )
      return redirect(checkout_session.url, code=303)
  return render(request, 'user_payment/product_page.html')