Ejemplo n.º 1
0
    def test_open(self):
        """ Verify the open method calls the API and returns the response content. """
        request = Request(API_URL)
        body = str(uuid.uuid4())
        httpretty.register_uri(httpretty.GET, API_URL, body=body, content_type=CONTENT_TYPE)

        transport = RequestsTransport()
        response = transport.open(request).getvalue()
        self.assertEqual(response, body)
Ejemplo n.º 2
0
    def test_open(self):
        """ Verify the open method calls the API and returns the response content. """
        request = Request(API_URL)
        body = str(uuid.uuid4())
        httpretty.register_uri(httpretty.GET,
                               API_URL,
                               body=body,
                               content_type=CONTENT_TYPE)

        transport = RequestsTransport()
        response = transport.open(request).getvalue()
        self.assertEqual(response, body)
Ejemplo n.º 3
0
    def test_send(self):
        """ Verify the send method POSTs data to the API and returns a Reply object. """
        request = Request(API_URL)
        body = str(uuid.uuid4())
        httpretty.register_uri(httpretty.POST,
                               API_URL,
                               body=body,
                               content_type=CONTENT_TYPE,
                               forcing_headers={'date': 'never'})

        transport = RequestsTransport()
        response = transport.send(request)

        self.assertEqual(response.code, 200)
        self.assertEqual(response.headers, {
            'date': 'never',
            'content-type': CONTENT_TYPE
        })
        self.assertEqual(response.message, body)
Ejemplo n.º 4
0
    def test_send(self):
        """ Verify the send method POSTs data to the API and returns a Reply object. """
        request = Request(API_URL)
        body = str(uuid.uuid4())
        httpretty.register_uri(httpretty.POST,
                               API_URL,
                               body=body,
                               content_type=CONTENT_TYPE,
                               forcing_headers={'date': 'never'})

        transport = RequestsTransport()
        response = transport.send(request)

        self.assertEqual(response.code, 200)
        self.assertEqual(response.headers, {
            'date': 'never',
            'content-type': CONTENT_TYPE
        })
        self.assertEqual(response.message, body)
Ejemplo n.º 5
0
    def issue_credit(self, source, amount, currency):
        order = source.order

        try:
            order_request_token = source.reference

            security = Security()
            token = UsernameToken(self.merchant_id, self.transaction_key)
            security.tokens.append(token)

            client = Client(self.soap_api_url, transport=RequestsTransport())
            client.set_options(wsse=security)

            credit_service = client.factory.create('ns0:CCCreditService')
            credit_service._run = 'true'  # pylint: disable=protected-access
            credit_service.captureRequestID = source.reference

            purchase_totals = client.factory.create('ns0:PurchaseTotals')
            purchase_totals.currency = currency
            purchase_totals.grandTotalAmount = unicode(amount)

            response = client.service.runTransaction(
                merchantID=self.merchant_id,
                merchantReferenceCode=order.number,
                orderRequestToken=order_request_token,
                ccCreditService=credit_service,
                purchaseTotals=purchase_totals)
            request_id = response.requestID
            ppr = self.record_processor_response(
                suds_response_to_dict(response),
                transaction_id=request_id,
                basket=order.basket)
        except:
            msg = 'An error occurred while attempting to issue a credit (via CyberSource) for order [{}].'.format(
                order.number)
            logger.exception(msg)
            raise GatewayError(msg)

        if response.decision == 'ACCEPT':
            source.refund(amount, reference=request_id)
            event_type, __ = PaymentEventType.objects.get_or_create(
                name=PaymentEventTypeName.REFUNDED)
            PaymentEvent.objects.create(event_type=event_type,
                                        order=order,
                                        amount=amount,
                                        reference=request_id,
                                        processor_name=self.NAME)
        else:
            raise GatewayError(
                'Failed to issue CyberSource credit for order [{order_number}]. '
                'Complete response has been recorded in entry [{response_id}]'.
                format(order_number=order.number, response_id=ppr.id))