Beispiel #1
0
 def init_amazon_payments(self):
     """
     Creates a `session` and `api` variables to be used for interacting
     with the Amazon Payments API. Returns True if successful, else
     returns False
     """
     try:
         self.session = self.request.basket.amazonpaymentssession
     except (AmazonPaymentsSession.DoesNotExist, AttributeError):
         return False
     logger.debug("Amazon Billing Agreement ID: %s" %
                  (self.session.billing_agreement_id))
     self.api = AmazonPaymentsAPI(
         settings.AMAZON_PAYMENTS_ACCESS_KEY,
         settings.AMAZON_PAYMENTS_SECRET_KEY,
         settings.AMAZON_PAYMENTS_SELLER_ID,
         settings.AMAZON_PAYMENTS_API_ENDPOINT,
         settings.AMAZON_PAYMENTS_API_VERSION,
         settings.AMAZON_PAYMENTS_IS_LIVE,
     )
     return True
 def init_amazon_payments(self):
     """
     Creates a `session` and `api` variables to be used for interacting
     with the Amazon Payments API. Returns True if successful, else
     returns False
     """
     try:
         self.session = self.request.basket.amazonpaymentssession
     except (AmazonPaymentsSession.DoesNotExist, AttributeError):
         return False
     logger.debug("Amazon Billing Agreement ID: %s" % (
         self.session.billing_agreement_id))
     self.api = AmazonPaymentsAPI(
         settings.AMAZON_PAYMENTS_ACCESS_KEY,
         settings.AMAZON_PAYMENTS_SECRET_KEY,
         settings.AMAZON_PAYMENTS_SELLER_ID,
         settings.AMAZON_PAYMENTS_API_ENDPOINT,
         settings.AMAZON_PAYMENTS_API_VERSION,
         settings.AMAZON_PAYMENTS_IS_LIVE,
     )
     return True
Beispiel #3
0
class AmazonCheckoutView(object):
    def init_amazon_payments(self):
        """
        Creates a `session` and `api` variables to be used for interacting
        with the Amazon Payments API. Returns True if successful, else
        returns False
        """
        try:
            self.session = self.request.basket.amazonpaymentssession
        except (AmazonPaymentsSession.DoesNotExist, AttributeError):
            return False
        logger.debug("Amazon Billing Agreement ID: %s" %
                     (self.session.billing_agreement_id))
        self.api = AmazonPaymentsAPI(
            settings.AMAZON_PAYMENTS_ACCESS_KEY,
            settings.AMAZON_PAYMENTS_SECRET_KEY,
            settings.AMAZON_PAYMENTS_SELLER_ID,
            settings.AMAZON_PAYMENTS_API_ENDPOINT,
            settings.AMAZON_PAYMENTS_API_VERSION,
            settings.AMAZON_PAYMENTS_IS_LIVE,
        )
        return True

    def save_to_db_callback(self, raw_request, raw_response):
        return self.session.transactions.create(request=raw_request,
                                                response=raw_response)

    def get_amazon_payments_context_vars(self):
        """
        Returns a dict with all the Amazon Payments data that would
        be needed in a template in order to display widgets.
        """
        return {
            'amazon_payments_seller_id':
            settings.AMAZON_PAYMENTS_SELLER_ID,
            'amazon_payments_client_id':
            settings.AMAZON_PAYMENTS_CLIENT_ID,
            'amazon_payments_is_live':
            settings.AMAZON_PAYMENTS_IS_LIVE,
            'amazon_payments_billing_agreement_id':
            (self.session.billing_agreement_id),
        }

    def get_amazon_order_details(self, request, **kwargs):
        """
        Preforms a GetBillingAgreementDetails request, and checks if
        there the user has set a valid shipping address (if
        validate_shipping_address is True) and/or there is a valid
        payment method (if validate_payment_details is True).
        """
        if kwargs.get("validate_shipping_address", True):
            kwargs["valid_shipping_countries"] = Country.objects\
                .filter(is_shipping_country=True)\
                .values_list("iso_3166_1_a2", flat=True)
        kwargs["callback"] = self.save_to_db_callback
        success, result = self.api.get_amazon_order_details(
            self.session.billing_agreement_id, self.session.access_token,
            getattr(request.basket, "has_subscriptions", False), **kwargs)
        if success:
            return result
        for error in result:
            messages.error(request, _(error))

    def check_user_email_is_captured(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        if not request.user.is_authenticated() \
                and not self.checkout_session.get_guest_email():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-index'),
                message=_("Please either sign in or enter your email address"))

    def check_basket_requires_shipping(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        # Check to see that a shipping address is actually required.  It may
        # not be if the basket is purely downloads
        if not request.basket.is_shipping_required():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-method'),
                message=_("Your basket does not require a shipping"
                          "address to be submitted"))

    def check_shipping_data_is_captured(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        if not request.basket.is_shipping_required():
            return

        # Check that shipping address has been completed
        if not self.checkout_session.is_shipping_address_set():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-address'),
                message=_("Please choose a shipping address"))

        # Check that shipping method has been set
        if not self.checkout_session.is_shipping_method_set(request.basket):
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-method'),
                message=_("Please choose a shipping method"))
