def response_payment(responseId): from ..main import app response = Response.objects.get({"_id": ObjectId(responseId)}) app.check_permissions(response.form, "Responses_Edit") amount = app.current_request.json_body["amount"] currency = app.current_request.json_body["currency"] date = app.current_request.json_body.get("date", None) if date and "$date" in date: date = dateutil.parser.parse(date["$date"]) id = app.current_request.json_body["id"] method = app.current_request.json_body["method"] send_email = app.current_request.json_body.get("sendEmail", True) paid = mark_successful_payment(response.form, response, { "type": "manual", "method": method, "id": id }, method, amount, currency, id, date=date, send_email=send_email) response.save() return { "res": { "success": True, "paid": paid, "response": serialize_model(response) } }
def test_mark_successful_payment_2(self): responseId, _ = self.submit_form(self.formId, ONE_FORMDATA) response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment(form=Form.objects.get( {"_id": ObjectId(self.formId)}), response=response, full_value={ "a2": "b2", "c2": "d2" }, method_name="unittest_ipn2", amount=0.6, currency="USD", id="payment2") response.save() self.assertEqual(paid, True) response = self.view_response(responseId) response['payment_trail'][0].pop("date") response['payment_trail'][0].pop("date_created") response['payment_trail'][0].pop("date_modified") self.assertEqual(response['payment_trail'], [{ 'value': { 'a2': 'b2', 'c2': 'd2' }, 'method': 'unittest_ipn2', 'status': 'SUCCESS', 'id': 'payment2', '_cls': 'chalicelib.models.PaymentTrailItem' }]) self.assertEqual(response['paid'], True) self.assertEqual(response['amount_paid'], "0.6")
def test_mark_successful_payment_no_email(self): responseId, _ = self.submit_form(self.formId, ONE_FORMDATA) response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment(form=Form.objects.get( {"_id": ObjectId(self.formId)}), response=response, full_value={ "a": "b", "c": "d" }, method_name="unittest_ipn", amount=0.5, currency="USD", id="payment1", send_email=False) response.save() self.assertEqual(paid, True) response = self.view_response(responseId) response['payment_trail'][0].pop("date") response['payment_trail'][0].pop("date_created") response['payment_trail'][0].pop("date_modified") self.assertEqual(response['payment_trail'], [{ 'value': { 'a': 'b', 'c': 'd' }, 'method': 'unittest_ipn', 'status': 'SUCCESS', 'id': 'payment1', '_cls': 'chalicelib.models.PaymentTrailItem' }]) self.assertEqual(response['paid'], True) self.assertEqual(response['amount_paid'], "0.5") self.assertTrue('email_trail' not in response)
def test_mark_successful_payment_not_full(self): responseId, _ = self.submit_form(self.formId, ONE_FORMDATA) paid = mark_successful_payment(form=Form.objects.get( {"_id": ObjectId(self.formId)}), responseId=responseId, full_value={ "a": "b", "c": "d" }, method_name="unittest_ipn", amount=0.4, currency="USD", id="payment2")
def response_add_payment(responseId): from ..main import app response = Response.objects.get({"_id": ObjectId(responseId)}) app.check_permissions(response.form, ["Responses_Edit", "Responses_AddPayment"]) amount = app.current_request.json_body["amount"] currency = app.current_request.json_body["currency"] date = app.current_request.json_body.get("date", None) if date and "$date" in date: date = dateutil.parser.parse(date["$date"]) id = app.current_request.json_body["id"] method = app.current_request.json_body["method"] send_email = app.current_request.json_body.get("sendEmail", True) email_template_id = _parse_email_parameters(app.current_request.json_body) notes = app.current_request.json_body.get("notes", None) if notes == "": notes = None value = {"type": "manual", "method": method, "id": id} if notes is not None: value = dict(value, notes=notes) paid = mark_successful_payment( response.form, response, value, method, amount, currency, id, date=date, send_email=send_email, notes=notes, email_template_id=email_template_id, ) response.save() return { "res": { "success": True, "paid": paid, "response": serialize_model(response) } }
def test_mark_successful_payment_dont_send_email(self): responseId, _ = self.submit_form(self.formId, ONE_FORMDATA) response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment( form=Form.objects.get({"_id": ObjectId(self.formId)}), response=response, full_value={ "a": "b", "c": "d" }, method_name="unittest_ipn", amount=0.5, currency="USD", id="payment1", send_email=False, ) response.save() self.assertEqual(paid, True) response = self.view_response(responseId) response["payment_trail"][0].pop("date") response["payment_trail"][0].pop("date_created") response["payment_trail"][0].pop("date_modified") self.assertEqual( response["payment_trail"], [{ "value": { "a": "b", "c": "d" }, "method": "unittest_ipn", "status": "SUCCESS", "id": "payment1", "_cls": "chalicelib.models.PaymentTrailItem", }], ) self.assertEqual(response["paid"], True) self.assertEqual(response["amount_paid"], "0.5") self.assertTrue("email_trail" not in response)
def test_submit_form_update_unpaid_to_paid(self): """Submit form.""" responseId, submit_res = self.submit_form(self.formId, ONE_FORMDATA) self.assertEqual(submit_res.pop("value"), ONE_FORMDATA) self.assertEqual(submit_res, ONE_SUBMITRES, submit_res) self.assertIn("paymentMethods", submit_res) """View response.""" response = self.view_response(responseId) self.assertEqual(response["value"], ONE_FORMDATA) self.assertEqual(response["paid"], False) self.assertTrue(response.get("user", None) == None) # Pay response. response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment( form=Form.objects.get({"_id": ObjectId(self.formId)}), response=response, full_value={ "a2": "b2", "c2": "d2" }, method_name="unittest_ipn", amount=0.5, currency="USD", id="payment1", ) response.save() response = self.view_response(responseId) self.assertEqual(response["paid"], True) self.assertEqual(response["amount_paid"], "0.5") self.assertEqual(response["paymentInfo"]["total"], 0.5) self.assertEqual(response["amount_owed_cents"], 50) new_data = dict(ONE_FORMDATA, children=[{}]) response = self.lg.handle_request( method="POST", path=f"/forms/{self.formId}", headers={ "authorization": "auth", "Content-Type": "application/json" }, body=json.dumps({ "data": new_data, "responseId": responseId }), ) self.assertEqual(response["statusCode"], 200, response) response = self.view_response(responseId) self.assertEqual(response["value"], new_data) self.assertEqual(response["paid"], False) self.assertEqual(response["amount_paid"], "0.5") self.assertEqual(response["paymentInfo"]["total"], 25.5) self.assertEqual(response["amount_owed_cents"], 2550) # Pay response. response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment( form=Form.objects.get({"_id": ObjectId(self.formId)}), response=response, full_value={ "a2": "b2", "c2": "d2" }, method_name="unittest_ipn", amount=25, currency="USD", id="payment2", ) response.save() response = self.view_response(responseId) self.assertEqual(response["value"], new_data) self.assertEqual(response["paid"], True) self.assertEqual(response["amount_paid"], "25.5") self.assertEqual(response["paymentInfo"]["total"], 25.5)
def test_mark_successful_payment_custom_email_template(self): formOptions = { "confirmationEmailTemplates": [{ "id": "template1", "confirmationEmailInfo": { "toField": "email", "subject": "subject 123", "template": { "html": "test123" }, }, }], "responseCanViewByLink": True, "responseCanEditByLink": True, } self.edit_form( self.formId, { "schema": { "a": "B" }, "uiSchema": { "a": "B" }, "formOptions": formOptions }, ) responseId, _ = self.submit_form(self.formId, ONE_FORMDATA) response = Response.objects.get({"_id": ObjectId(responseId)}) paid = mark_successful_payment( form=Form.objects.get({"_id": ObjectId(self.formId)}), response=response, full_value={ "a": "b", "c": "d" }, method_name="unittest_ipn", amount=0.5, currency="USD", id="payment1", email_template_id="template1", ) response.save() self.assertEqual(paid, True) response = self.view_response(responseId) response["payment_trail"][0].pop("date") response["payment_trail"][0].pop("date_created") response["payment_trail"][0].pop("date_modified") self.assertEqual( response["payment_trail"], [{ "value": { "a": "b", "c": "d" }, "method": "unittest_ipn", "status": "SUCCESS", "id": "payment1", "_cls": "chalicelib.models.PaymentTrailItem", }], ) self.assertEqual(response["paid"], True) self.assertEqual(response["amount_paid"], "0.5") self.assertEqual(len(response["email_trail"]), 1)