0

I am using a stripe webhook to update my database. In the webhook the amount is returned in smallest currency, eg. 1000 for $10.00.

I would Like to save into db as $10.00 but cant find the way.

         # Retrieve the request's body and parse it as JSON:
event_json = json.loads(request.data)
event = stripe.Event.retrieve(event_json['id'], stripe_account=(event_json["account"]))
#event = event_json.json().get('id')
 # Do something with event_json
if event.type == 'charge.succeeded':
    date = event.created
    donor = event.data.object.customer
    amount = event.data.object.amount
    payment_date=datetime.datetime.fromtimestamp(date).strftime('%m-%d-%Y')
    # donor_ID to query db
    donor_paid = db1.Donor.query.filter_by(donor_ID=donor).first()

    # update db
    donor_paid.donation_date=payment_date
    donor_paid.donation_amount=amount

    db.session.commit()

here is the model:

class Donor(db.Model):
__tablename__="donor"
donor_ID=db.Column(db.String, primary_key=True)
rep_ID=db.Column(db.String, db.ForeignKey('rep.rep_ID'))
office_ID=db.Column(db.String)
donor_name=db.Column(db.String)
donor_email=db.Column(db.String)
donor_phone=db.Column(db.String)
donor_address=db.Column(db.String)
donation_amount=db.Column(db.String)
donation_date=db.Column(db.DateTime)
monthly_start_date=db.Column(db.DateTime)
monthly_stop_date=db.Column(db.DateTime)
donor_status=db.Column(db.Boolean)

def __init__(self,donor_ID,rep_ID,office_ID,donor_name,donor_email,donor_phone,donor_address,donation_amount,donation_date,monthly_start_date,monthly_stop_date,donor_status):
    self.donor_ID=donor_ID
    self.rep_ID=rep_ID
    self.office_ID=office_ID
    self.donor_name=donor_name
    self.donor_email=donor_email
    self.donor_phone=donor_phone
    self.donor_address=donor_address
    self.donation_amount=donation_amount
    self.donation_date=donation_date
    self.monthly_start_date=monthly_start_date
    self.monthly_stop_date=monthly_stop_date
    self.donor_status=donor_status

    def __repr__(self):
        return '<Donor %r>' % self.donor_name
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jesseCampeez
  • 97
  • 10
  • Show us the model. – Klaus D. Apr 10 '19 at 13:59
  • 1
    Just divide the amount by 100? It's returned in cents that way you can do math on it and not have to worry about floating point. – gen_Eric Apr 10 '19 at 14:52
  • @ Rocket Hazmat divide by 100 will remove the second 0 after decimal it there is no change. For example 1000 / 100 returns 10.0 – jesseCampeez Apr 10 '19 at 15:32
  • ok got it bassed on @ Rocket Hazmat and this https://stackoverflow.com/a/14540172/9878121 it is working perfect. Thank you – jesseCampeez Apr 11 '19 at 02:00
  • Your database schema uses strings to represent amounts, so you can store your amounts as strings, although I would recommend types more suited to currency representation if your database supports it – sleblanc Apr 14 '19 at 19:58

1 Answers1

1

@ Rocket Hazmat divide by 100 will remove the second 0 after decimal it there is no change. For example 1000 / 100 returns 10.0 – jesseCampeez Apr 10 at 15:32

Convert your number to a representation that will keep track of the cents. I recommend you use a Decimal:

from decimal import Decimal

value_in_cents = 1000
amount = Decimal('0.01') * value_in_cents

Then when comes the time to save it in the database, you must format it as a string that retains the decimals:

string_value = "$ %.2f" % amount

If you prefer string.format, you can do this instead:

string_value = "$ {:.2f}".format(amount)

This should output your amount in this format:

$ 10.00

sleblanc
  • 3,821
  • 1
  • 34
  • 42