user9252255
user9252255

Reputation:

Django: Database entry twice

I use Stripe in my Django project and first I charge the customer. After I have the var charge, I enter this data in my Charge model, as well as I save data in my Fee model. Now I realised once I save the foreign key: charge=new_charge_obj, the whole POST request is running twice. Is my approach wrong to assigning the foreign key (Charge) for my Fee model

if request.method == 'POST':
            stripe.api_key = "XYZ" # TODO Marc (it said keep api in call?)
            token = request.POST.get('stripeToken')



      # atomic ?
        #try:
        charge = stripe.Charge.create(
            amount=900,
            application_fee=100,
            currency='EUR', # TODO Marc
            source=token,
            stripe_account="XYZ",  # TODO Marc: Replace with organizer stripe account
        )

        new_charge_obj = Charge.objects.create(
            amount=charge.amount,
            # amount_refunded=charge.amount_refunded,
            charge_id=charge.id,
            livemode=charge.livemode,
            paid=charge.paid,
            refunded=charge.refunded,
            currency=charge.currency,
            failure_code=charge.failure_code,
            failure_message=charge.failure_message,
            fraud_details=charge.fraud_details,
            outcome=charge.outcome,
            status=charge.status,
            application_fee=charge.application_fee,
            captured=charge.captured,
            created=charge.created,
        )

        application_fee = stripe.ApplicationFee.retrieve(charge.application_fee)

        Fee.objects.create(
            fee_id=application_fee.id,
            livemode=application_fee.livemode,
            currency=application_fee.currency,
            amount=application_fee.amount,
            charge=new_charge_obj,
        )

Upvotes: 0

Views: 62

Answers (1)

Umair Mohammad
Umair Mohammad

Reputation: 4635

You can use get_or_create() to prevent creation of duplicates

charge = stripe.Charge.get_or_create(
    amount = 900,
    application_fee = 100,
    currency = 'EUR', #TODO Marc source = token,
    stripe_account = "XYZ", #TODO Marc: Replace with organizer stripe account
)

new_charge_obj = Charge.objects.get_or_create(
    amount = charge.amount, #amount_refunded = charge.amount_refunded,
    charge_id = charge.id,
    livemode = charge.livemode,
    paid = charge.paid,
    refunded = charge.refunded,
    currency = charge.currency,
    failure_code = charge.failure_code,
    failure_message = charge.failure_message,
    fraud_details = charge.fraud_details,
    outcome = charge.outcome,
    status = charge.status,
    application_fee = charge.application_fee,
    captured = charge.captured,
    created = charge.created,
)

application_fee = stripe.ApplicationFee.retrieve(charge.application_fee)

Fee.objects.get_or_create(
    fee_id = application_fee.id,
    livemode = application_fee.livemode,
    currency = application_fee.currency,
    amount = application_fee.amount,
    charge = new_charge_obj,
)

Upvotes: 1

Related Questions