Stripe integration corrections
This commit is contained in:
parent
a45ea63b5d
commit
ba6f6d976d
@ -56,3 +56,8 @@ frappe.ui.form.on("Payment Request", "refresh", function(frm) {
|
|||||||
}).addClass("btn-primary");
|
}).addClass("btn-primary");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
frappe.ui.form.on("Payment Request", "is_a_subscription", function(frm) {
|
||||||
|
frm.toggle_reqd("payment_gateway_account", frm.doc.is_a_subscription);
|
||||||
|
frm.toggle_reqd("payment_plan", frm.doc.is_a_subscription);
|
||||||
|
});
|
||||||
|
|||||||
@ -7,11 +7,6 @@ def get_data():
|
|||||||
"label": _("Payments"),
|
"label": _("Payments"),
|
||||||
"icon": "fa fa-star",
|
"icon": "fa fa-star",
|
||||||
"items": [
|
"items": [
|
||||||
{
|
|
||||||
"type": "doctype",
|
|
||||||
"name": "Stripe Settings",
|
|
||||||
"description": _("Stripe payment gateway settings"),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "doctype",
|
"type": "doctype",
|
||||||
"name": "GoCardless Settings",
|
"name": "GoCardless Settings",
|
||||||
|
|||||||
@ -4,6 +4,56 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
import frappe
|
||||||
|
from frappe import _
|
||||||
|
from frappe.integrations.utils import create_request_log
|
||||||
|
import stripe
|
||||||
|
|
||||||
class PaymentPlan(Document):
|
class PaymentPlan(Document):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def create_stripe_subscription(gateway_controller, data):
|
||||||
|
stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
|
||||||
|
stripe_settings.data = frappe._dict(data)
|
||||||
|
|
||||||
|
stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False)
|
||||||
|
stripe.default_http_client = stripe.http_client.RequestsClient()
|
||||||
|
|
||||||
|
try:
|
||||||
|
stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe")
|
||||||
|
stripe_settings.payment_plan = frappe.db.get_value("Payment Request", stripe_settings.data.reference_docname, 'payment_plan')
|
||||||
|
return create_subscription_on_stripe(stripe_settings)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
frappe.log_error(frappe.get_traceback())
|
||||||
|
return{
|
||||||
|
"redirect_to": frappe.redirect_to_message(_('Server Error'), _("It seems that there is an issue with the server's stripe configuration. In case of failure, the amount will get refunded to your account.")),
|
||||||
|
"status": 401
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def create_subscription_on_stripe(stripe_settings):
|
||||||
|
items = [
|
||||||
|
{
|
||||||
|
"plan": stripe_settings.payment_plan
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
customer = stripe.Customer.create(description=stripe_settings.data.payer_name, email=stripe_settings.data.payer_email, source=stripe_settings.data.stripe_token_id)
|
||||||
|
subscription = stripe.Subscription.create(customer=customer, items=items)
|
||||||
|
|
||||||
|
if subscription.status == "active":
|
||||||
|
stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False)
|
||||||
|
stripe_settings.flags.status_changed_to = "Completed"
|
||||||
|
|
||||||
|
else:
|
||||||
|
stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
|
||||||
|
frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed')
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
|
||||||
|
frappe.log_error(frappe.get_traceback())
|
||||||
|
|
||||||
|
return stripe_settings.finalize_request()
|
||||||
|
|||||||
@ -6,6 +6,7 @@ from frappe import _
|
|||||||
from frappe.utils import cint, fmt_money
|
from frappe.utils import cint, fmt_money
|
||||||
import json
|
import json
|
||||||
from erpnext.erpnext_integrations.doctype.stripe_settings.stripe_settings import get_gateway_controller
|
from erpnext.erpnext_integrations.doctype.stripe_settings.stripe_settings import get_gateway_controller
|
||||||
|
from erpnext.erpnext_integrations.doctype.payment_plan.payment_plan import create_stripe_subscription
|
||||||
|
|
||||||
no_cache = 1
|
no_cache = 1
|
||||||
no_sitemap = 1
|
no_sitemap = 1
|
||||||
@ -59,6 +60,11 @@ def make_payment(stripe_token_id, data, reference_doctype=None, reference_docnam
|
|||||||
})
|
})
|
||||||
|
|
||||||
gateway_controller = get_gateway_controller(reference_docname)
|
gateway_controller = get_gateway_controller(reference_docname)
|
||||||
|
|
||||||
|
if frappe.db.get_value("Payment Request", reference_docname, 'is_a_subscription'):
|
||||||
|
data = create_stripe_subscription(gateway_controller, data)
|
||||||
|
else:
|
||||||
data = frappe.get_doc("Stripe Settings", gateway_controller).create_request(data)
|
data = frappe.get_doc("Stripe Settings", gateway_controller).create_request(data)
|
||||||
|
|
||||||
frappe.db.commit()
|
frappe.db.commit()
|
||||||
return data
|
return data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user