def donate(): """Donation page for Stripe. Stripe API reference for Python can be found at https://stripe.com/docs/api/python. """ form = DonationForm() if not form.validate(): return redirect(url_for('donations.error')) if current_app.config['PAYMENT_PRODUCTION']: stripe.api_key = current_app.config['STRIPE_KEYS']['SECRET'] else: stripe.api_key = current_app.config['STRIPE_TEST_KEYS']['SECRET'] # Get the credit card details submitted by the form token = request.form['stripeToken'] # Create the charge on Stripe's servers - this will charge the donor's card try: charge = stripe.Charge.create( amount=int(form.amount.data * 100), # amount in cents currency='USD', card=token, description='Donation to MetaBrainz Foundation', metadata={ 'email': request.form['stripeEmail'], 'editor': form.editor.data, 'anonymous': form.anonymous.data, 'can_contact': form.can_contact.data, }, ) except stripe.CardError: # The card has been declined return redirect(url_for('donations.error')) Donation.log_stripe_charge(charge) return redirect(url_for('donations.complete'))
def test_log_stripe_charge(self): # Function should execute without any exceptions charge = convert_to_stripe_object( { "id": u"ch_15AjX1F21qH57QtHT6avvqrM", "object": u"charge", "created": 1418829367, "livemode": False, "paid": True, "status": u"succeeded", "amount": 99999900, "currency": u"usd", "refunded": False, "source": { "id": u"card_15AjWxF21qH57QtHHVNgaHOP", "object": u"card", "last4": u"4242", "brand": u"Visa", "funding": u"credit", "exp_month": 11, "exp_year": 2016, "country": u"US", "name": u"Uh Oh", "address_line1": u"test 12", "address_line2": None, "address_city": u"Schenectady", "address_state": u"NY", "address_zip": u"12345", "address_country": u"United States", "cvc_check": u"pass", "address_line1_check": u"pass", "address_zip_check": u"pass", "dynamic_last4": None, "metadata": {}, "customer": None }, "captured": True, "balance_transaction": u"txn_159qthF21qH57QtHBksXX3tN", "failure_message": None, "failure_code": None, "amount_refunded": 0, "customer": None, "invoice": None, "description": u"Donation to MetaBrainz Foundation", "dispute": None, "metadata": { "anonymous": u"True", # passed as a string "can_contact": u"False", # passed as a string "email": u"*****@*****.**", "editor": u"null" }, "statement_descriptor": None, "fraud_details": {}, "receipt_email": None, "receipt_number": None, "shipping": None, "application_fee": None, "refunds": { "object": "list", "total_count": 0, "has_more": False, "url": "/v1/charges/ch_15AjX1F21qH57QtHT6avvqrM/refunds", "data": [] } }, api_key=None, account=None ) Donation.log_stripe_charge(charge)
def test_log_stripe_charge(self): # Function should execute without any exceptions charge = convert_to_stripe_object( { "id": u"ch_15AjX1F21qH57QtHT6avvqrM", "object": u"charge", "created": 1418829367, "livemode": False, "paid": True, "status": u"succeeded", "amount": 99999900, "currency": u"usd", "refunded": False, "source": { "id": u"card_15AjWxF21qH57QtHHVNgaHOP", "object": u"card", "last4": u"4242", "brand": u"Visa", "funding": u"credit", "exp_month": 11, "exp_year": 2016, "country": u"US", "name": u"Uh Oh", "address_line1": u"test 12", "address_line2": None, "address_city": u"Schenectady", "address_state": u"NY", "address_zip": u"12345", "address_country": u"United States", "cvc_check": u"pass", "address_line1_check": u"pass", "address_zip_check": u"pass", "dynamic_last4": None, "metadata": {}, "customer": None }, "captured": True, "balance_transaction": u"txn_159qthF21qH57QtHBksXX3tN", "failure_message": None, "failure_code": None, "amount_refunded": 0, "customer": None, "invoice": None, "description": u"Donation to MetaBrainz Foundation", "dispute": None, "metadata": { "anonymous": u"True", # passed as a string "can_contact": u"False", # passed as a string "email": u"*****@*****.**", "editor": u"null" }, "statement_descriptor": None, "fraud_details": {}, "receipt_email": None, "receipt_number": None, "shipping": None, "application_fee": None, "refunds": { "object": "list", "total_count": 0, "has_more": False, "url": "/v1/charges/ch_15AjX1F21qH57QtHT6avvqrM/refunds", "data": [] } }, api_key=None, account=None) Donation.log_stripe_charge(charge)