def payment_refund_request(payment_id, currency=None): payment = get_user_payment_or_abort(payment_id, valid_states=["paid"]) if currency is None: currency = payment.currency form = RefundRequestForm() form._currency = currency form._provider = payment.provider form._total_amount = payment.amount bank_validation_failed = False if form.validate_on_submit(): if (payment.provider in ("banktransfer", "gocardless") and payment.amount != form.donation_amount.data and not validate_bank_details(form, currency)): if payment.currency == "GBP": bank_type = "UK" else: bank_type = "Euro" msg = f"Your {bank_type} bank details don't appear to be valid, please check them." form.sort_code.errors.append(msg) form.account.errors.append(msg) form.iban.errors.append(msg) form.swiftbic.errors.append(msg) bank_validation_failed = True else: app.logger.info("Creating refund request for payment %s", payment.id) req = RefundRequest( payment=payment, currency=currency, donation=form.donation_amount.data, sort_code=form.sort_code.data, account=form.account.data, iban=form.iban.data, swiftbic=form.swiftbic.data, note=form.note.data, payee_name=form.payee_name.data, ) db.session.add(req) payment.state = "refund-requested" db.session.commit() flash( "Your refund request has been submitted. We will email you when it's processed." ) return redirect(url_for("users.purchases")) return render_template( "payments/refund-request.html", payment=payment, form=form, currency=currency, bank_validation_failed=bank_validation_failed, )
def stripe_refund_start(payment_id): payment = get_user_payment_or_abort( payment_id, 'stripe', valid_states=['paid'], ) form = StripeRefundForm() if form.validate_on_submit(): app.logger.info('Creating refund request for Stripe payment %s', payment.id) req = RefundRequest(payment=payment, note=form.note.data) db.session.add(req) payment.state = 'refund-requested' if not app.config.get('TICKETS_NOTICE_EMAIL'): app.logger.warning( 'No tickets notice email configured, not sending') else: msg = Message( "An EMF refund request has been received", sender=app.config.get('TICKETS_EMAIL'), recipients=[app.config.get('TICKETS_NOTICE_EMAIL')[1]]) msg.body = render_template('emails/notice-refund-request.txt', payment=payment) mail.send(msg) db.session.commit() flash("Your refund request has been sent") return redirect(url_for('users.purchases')) return render_template('payments/stripe-refund.html', payment=payment, form=form)
def stripe_refund_start(payment_id): payment = get_user_payment_or_abort(payment_id, "stripe", valid_states=["paid"]) form = StripeRefundForm() if form.validate_on_submit(): app.logger.info("Creating refund request for Stripe payment %s", payment.id) req = RefundRequest(payment=payment, note=form.note.data) db.session.add(req) payment.state = "refund-requested" if not app.config.get("TICKETS_NOTICE_EMAIL"): app.logger.warning( "No tickets notice email configured, not sending") else: msg = Message( "An EMF refund request has been received", sender=app.config.get("TICKETS_EMAIL"), recipients=[app.config.get("TICKETS_NOTICE_EMAIL")[1]], ) msg.body = render_template("emails/notice-refund-request.txt", payment=payment) mail.send(msg) db.session.commit() flash("Your refund request has been sent") return redirect(url_for("users.purchases")) return render_template("payments/stripe-refund.html", payment=payment, form=form)
def payment_refund_request(payment_id, currency='GBP'): payment = get_user_payment_or_abort( payment_id, valid_states=['paid'], ) no_stripe = 'no_stripe' in request.args if payment.provider == 'stripe' and not no_stripe: return redirect(url_for('.stripe_refund_start', payment_id=payment.id)) form = RefundRequestForm() form._currency = currency bank_validation_failed = False if form.validate_on_submit(): app.logger.info("Validating bank details") if currency == 'GBP': params = { 'country_code': 'GB', 'branch_code': form.bank.data, 'account_number': form.account.data } elif currency == 'EUR': params = {'iban': form.iban.data} try: result = gocardless_client.bank_details_lookups.create(params) app.logger.info("Bank identified as %r", result.attributes.get('bank_name')) except gocardless_pro.errors.ValidationFailedError as e: app.logger.warn("Error validating bank details: %s", e) if not form.really_submit.data: msg = "This doesn't look right. Please check and click below if you're sure." bank_validation_failed = True form.bank.errors.append(msg) form.account.errors.append(msg) form.iban.errors.append(msg) if not bank_validation_failed: app.logger.info('Creating refund request for payment %s', payment.id) if currency == 'GBP': account = form.account.data elif currency == 'EUR': account = form.iban.data req = RefundRequest(payment, currency, form.bank.data, account) db.session.add(req) payment.state = 'refund-requested' if not app.config.get('TICKETS_NOTICE_EMAIL'): app.logger.warning( 'No tickets notice email configured, not sending') else: msg = Message( "An EMF refund request has been received", sender=app.config.get('TICKETS_EMAIL'), recipients=[app.config.get('TICKETS_NOTICE_EMAIL')[1]]) msg.body = render_template('emails/notice-refund-request.txt', payment=payment) mail.send(msg) db.session.commit() flash("Your refund request has been sent") return redirect(url_for('users.purchases')) return render_template('payments/refund-request.html', payment=payment, form=form, currency=currency, no_stripe=no_stripe, bank_validation_failed=bank_validation_failed)
def payment_refund_request(payment_id, currency="GBP"): payment = get_user_payment_or_abort(payment_id, valid_states=["paid"]) no_stripe = "no_stripe" in request.args if payment.provider == "stripe" and not no_stripe: return redirect(url_for(".stripe_refund_start", payment_id=payment.id)) form = RefundRequestForm() form._currency = currency bank_validation_failed = False if form.validate_on_submit(): app.logger.info("Validating bank details") if currency == "GBP": params = { "country_code": "GB", "branch_code": form.bank.data, "account_number": form.account.data, } elif currency == "EUR": params = {"iban": form.iban.data} try: result = gocardless_client.bank_details_lookups.create(params) app.logger.info("Bank identified as %r", result.attributes.get("bank_name")) except gocardless_pro.errors.ValidationFailedError as e: app.logger.warn("Error validating bank details: %s", e) if not form.really_submit.data: msg = "This doesn't look right. Please check and click below if you're sure." bank_validation_failed = True form.bank.errors.append(msg) form.account.errors.append(msg) form.iban.errors.append(msg) if not bank_validation_failed: app.logger.info("Creating refund request for payment %s", payment.id) if currency == "GBP": account = form.account.data elif currency == "EUR": account = form.iban.data req = RefundRequest( payment=payment, currency=currency, bank=form.bank.data, account=account, note=form.note.data, ) db.session.add(req) payment.state = "refund-requested" if not app.config.get("TICKETS_NOTICE_EMAIL"): app.logger.warning( "No tickets notice email configured, not sending") else: msg = Message( "An EMF refund request has been received", sender=app.config.get("TICKETS_EMAIL"), recipients=[app.config.get("TICKETS_NOTICE_EMAIL")[1]], ) msg.body = render_template("emails/notice-refund-request.txt", payment=payment) mail.send(msg) db.session.commit() flash("Your refund request has been sent") return redirect(url_for("users.purchases")) return render_template( "payments/refund-request.html", payment=payment, form=form, currency=currency, no_stripe=no_stripe, bank_validation_failed=bank_validation_failed, )