class AmazonCheckoutView(object):

    def init_amazon_payments(self):
        """
        Creates a `session` and `api` variables to be used for interacting
        with the Amazon Payments API. Returns True if successful, else
        returns False
        """
        try:
            self.session = self.request.basket.amazonpaymentssession
        except (AmazonPaymentsSession.DoesNotExist, AttributeError):
            return False
        logger.debug("Amazon Billing Agreement ID: %s" % (
            self.session.billing_agreement_id))
        self.api = AmazonPaymentsAPI(
            settings.AMAZON_PAYMENTS_ACCESS_KEY,
            settings.AMAZON_PAYMENTS_SECRET_KEY,
            settings.AMAZON_PAYMENTS_SELLER_ID,
            settings.AMAZON_PAYMENTS_API_ENDPOINT,
            settings.AMAZON_PAYMENTS_API_VERSION,
            settings.AMAZON_PAYMENTS_IS_LIVE,
        )
        return True

    def save_to_db_callback(self, raw_request, raw_response):
        return self.session.transactions.create(
            request=raw_request, response=raw_response)

    def get_amazon_payments_context_vars(self):
        """
        Returns a dict with all the Amazon Payments data that would
        be needed in a template in order to display widgets.
        """
        return {
            'amazon_payments_seller_id': settings.AMAZON_PAYMENTS_SELLER_ID,
            'amazon_payments_client_id': settings.AMAZON_PAYMENTS_CLIENT_ID,
            'amazon_payments_is_live': settings.AMAZON_PAYMENTS_IS_LIVE,
            'amazon_payments_billing_agreement_id': (
                self.session.billing_agreement_id),
        }

    def get_amazon_order_details(self, request, **kwargs):
        """
        Preforms a GetBillingAgreementDetails request, and checks if
        there the user has set a valid shipping address (if
        validate_shipping_address is True) and/or there is a valid
        payment method (if validate_payment_details is True).
        """
        if kwargs.get("validate_shipping_address", True):
            kwargs["valid_shipping_countries"] = Country.objects\
                .filter(is_shipping_country=True)\
                .values_list("iso_3166_1_a2", flat=True)
        kwargs["callback"] = self.save_to_db_callback
        success, result = self.api.get_amazon_order_details(
            self.session.billing_agreement_id, self.session.access_token,
            getattr(request.basket, "has_subscriptions", False), **kwargs)
        if success:
            return result
        for error in result:
            messages.error(request, _(error))

    def check_user_email_is_captured(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        if not request.user.is_authenticated() \
                and not self.checkout_session.get_guest_email():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-index'),
                message=_(
                    "Please either sign in or enter your email address")
            )

    def check_basket_requires_shipping(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        # Check to see that a shipping address is actually required.  It may
        # not be if the basket is purely downloads
        if not request.basket.is_shipping_required():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-method'),
                message=_(
                    "Your basket does not require a shipping"
                    "address to be submitted")
            )

    def check_shipping_data_is_captured(self, request):
        """
        Overrides Oscar's pre-condition to change URL to redirect
        to if condition not satisfied.
        """
        if not request.basket.is_shipping_required():
            return

        # Check that shipping address has been completed
        if not self.checkout_session.is_shipping_address_set():
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-address'),
                message=_("Please choose a shipping address")
            )

        # Check that shipping method has been set
        if not self.checkout_session.is_shipping_method_set(
                request.basket):
            raise FailedPreCondition(
                url=reverse('checkout:amazon-payments-shipping-method'),
                message=_("Please choose a shipping method")
            )