Skip to content

Instantly share code, notes, and snippets.

@nychng
Created February 28, 2013 07:38
Show Gist options
  • Save nychng/5054975 to your computer and use it in GitHub Desktop.
Save nychng/5054975 to your computer and use it in GitHub Desktop.
def test_paypal_callback(self):
response = self.client.login(username=self.user.email, password='foz')
self.assertTrue(response)
# create post params simulation from paypal
order = OrderFactory(event=self.event, cart=self.cart,
payment_method=self.payment_method)
total = '%.2f' % self.total
post_param = {
'protection_eligibility': 'Ineligible',
'last_name': 'User',
'txn_id': '5CX1007279882822D',
'receiver_email': settings.PAYPAL_RECEIVER_EMAIL,
'payment_status': 'Completed',
'payment_gross': total,
'tax': '0.00',
'residence_country': 'US',
'invoice': str(order.invoice),
'payer_status': 'verified',
'txn_type': 'web_accept',
'handling_amount': '0.00',
'payment_date': '20:14:30 Oct 26, 2011 PDT',
'first_name': 'Test',
'item_name': 'Tickets purchase for "%s" event' % self.event.name,
'charset': 'windows-1252',
'custom': str(self.cart.id),
'notify_version': '3.4',
'transaction_subject': '56',
'test_ipn': '1',
'item_number': '',
'receiver_id': '2WGLZMQJ2Q55S',
'business': 'nychng_1318992102_biz@yahoo.com',
'payer_id': 'H9EKJD2UM57NS',
'verify_sign': 'An5ns1Kso7MWUdW4ErQKJJJ4qi4-Akjuvg1YuENslOPf8ZbzcTsrCw0I',
'payment_fee': '0.62',
'mc_fee': '0.62',
'currency': 'USD',
'mc_currency': 'USD',
'shipping': '0.00',
'payer_email': 'nychng_1319511621_per@yahoo.com',
'payment_type': 'instant',
'mc_gross': total,
'ipn_track_id': 'cauOC6SwncFkBL9PDclc-Q',
'quantity': '1'
}
# Monkey patch over PayPalIPN to make it get a VERFIED response.
self.old_postback = PayPalIPN._postback
PayPalIPN._postback = lambda self: "VERIFIED"
# call paypal ipn with post params
notify_url = reverse('paypal-ipn')
response = self.client.post(notify_url, post_param, follow=True)
self.assertEqual(response.status_code, 200)
# check ipn object exists
self.assertEqual(PayPalIPN.objects.all().count(), 1)
ipn_obj = PayPalIPN.objects.all()[0]
self.assertEqual(ipn_obj.payer_email, post_param['payer_email'])
self.assertEqual(ipn_obj.custom, str(post_param['custom']))
self.assertEqual(ipn_obj.txn_id, post_param['txn_id'])
self.assertEqual(ipn_obj.payment_gross,
Decimal(post_param['payment_gross']))
self.assertEqual(ipn_obj.mc_gross, Decimal(post_param['mc_gross']))
# check updates for the cart and orders models
order = Order.objects.all()[0]
self.cart = Cart.objects.all()[0]
self.assertTrue(self.cart.paid)
self.assertEqual(self.cart.status, 'finished')
order = Order.objects.all()[0]
self.assertEqual(order.order_status, 2)
# ticket holding
self.assertEqual(TicketHolding.objects.all().count(), 1)
tix = TicketHolding.objects.all()[0]
self.assertEqual(tix.confirmed, 1)
# check pdf generated
order = Order.objects.all()[0]
path = '{0}{1}{2}{3}'.format(settings.MEDIA_ROOT,
'files/registration/', order.receipt, '.pdf')
self.assertTrue(os.path.exists(path))
# destroy pdf
os.remove(path)
self.assertFalse(os.path.exists(path))
# check email sent
#self.assertEqual(Notification.objects.all().count(), 1)
#item = Item.objects.all()[0]
#self.assertEqual(item.quantity_left, 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment