def test_ipn_refund(self): responses.add(responses.POST, 'https://www.sandbox.paypal.com/cgi-bin/webscr', body='VERIFIED', status=200) responseId = str(ObjectId()) response = Response( id=ObjectId(responseId), form=self.formId, date_modified=datetime.datetime.now(), date_created=datetime.datetime.now(), value={"a":"b", "email": "*****@*****.**"}, paymentInfo={"total": .5, "currency": "USD","items": [{"name": "a", "description": "b", "amount": .25, "quantity": 1}, {"name": "a2", "description": "b2", "amount": .25, "quantity": 1}]}, payment_status_detail=[PaymentStatusDetailItem(amount="1080.00", method="paypal_ipn", currency="USD", id="id1", date=datetime.datetime.now())], paid=True, amount_paid="1080.0" ).save() ipn_value = f"mc_gross=-1080.00&protection_eligibility=Eligible&item_number1=Double Room&payer_id=A4CSL993V3BDG&address_street=1 Main St&payment_date=07:43:04 Jun 23, 2019 PDT&payment_status=Refunded&charset=windows-1252&address_zip=95131&mc_shipping=0.00&mc_handling=0.00&first_name=test&mc_fee=-31.32&address_country_code=US&address_name=test buyer¬ify_version=3.9&reason_code=refund&custom={responseId}&[email protected]&address_country=United States&mc_handling1=0.00&address_city=San Jose&verify_sign=Ajpb2sm-lsDSWA5XwZfLh0PpsG6IAYl-yqrJx2GLHimSQ3aPkkfr3i2Y&[email protected]&mc_shipping1=0.00&tax1=0.00&parent_txn_id=23X19291EC9014330&txn_id=69B89632XK721821J&payment_type=instant&last_name=buyer&address_state=CA&item_name1=Double Room ($1200/person)&[email protected]&payment_fee=-31.32&shipping_discount=0.00&quantity1=1&receiver_id=T4A6C58SP7PP2&insurance_amount=0.00&discount=120.00&mc_gross_1=1200.00&mc_currency=USD&residence_country=US&test_ipn=1&shipping_method=Default&transaction_subject=&payment_gross=-1080.00&ipn_track_id=539cae4d23348" response = self.send_ipn(responseId, ipn_value) response = self.view_response(responseId) self.assertTrue("payment_status_detail" in response) self.assertEqual(len(response["payment_trail"]), 1) detail_history_one = response["payment_trail"][0] detail_history_one.pop("date") # TODO: why do these two not exist? detail_history_one.pop("date_created") detail_history_one.pop("date_modified") self.assertEqual(detail_history_one, {'value': {'mc_gross': '-1080.00', 'protection_eligibility': 'Eligible', 'item_number1': 'Double Room', 'payer_id': 'A4CSL993V3BDG', 'address_street': '1 Main St', 'payment_date': '07:43:04 Jun 23, 2019 PDT', 'payment_status': 'Refunded', 'charset': 'windows-1252', 'address_zip': '95131', 'mc_shipping': '0.00', 'mc_handling': '0.00', 'first_name': 'test', 'mc_fee': '-31.32', 'address_country_code': 'US', 'address_name': 'test buyer', 'notify_version': '3.9', 'reason_code': 'refund', 'custom': responseId, 'business': '*****@*****.**', 'address_country': 'United States', 'mc_handling1': '0.00', 'address_city': 'San Jose', 'verify_sign': 'Ajpb2sm-lsDSWA5XwZfLh0PpsG6IAYl-yqrJx2GLHimSQ3aPkkfr3i2Y', 'payer_email': '*****@*****.**', 'mc_shipping1': '0.00', 'tax1': '0.00', 'parent_txn_id': '23X19291EC9014330', 'txn_id': '69B89632XK721821J', 'payment_type': 'instant', 'last_name': 'buyer', 'address_state': 'CA', 'item_name1': 'Double Room ($1200/person)', 'receiver_email': '*****@*****.**', 'payment_fee': '-31.32', 'shipping_discount': '0.00', 'quantity1': '1', 'receiver_id': 'T4A6C58SP7PP2', 'insurance_amount': '0.00', 'discount': '120.00', 'mc_gross_1': '1200.00', 'mc_currency': 'USD', 'residence_country': 'US', 'test_ipn': '1', 'shipping_method': 'Default', 'payment_gross': '-1080.00', 'ipn_track_id': '539cae4d23348'}, 'method': 'paypal_ipn', 'status': 'SUCCESS', 'id': '69B89632XK721821J', '_cls': 'chalicelib.models.PaymentTrailItem'}) self.assertTrue("email_trail" in response) self.assertEqual(len(response["email_trail"]), 1) self.assertEqual(response["paid"], False) self.assertEqual(response["amount_paid"], "0.0")
def mark_successful_payment(form, response, full_value, method_name, amount, currency, id, date=None, send_email=True): if not date: date = datetime.datetime.now() response.payment_trail.append(PaymentTrailItem(value=full_value, status="SUCCESS", date=date, date_created=date, date_modified=date, method=method_name, id=id)) response.payment_status_detail.append(PaymentStatusDetailItem(amount=str(amount), currency=currency, date=date, date_created=date, date_modified=date, method=method_name, id=id)) response.amount_paid = str(float(response.amount_paid or 0) + float(amount)) update_response_paid_status(response) if form.formOptions.confirmationEmailInfo and send_email: email_sent = send_confirmation_email(response, form.formOptions.confirmationEmailInfo) return response.paid
def mark_successful_payment( form, response, full_value, method_name, amount, currency, id, date=None, send_email=True, notes=None, email_template_id=None, ): if not date: date = datetime.datetime.now() payment_trail_kwargs = dict( value=full_value, status="SUCCESS", date=date, date_created=date, date_modified=date, method=method_name, id=id, ) payment_status_detail_kwargs = dict( amount=str(amount), currency=currency, date=date, date_created=date, date_modified=date, method=method_name, id=id, ) if notes is not None: payment_trail_kwargs = dict(payment_trail_kwargs, notes=notes) payment_status_detail_kwargs = dict(payment_status_detail_kwargs, notes=notes) response.payment_trail.append(PaymentTrailItem(**payment_trail_kwargs)) response.payment_status_detail.append( PaymentStatusDetailItem(**payment_status_detail_kwargs)) amount_paid = float(response.amount_paid or 0) + float(amount) response.amount_paid_cents = int(100 * amount_paid) response.amount_paid = str(amount_paid) update_response_paid_status(response) if send_email: send_email_receipt(response, form.formOptions, email_template_id) return response.paid
if isinstance(o, Decimal): return float(o) return super(DecimalEncoder, self).default(o) for response in query['Items']: response = json.loads(json.dumps(response, cls=DecimalEncoder)) res = Response( date_created=dateutil.parser.parse(response["date_created"]), date_modified=dateutil.parser.parse(response["date_last_modified"]), form=ObjectId(formIdNew), paymentInfo=response["paymentInfo"], value=response["value"], paid=response["PAID"], payment_trail=[ PaymentTrailItem(date=dateutil.parser.parse(i["date"]), value=i["value"], status=i["status"], id=i["value"]["txn_id"], method="paypal_ipn") for i in response.get("IPN_HISTORY", []) ] or [], payment_status_detail=[ PaymentStatusDetailItem(date=dateutil.parser.parse(i["date"]), amount=i["amount"], currency=i["currency"], method="paypal_ipn" if i["method"] == "paypal" else i["method"]) for i in response.get("PAYMENT_HISTORY", []) ] or []) res.save()
def test_ipn_refund(self): responses.add( responses.POST, "https://www.sandbox.paypal.com/cgi-bin/webscr", body="VERIFIED", status=200, ) responseId = str(ObjectId()) response = Response( id=ObjectId(responseId), form=self.formId, date_modified=datetime.datetime.now(), date_created=datetime.datetime.now(), value={"a": "b", "email": "*****@*****.**"}, paymentInfo={ "total": 0.5, "currency": "USD", "items": [ {"name": "a", "description": "b", "amount": 0.25, "quantity": 1}, {"name": "a2", "description": "b2", "amount": 0.25, "quantity": 1}, ], }, payment_status_detail=[ PaymentStatusDetailItem( amount="1080.00", method="paypal_ipn", currency="USD", id="id1", date=datetime.datetime.now(), ) ], paid=True, amount_paid="1080.0", ).save() ipn_value = f"mc_gross=-1080.00&protection_eligibility=Eligible&item_number1=Double Room&payer_id=A4CSL993V3BDG&address_street=1 Main St&payment_date=07:43:04 Jun 23, 2019 PDT&payment_status=Refunded&charset=windows-1252&address_zip=95131&mc_shipping=0.00&mc_handling=0.00&first_name=test&mc_fee=-31.32&address_country_code=US&address_name=test buyer¬ify_version=3.9&reason_code=refund&custom={responseId}&[email protected]&address_country=United States&mc_handling1=0.00&address_city=San Jose&verify_sign=Ajpb2sm-lsDSWA5XwZfLh0PpsG6IAYl-yqrJx2GLHimSQ3aPkkfr3i2Y&[email protected]&mc_shipping1=0.00&tax1=0.00&parent_txn_id=23X19291EC9014330&txn_id=69B89632XK721821J&payment_type=instant&last_name=buyer&address_state=CA&item_name1=Double Room ($1200/person)&[email protected]&payment_fee=-31.32&shipping_discount=0.00&quantity1=1&receiver_id=T4A6C58SP7PP2&insurance_amount=0.00&discount=120.00&mc_gross_1=1200.00&mc_currency=USD&residence_country=US&test_ipn=1&shipping_method=Default&transaction_subject=&payment_gross=-1080.00&ipn_track_id=539cae4d23348" response = self.send_ipn(responseId, ipn_value) response = self.view_response(responseId) self.assertTrue("payment_status_detail" in response) self.assertEqual(len(response["payment_trail"]), 1) detail_history_one = response["payment_trail"][0] detail_history_one.pop("date") # TODO: why do these two not exist? detail_history_one.pop("date_created") detail_history_one.pop("date_modified") self.assertEqual( detail_history_one, { "value": { "mc_gross": "-1080.00", "protection_eligibility": "Eligible", "item_number1": "Double Room", "payer_id": "A4CSL993V3BDG", "address_street": "1 Main St", "payment_date": "07:43:04 Jun 23, 2019 PDT", "payment_status": "Refunded", "charset": "windows-1252", "address_zip": "95131", "mc_shipping": "0.00", "mc_handling": "0.00", "first_name": "test", "mc_fee": "-31.32", "address_country_code": "US", "address_name": "test buyer", "notify_version": "3.9", "reason_code": "refund", "custom": responseId, "business": "*****@*****.**", "address_country": "United States", "mc_handling1": "0.00", "address_city": "San Jose", "verify_sign": "Ajpb2sm-lsDSWA5XwZfLh0PpsG6IAYl-yqrJx2GLHimSQ3aPkkfr3i2Y", "payer_email": "*****@*****.**", "mc_shipping1": "0.00", "tax1": "0.00", "parent_txn_id": "23X19291EC9014330", "txn_id": "69B89632XK721821J", "payment_type": "instant", "last_name": "buyer", "address_state": "CA", "item_name1": "Double Room ($1200/person)", "receiver_email": "*****@*****.**", "payment_fee": "-31.32", "shipping_discount": "0.00", "quantity1": "1", "receiver_id": "T4A6C58SP7PP2", "insurance_amount": "0.00", "discount": "120.00", "mc_gross_1": "1200.00", "mc_currency": "USD", "residence_country": "US", "test_ipn": "1", "shipping_method": "Default", "payment_gross": "-1080.00", "ipn_track_id": "539cae4d23348", }, "method": "paypal_ipn", "status": "SUCCESS", "id": "69B89632XK721821J", "_cls": "chalicelib.models.PaymentTrailItem", }, ) self.assertTrue("email_trail" in response) self.assertEqual(len(response["email_trail"]), 1) self.assertEqual(response["paid"], False) self.assertEqual(response["amount_paid"], "0.0")