def testResultBadFormData(self): data = self.data del data['eshopAccount'] client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertTrue('eshopAccount' in form.errors)
def testResultBadInvoiceDoesNotFound(self): data = self.data hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 200) form = ResultUrlForm(data) self.assertTrue(form.is_valid())
def testResultAlreadyHavePaymentStatus(self): data = self.data data['orderId'] = self.inv.id hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200)
def testResultBadFormData(self): data = self.data del data['eshopAccount'] client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertTrue('eshopAccount' in form.errors) self._assertTicketExists()
def testResultWithUnknownStatus(self): data = self.data data['paymentStatus'] = 4 hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200)
def testResultBadSecretKey(self): settings.SEND_SECRETKEY = True data = self.data data['secretKey'] = data['secretKey'] + '343' client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 1) self.assertTrue('secretKey' in form.errors)
def testResultBadShopId(self): settings.SEND_SECRETKEY = False data = self.data data['eshopId'] = '%s1' % settings.SHOPID data['hash'] = 1 client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 2) self.assertTrue('eshopId' in form.errors)
def testResultBadSecretKey(self): settings.SEND_SECRETKEY = True data = self.data data['secretKey'] = data['secretKey'] + '343' client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 1) self.assertTrue('secretKey' in form.errors) self._assertTicketExists()
def testResultAlreadyHavePaymentStatus(self): data = self.data data['orderId'] = inv.id hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200) self.assertFalse(payments.exists()) self._assertTicketExists()
def testResultBadShopId(self): settings.SEND_SECRETKEY = False data = self.data data['eshopId'] = '%s1' % settings.SHOPID data['hash'] = 1 client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 2) self.assertTrue('eshopId' in form.errors) self._assertTicketExists()
def testResult(self): amount = '10.11' data = self.data data['orderId'] = self.inv.id hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200) orderId = data['orderId'] recipientAmount = data['recipientAmount']
def testResultWithUnknownStatus(self): data = self.data data['paymentStatus'] = 4 hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200) # TODO: Существование платежей self.assertFalse(payments.exists()) self._assertTicketExists()
def testResultWithUnknownStatus(self): data = self.data data['paymentStatus'] = 4 hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200) #TODO: Существование платежей self.assertFalse(payments.exists()) self._assertTicketExists()
def testResultBadInvoiceDoesNotFound(self): data = self.data hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 200) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) #TODO: Существование счетов self.assertTrue(invoices.exists()) #TODO: Существование платежей self.assertTrue(payments.exists()) self._assertTicketExists()
def testResultBadHash(self): settings.SEND_SECRETKEY = False data = self.data hash = getHashOnReceiveResult(data) data['hash'] = hash + '343' client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 400) form = ResultUrlForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 1) self.assertTrue('__all__' in form.errors) self.assertTrue('hash' in unicode(form.errors['__all__'])) self._assertTicketExists()
def testResultBadInvoiceDoesNotFound(self): data = self.data hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) self.assertEqual(response.status_code, 200) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) # TODO: Существование счетов self.assertTrue(invoices.exists()) # TODO: Существование платежей self.assertTrue(payments.exists()) self._assertTicketExists()
def receive_result(request): request.encoding = 'cp1251' # explicitly set encoding ip = request.META['REMOTE_ADDR'] preffix = 'IntellectMoney: ' info = request.POST if ip != settings.IP: subject = u'%sОповещение о платеже с неправильного ip' % preffix mail_admins(subject, message=u'Дата: %s' % info) raise Http404 form = ResultUrlForm(request.POST) if form.is_valid(): data = form.cleaned_data orderId = data['orderId'] recipientAmount = data['recipientAmount'] paymentId = data['paymentId'] invoice = get_object_or_None(IntellectMoney, orderId=orderId) if not invoice: subject = u'%sОповещение об оплате несуществующего счета #%s' % ( preffix, paymentId ) mail_admins(subject, message=u'Дата: %s' % info) return HttpResponse('OK') paymentStatus = data['paymentStatus'] if paymentStatus in [5, 6, 7]: subject = u'Оплата через intellectmoney #%s' % paymentId if paymentStatus == 6: message = u'%sОплачен счет %s (ЗАБЛОКИРОВАНО %s руб)' % ( preffix, orderId, recipientAmount, ) else: message = u'%sОплачен счет %s (%s руб)' % ( preffix, orderId, recipientAmount, ) mail_admins(subject, message=message) result_received.send( sender=invoice, orderId=orderId, recipientAmount=recipientAmount, ) elif paymentStatus == 3: return HttpResponse('OK') else: subject = u'%sПришло оповещение с неожидаемым статусом' % preffix mail_admins(subject, message=u'Дата: %s' % info) return HttpResponse('OK') else: subject = u'%sФорма оповещения платежа: невалидные данные' % preffix body = u'Ошибки в форме: %s\n\nДанные:%s' % (unicode(form.errors), info) mail_admins(subject, message=body) return HttpResponse('Bad', status=400)
def testResult(self): amount = '10.11' data = self.data data['orderId'] = inv.id hash = getHashOnReceiveResult(data) data['hash'] = hash client = self.client response = client.post(self.url, data) form = ResultUrlForm(data) self.assertTrue(form.is_valid()) self.assertEqual(response.status_code, 200) orderId = data['orderId'] recipientAmount = data['recipientAmount'] self.assertTrue(payments.exists()) payment = payments[0] self.assertTrue(invoices.exists())
def receive_result(request): ip = request.META['REMOTE_ADDR'] preffix = 'IntellectMoney: ' info = request.POST if settings.CHECK_IP_ENABLED and ip != settings.IP: subject = u'{}Оповещение о платеже с неправильного ip={}'.format( preffix, ip) message = u'Дата: {}'.format(info) _send_admin_email(subject, message) raise Http404 form = ResultUrlForm(request.POST) if not form.is_valid(): subject = '{}Форма оповещения платежа: невалидные данные'.format( preffix) body = 'Ошибки в форме: {}\n\nДанные:{}'.format(form.errors, info) logger.error('Form errors %s, %s', form.errors, info) _send_admin_email(subject, body) return HttpResponse('Bad', status=400) data = form.cleaned_data orderId = data['orderId'] recipientAmount = data['recipientAmount'] paymentId = data['paymentId'] reccuringState = data['reccuringState'] try: invoice = IntellectMoney.objects.get(orderId=orderId) except IntellectMoney.DoesNotExist: invoice = None if not invoice: subject = u'{}Оповещение об оплате несуществующего счета #{}'.format( preffix, paymentId) _send_admin_email(subject, u'Дата: {}'.format(info)) return HttpResponse('OK') if not invoice.paymentId: invoice.paymentId = paymentId invoice.save() paymentStatus = data['paymentStatus'] if paymentStatus in [5, 6, 7]: subject = u'Оплата через intellectmoney #{}'.format(paymentId) if paymentStatus == 6: message = u'{}Оплачен счет {} (ЗАБЛОКИРОВАНО {} руб)'.format( preffix, orderId, recipientAmount) else: message = u'{}Оплачен счет {} ({} руб)'.format( preffix, orderId, recipientAmount) _send_admin_email(subject, message) result_received.send( sender=invoice, orderId=orderId, recipientAmount=recipientAmount, reccuringState=reccuringState, paymentId=paymentId, ) elif paymentStatus == 3: return HttpResponse('OK') else: subject = u'{}Пришло оповещение с неожидаемым статусом'.format(preffix) _send_admin_email(subject, u'Дата: %s' % info) return HttpResponse('OK')