def entry(request): """Browser is POSTed to here to initiate a payment""" agency_id = request.POST.get('agency_id') app_name = request.POST.get('app_name') if not agency_id: return HttpResponseBadRequest('Needs agency_id') elif not request.POST.get('agency_tracking_id'): return HttpResponseBadRequest('Needs agency_tracking_id') else: url = lookup_config("transaction_url", agency_id, app_name) if not url: return HttpResponseBadRequest( 'agency_id + app_name cannot be found in settings') else: data = { 'agency_id': agency_id, 'agency_tracking_id': request.POST['agency_tracking_id'] } agency_response = requests.post(url, data=data) agency_response = agency_response_to_dict(agency_response.text) if isinstance(agency_response, dict): agency_response = clean_response(agency_response) if isinstance(agency_response, str): return HttpResponseBadRequest(agency_response) else: return generate_form(request, agency_id, app_name, agency_response)
def entry(request): """Browser is POSTed to here to initiate a payment""" agency_id = request.POST.get('agency_id') app_name = request.POST.get('app_name') if not agency_id: return HttpResponseBadRequest('Needs agency_id') elif not request.POST.get('agency_tracking_id'): return HttpResponseBadRequest('Needs agency_tracking_id') else: url = lookup_config("transaction_url", agency_id, app_name) if not url: return HttpResponseBadRequest( 'agency_id + app_name cannot be found in settings') else: data = {'agency_id': agency_id, 'agency_tracking_id': request.POST['agency_tracking_id']} agency_response = requests.post(url, data=data) agency_response = agency_response_to_dict(agency_response.text) if isinstance(agency_response, dict): agency_response = clean_response(agency_response) if isinstance(agency_response, str): return HttpResponseBadRequest(agency_response) else: return generate_form(request, agency_id, app_name, agency_response)
def test_lookup_config(self): """Fetching a key should first look in the app info and cascade up""" mock_config = {"AGENCY": {"key_a": "value_1", "key_b": "value_2", "apps": {"APPNAME": {"key_b": "value_3"}}}} with self.settings(AGENCY_CONFIG=mock_config): # Agency not present in config self.assertEqual( access_settings.lookup_config("key_b", "other", None), None) # App not present in config self.assertEqual( access_settings.lookup_config("key_b", "AGENCY", "NONAPP"), None) # Key not present in config self.assertEqual( access_settings.lookup_config("key_z", "AGENCY", "APPNAME"), None) # App overrides Agency setting self.assertEqual( access_settings.lookup_config("key_b", "AGENCY", "APPNAME"), "value_3") # Final parameter overrides everything self.assertEqual( access_settings.lookup_config("key_b", "AGENCY", "APPNAME", {"key_b": "value_10"}), "value_10") # Falls back to Agency setting self.assertEqual( access_settings.lookup_config("key_a", "AGENCY", "APPNAME"), "value_1") self.assertEqual( access_settings.lookup_config("key_b", "AGENCY", None), "value_2")
def send_status_to_agency(request, agency_id, app_name): """As part of the exit redirect, we need to tell the agency what the outcome was.""" canceled = request.POST.get('cancel') callback_data = deepcopy(request.POST) if canceled: callback_data['payment_status'] = 'Canceled' else: callback_data['payment_status'] = 'Success' callback_data = clean_callback(callback_data) callback_url = lookup_config("collection_results_url", agency_id, app_name, request.POST) agency_response = requests.post(callback_url, data=callback_data) return agency_response_to_dict(agency_response.text)
def exit_redirect(request): """Browser POST to here with their credit card info. Redirect as needed""" agency_id = request.POST['agency_id'] app_name = request.POST.get('app_name') canceled = request.POST.get('cancel') agency_response = send_status_to_agency(request, agency_id, app_name) if (not isinstance(agency_response, dict) or agency_response.get('response_message') != 'OK'): canceled = True if canceled: url_key = 'failure_return_url' else: url_key = 'success_return_url' redirect_to = lookup_config(url_key, agency_id, app_name, request.POST) return render(request, "mockpay/redirect.html", { "agency_id": agency_id, "redirect_url": redirect_to, "agency_tracking_id": request.POST.get('agency_tracking_id')})
def exit_redirect(request): """Browser POST to here with their credit card info. Redirect as needed""" agency_id = request.POST['agency_id'] app_name = request.POST.get('app_name') canceled = request.POST.get('cancel') agency_response = send_status_to_agency(request, agency_id, app_name) if (not isinstance(agency_response, dict) or agency_response.get('response_message') != 'OK'): canceled = True if canceled: url_key = 'failure_return_url' else: url_key = 'success_return_url' redirect_to = lookup_config(url_key, agency_id, app_name, request.POST) return render( request, "mockpay/redirect.html", { "agency_id": agency_id, "redirect_url": redirect_to, "agency_tracking_id": request.POST.get('agency_tracking_id') })