def handle_request(self, request):
        """
        Handle the income request which can have two different forms:
            1. user coming back on the store after clicking on a "go back to store" button
            OR
            2. a notification of payment issued from SytemPay server to our server

        In both cases the data will be POST data, the only difference is that in the case 2. the 
        POST data will contain a ``hash`` parameter that needs to be include in the signature computation
        """
        form = self.get_return_form_populated_with_request(request)

        # create the database record
        order_number = self.get_order_number(form)
        total_incl_tax = self.get_total_incl_tax(form)
        txn = self.record_return_txn(order_number, total_incl_tax, request)

        if not form.is_valid():
            txn.error_message = printable_form_errors(form)
            txn.save()
            raise SystemPayFormNotValid("The data received are not complete: %s. See the transaction record #%s for more details" % (
                    printable_form_errors(form), txn.id)
                )

        if not self.gateway.is_signature_valid(form):
            txn.error_message = u"Signature not valid. Get '%s' vs Expected '%s'" % (
                    form.cleaned_data['signature'], self.gateway.compute_signature(form) 
                )
            txn.save()
            raise SystemPayFormNotValid("The data received are not valid. See the transaction record #%s for more details" % txn.id)

        if not txn.is_complete():

            if txn.result == '02':
                raise SystemPayGatewayPaymentRejected("The shop must contact the bank")
            elif txn.result == '05':
                raise SystemPayGatewayPaymentRejected("The payment has been rejected")
            elif txn.result == '30':
                extra_result = self.get_extra_result(form)
                raise SystemPayGatewayParamError(code=extra_result)
            elif txn.result == '96':
                raise SystemPayGatewayServerError("Technical error while processing the payment")
            else:
                raise SystemPayGatewayServerError("Unknown error")

            # TODO handle the ``auth_result`` param
            # auth_result = self.get_auth_result(form)
            # if auth_result == '':
            #     raise SystemPayGatewayError

        return txn
 def test_is_signature_valid(self):
     form = self.facade.get_return_form_populated_with_request(self.request)
     # form.is_valid()
     # print form.compute_signature()$
     self.assertTrue( form.is_valid(), msg=u"Errors: %s" % printable_form_errors(form) )
     self.assertEqual( len(form.cleaned_data['signature']), 40 )
     self.assertTrue( self.facade.gateway.is_signature_valid(form), msg=u"data: %s, excepted: %s" % (
         form.cleaned_data['signature'], self.facade.gateway.compute_signature(form)) )
 def test_is_signature_valid(self):
     form = self.facade.get_return_form_populated_with_request(self.request)
     # form.is_valid()
     # print form.compute_signature()$
     self.assertTrue( form.is_valid(), msg=u"Errors: %s" % printable_form_errors(form) )
     self.assertEqual( len(form.cleaned_data['signature']), 40 )
     self.assertTrue( self.facade.gateway.is_signature_valid(form), msg=u"data: %s, excepted: %s" % (
         form.cleaned_data['signature'], self.facade.gateway.compute_signature(form)) )
 def test_is_signature_valid(self):
     form = self.create_submit_form_with_order(self.order)
     self.assertTrue( form.is_valid(), msg=u"Errors: %s" % printable_form_errors(form) )
     self.assertEqual( len(form.cleaned_data['signature']), 40 )
     print form.cleaned_data
     self.assertEqual( form.cleaned_data['signature'], '5e1c92ed97089f3192c8c729236ec87567048590' )
 def test_is_signature_valid(self):
     form = self.create_submit_form_with_order(self.order)
     self.assertTrue( form.is_valid(), msg=u"Errors: %s" % printable_form_errors(form) )
     self.assertEqual( len(form.cleaned_data['signature']), 40 )
     print form.cleaned_data
     self.assertEqual( form.cleaned_data['signature'], '5e1c92ed97089f3192c8c729236ec87567048590' )