Beispiel #1
0
    def get_auth_token(self):
        if time_diff_in_seconds(self.e_invoice_settings.token_expiry,
                                now_datetime()) < 150.0:
            self.fetch_auth_token()

        return self.e_invoice_settings.auth_token
def get_status(data):
	if isinstance(data, str):
		data = json.loads(data)
		data = frappe._dict(data)

	# sanity gate keeper. Don't process anything without reference doctypes
	if not data.reference_doctype or not data.reference_docname:
		return {
			"status": "Failed",
			"description": "Invalid request. Submitted data contains no order reference. This seems to be an internal error. Please contact support."
		}

	# fetch previous requests info
	request_info = get_order_request_info(data.reference_doctype, data.reference_docname)

	# and document references
	pr = frappe.get_doc(data.reference_doctype, data.reference_docname)
	reference_doc = frappe.get_doc(pr.reference_doctype, pr.reference_name)

	if request_info:
		status = request_info.get("status")
		integration_request = frappe.get_doc("Integration Request", request_info.name)

		if status == "Queued":
			# we don't want to wait forever for a failed request that never updated
			# the integration request status
			time_elapsed_since_update = time_diff_in_seconds(now(), request_info.modified)
			if time_elapsed_since_update < 60:
				return {
					"status": "Queued",
					"wait": 5000
				}
			else:
				# assume dropped request and flag as failed. Then restart call process with new
				# integration request.
				integration_request.error = "[Timeout] Integration Request lasted longer than 60 seconds. Assuming dropped request."
				integration_request.update_status(data, "Failed")
				integration_request = None
				early_exit = False
				return {
					"status": "Failed",
					"type": "ProcessorTimeout",
					"description": "Request to credit card processor Timed out."
				}
		elif status == "Completed":
			return {
				"status": "Completed"
			}

		if status == "Failed":
			if "declined" in (integration_request.error or "").lower():
				return {
					"status": "Failed",
					"type": "Validation",
					"description": integration_request.error
				}

			# don't early exit on failed. Means we are retrying a new card
			return {
				"status": "Failed",
				"type": "Unhandled",
				"description": integration_request.error
			}
	
	return {
		"status": "NotFound"
	}