def free(order): """ Completes a order which has a final_amount of 0 """ order_amounts = order.get_amounts(LINE_ITEM_STATUS.PURCHASE_ORDER) if order_amounts.final_amount == 0: order.confirm_sale() db.session.add(order) db.session.commit() for line_item in order.line_items: line_item.confirm() db.session.add(line_item) if line_item.discount_coupon: line_item.discount_coupon.update_used_count() db.session.add(line_item.discount_coupon) db.session.commit() send_receipt_mail.delay( order.id, subject="{item_collection_title}: Your registration is confirmed!". format(item_collection_title=order.item_collection.title), template='free_order_confirmation_mail.html.jinja2') return api_success(result={'order_id': order.id}, doc=_(u"Free order confirmed"), status_code=201) else: return api_error(message="Free order confirmation failed", status_code=402)
def payment(order): """ Accepts JSON containing `pg_paymentid`. Creates a payment object, attempts to 'capture' the payment from Razorpay, and returns a JSON containing the result of the operation. A successful capture results in a `payment_transaction` registered against the order. """ if not request.json.get('pg_paymentid'): return api_error(message="Missing payment id", status_code=400) order_amounts = order.get_amounts(LINE_ITEM_STATUS.PURCHASE_ORDER) online_payment = OnlinePayment( pg_paymentid=request.json.get('pg_paymentid'), order=order) rp_resp = razorpay.capture_payment(online_payment.pg_paymentid, order_amounts.final_amount) if rp_resp.status_code == 200: online_payment.confirm() db.session.add(online_payment) # Only INR is supported as of now transaction = PaymentTransaction(order=order, online_payment=online_payment, amount=order_amounts.final_amount, currency=CURRENCY.INR) db.session.add(transaction) order.confirm_sale() db.session.add(order) invoice_organization = order.organization.invoicer if order.organization.invoicer else order.organization invoice = Invoice(order=order, organization=invoice_organization) db.session.add(invoice) db.session.commit() for line_item in order.line_items: line_item.confirm() db.session.add(line_item) if line_item.discount_coupon: line_item.discount_coupon.update_used_count() db.session.add(line_item.discount_coupon) db.session.commit() send_receipt_mail.delay( order.id, subject= "{item_collection_title}: Thank you for your order (#{invoice_no})!" .format(item_collection_title=order.item_collection.title, invoice_no=order.invoice_no)) return api_success(result={'invoice_id': invoice.id}, doc=_(u"Payment verified"), status_code=201) else: online_payment.fail() db.session.add(online_payment) db.session.commit() raise PaymentGatewayError( "Online payment failed for order - {order} with the following details - {msg}" .format(order=order.id, msg=rp_resp.content), 424, 'Your payment failed. Please try again or contact us at {email}.'. format(email=order.organization.contact_email))
def free(order): """ Completes a order which has a final_amount of 0 """ order_amounts = order.get_amounts(LINE_ITEM_STATUS.PURCHASE_ORDER) if order_amounts.final_amount == 0: order.confirm_sale() db.session.add(order) db.session.commit() for line_item in order.line_items: line_item.confirm() db.session.add(line_item) if line_item.discount_coupon: line_item.discount_coupon.update_used_count() db.session.add(line_item.discount_coupon) db.session.commit() send_receipt_mail.delay(order.id) return make_response(jsonify(message="Free order confirmed"), 201) else: return make_response(jsonify(message='Free order confirmation failed'), 402)
def payment(order): """ Accepts JSON containing `pg_paymentid`. Creates a payment object, attempts to 'capture' the payment from Razorpay, and returns a JSON containing the result of the operation. A successful capture results in a `payment_transaction` registered against the order. """ if not request.json.get('pg_paymentid'): return make_response(jsonify(message='Missing payment id.'), 400) order_amounts = order.get_amounts(LINE_ITEM_STATUS.PURCHASE_ORDER) online_payment = OnlinePayment(pg_paymentid=request.json.get('pg_paymentid'), order=order) rp_resp = razorpay.capture_payment(online_payment.pg_paymentid, order_amounts.final_amount) if rp_resp.status_code == 200: online_payment.confirm() db.session.add(online_payment) # Only INR is supported as of now transaction = PaymentTransaction(order=order, online_payment=online_payment, amount=order_amounts.final_amount, currency=CURRENCY.INR) db.session.add(transaction) order.confirm_sale() db.session.add(order) db.session.commit() for line_item in order.line_items: line_item.confirm() db.session.add(line_item) if line_item.discount_coupon: line_item.discount_coupon.update_used_count() db.session.add(line_item.discount_coupon) db.session.commit() send_receipt_mail.delay(order.id) return make_response(jsonify(message="Payment verified"), 201) else: online_payment.fail() db.session.add(online_payment) db.session.commit() raise PaymentGatewayError("Online payment failed for order - {order} with the following details - {msg}".format(order=order.id, msg=rp_resp.content), 424, 'Your payment failed. Please try again or contact us at {email}.'.format(email=order.organization.contact_email))