<s> import decimal <EOL> from django . conf import settings <EOL> import stripe <EOL> from . . import hooks <EOL> from . . import models <EOL> from . . import utils <EOL> def calculate_refund_amount ( charge , amount = None ) : <EOL> """<STR_LIT>""" <EOL> eligible_to_refund = charge . amount - ( charge . amount_refunded or <NUM_LIT:0> ) <EOL> if amount : <EOL> return min ( eligible_to_refund , amount ) <EOL> return eligible_to_refund <EOL> def capture ( charge , amount = None ) : <EOL> """<STR_LIT>""" <EOL> stripe_charge = charge . stripe_charge . capture ( <EOL> amount = utils . convert_amount_for_api ( <EOL> amount if amount else charge . amount , <EOL> charge . currency <EOL> ) <EOL> ) <EOL> sync_charge_from_stripe_data ( stripe_charge ) <EOL> def create ( amount , customer , source = None , currency = "<STR_LIT>" , description = None , send_receipt = settings . PINAX_STRIPE_SEND_EMAIL_RECEIPTS , capture = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( amount , decimal . Decimal ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> stripe_charge = stripe . Charge . create ( <EOL> amount = utils . convert_amount_for_api ( amount , currency ) , <EOL> currency = currency , <EOL> source = source , <EOL> customer = customer , <EOL> description = description , <EOL> capture = capture , <EOL> ) <EOL> charge = sync_charge_from_stripe_data ( stripe_charge ) <EOL> if send_receipt : <EOL> hooks . hookset . send_receipt ( charge ) <EOL> return charge <EOL> def sync_charges_for_customer ( customer ) : <EOL> """<STR_LIT>""" <EOL> for charge in customer . stripe_customer . charges ( ) . data : <EOL> sync_charge_from_stripe_data ( charge ) <EOL> def sync_charge_from_stripe_data ( data ) : <EOL> """<STR_LIT>""" <EOL> customer = models . Customer . objects . get ( stripe_id = data [ "<STR_LIT>" ] ) <EOL> obj , _ = models . Charge . objects . get_or_create ( <EOL> customer = customer , <EOL> stripe_id = data [ "<STR_LIT:id>" ] <EOL> ) <EOL> obj . source = data [ "<STR_LIT:source>" ] [ "<STR_LIT:id>" ] <EOL> obj . currency = data [ "<STR_LIT>" ] <EOL> obj . invoice = next ( iter ( models . Invoice . objects . filter ( stripe_id = data [ "<STR_LIT>" ] ) ) , None ) <EOL> obj . amount = utils . convert_amount_for_db ( data [ "<STR_LIT>" ] , obj . currency ) <EOL> obj . paid = data [ "<STR_LIT>" ] <EOL> obj . refunded = data [ "<STR_LIT>" ] <EOL> obj . captured = data [ "<STR_LIT>" ] <EOL> obj . disputed = data [ "<STR_LIT>" ] is not None <EOL> obj . charge_created = utils . convert_tstamp ( data , "<STR_LIT>" ) <EOL> if data . get ( "<STR_LIT:description>" ) : <EOL> obj . description = data [ "<STR_LIT:description>" ] <EOL> if data . get ( "<STR_LIT>" ) : <EOL> obj . amount_refunded = utils . convert_amount_for_db ( data [ "<STR_LIT>" ] , obj . currency ) <EOL> if data [ "<STR_LIT>" ] : <EOL> obj . amount_refunded = obj . amount <EOL> obj . save ( ) <EOL> return obj </s>
