Exemple #1
0
def oauth_return(request, *args, **kwargs):
    if 'payment_paypal_oauth_event' not in request.session:
        messages.error(request, _('An error occurred during connecting with PayPal, please try again.'))
        return redirect(reverse('control:index'))

    event = get_object_or_404(Event, pk=request.session['payment_paypal_oauth_event'])

    prov = Paypal(event)
    prov.init_api()

    try:
        tokeninfo = Tokeninfo.create(request.GET.get('code'))
        userinfo = Tokeninfo.create_with_refresh_token(tokeninfo['refresh_token']).userinfo()
    except:
        logger.exception('Failed to obtain OAuth token')
        messages.error(request, _('An error occurred during connecting with PayPal, please try again.'))
    else:
        messages.success(request,
                         _('Your PayPal account is now connected to pretix. You can change the settings in '
                           'detail below.'))

        event.settings.payment_paypal_connect_refresh_token = tokeninfo['refresh_token']
        event.settings.payment_paypal_connect_user_id = userinfo.email

    return redirect(reverse('control:event.settings.payment.provider', kwargs={
        'organizer': event.organizer.slug,
        'event': event.slug,
        'provider': 'paypal'
    }))
Exemple #2
0
    def checkout_prepare(self, request, cart):
        self.init_api()
        kwargs = {}
        if request.resolver_match and 'cart_namespace' in request.resolver_match.kwargs:
            kwargs['cart_namespace'] = request.resolver_match.kwargs[
                'cart_namespace']

        if request.event.settings.payment_paypal_connect_user_id:
            userinfo = Tokeninfo.create_with_refresh_token(
                request.event.settings.payment_paypal_connect_refresh_token
            ).userinfo()
            request.event.settings.payment_paypal_connect_user_id = userinfo.email
            payee = {
                "email": request.event.settings.payment_paypal_connect_user_id,
                # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                # we should use it instead of the merchant's eMail-address
                # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
            }
        else:
            payee = {}

        payment = paypalrestsdk.Payment({
            'intent':
            'sale',
            'payer': {
                "payment_method": "paypal",
            },
            "redirect_urls": {
                "return_url":
                build_absolute_uri(request.event,
                                   'plugins:paypal:return',
                                   kwargs=kwargs),
                "cancel_url":
                build_absolute_uri(request.event,
                                   'plugins:paypal:abort',
                                   kwargs=kwargs),
            },
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name": __('Order for %s') % str(request.event),
                        "quantity": 1,
                        "price": self.format_price(cart['total']),
                        "currency": request.event.currency
                    }]
                },
                "amount": {
                    "currency": request.event.currency,
                    "total": self.format_price(cart['total'])
                },
                "description":
                __('Event tickets for {event}').format(
                    event=request.event.name),
                "payee":
                payee
            }]
        })
        request.session['payment_paypal_order'] = None
        return self._create_payment(request, payment)
Exemple #3
0
    def payment_prepare(self, request, payment_obj):
        self.init_api()

        if request.event.settings.payment_paypal_connect_user_id:
            userinfo = Tokeninfo.create_with_refresh_token(
                request.event.settings.payment_paypal_connect_refresh_token
            ).userinfo()
            request.event.settings.payment_paypal_connect_user_id = userinfo.email
            payee = {
                "email": request.event.settings.payment_paypal_connect_user_id,
                # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                # we should use it instead of the merchant's eMail-address
                # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
            }
        else:
            payee = {}

        payment = paypalrestsdk.Payment({
            'intent':
            'sale',
            'payer': {
                "payment_method": "paypal",
            },
            "redirect_urls": {
                "return_url":
                build_absolute_uri(request.event, 'plugins:paypal:return'),
                "cancel_url":
                build_absolute_uri(request.event, 'plugins:paypal:abort'),
            },
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name":
                        __('Order {slug}-{code}').format(
                            slug=self.event.slug.upper(),
                            code=payment_obj.order.code),
                        "quantity":
                        1,
                        "price":
                        self.format_price(payment_obj.amount),
                        "currency":
                        payment_obj.order.event.currency
                    }]
                },
                "amount": {
                    "currency": request.event.currency,
                    "total": self.format_price(payment_obj.amount)
                },
                "description":
                __('Order {order} for {event}').format(
                    event=request.event.name, order=payment_obj.order.code),
                "payee":
                payee
            }]
        })
        request.session['payment_paypal_order'] = payment_obj.order.pk
        request.session['payment_paypal_payment'] = payment_obj.pk
        return self._create_payment(request, payment)
Exemple #4
0
    def payment_prepare(self, request, payment_obj):
        self.init_api()

        if request.event.settings.payment_paypal_connect_user_id:
            userinfo = Tokeninfo.create_with_refresh_token(request.event.settings.payment_paypal_connect_refresh_token).userinfo()
            request.event.settings.payment_paypal_connect_user_id = userinfo.email
            payee = {
                "email": request.event.settings.payment_paypal_connect_user_id,
                # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                # we should use it instead of the merchant's eMail-address
                # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
            }
        else:
            payee = {}

        payment = paypalrestsdk.Payment({
            'intent': 'sale',
            'payer': {
                "payment_method": "paypal",
            },
            "redirect_urls": {
                "return_url": build_absolute_uri(request.event, 'plugins:paypal:return'),
                "cancel_url": build_absolute_uri(request.event, 'plugins:paypal:abort'),
            },
            "transactions": [
                {
                    "item_list": {
                        "items": [
                            {
                                "name": __('Order {slug}-{code}').format(slug=self.event.slug.upper(),
                                                                         code=payment_obj.order.code),
                                "quantity": 1,
                                "price": self.format_price(payment_obj.amount),
                                "currency": payment_obj.order.event.currency
                            }
                        ]
                    },
                    "amount": {
                        "currency": request.event.currency,
                        "total": self.format_price(payment_obj.amount)
                    },
                    "description": __('Order {order} for {event}').format(
                        event=request.event.name,
                        order=payment_obj.order.code
                    ),
                    "payee": payee
                }
            ]
        })
        request.session['payment_paypal_order'] = payment_obj.order.pk
        request.session['payment_paypal_payment'] = payment_obj.pk
        return self._create_payment(request, payment)
Exemple #5
0
    def checkout_prepare(self, request, cart):
        self.init_api()
        kwargs = {}
        if request.resolver_match and 'cart_namespace' in request.resolver_match.kwargs:
            kwargs['cart_namespace'] = request.resolver_match.kwargs['cart_namespace']

        if request.event.settings.payment_paypal_connect_user_id:
            userinfo = Tokeninfo.create_with_refresh_token(request.event.settings.payment_paypal_connect_refresh_token).userinfo()
            request.event.settings.payment_paypal_connect_user_id = userinfo.email
            payee = {
                "email": request.event.settings.payment_paypal_connect_user_id,
                # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                # we should use it instead of the merchant's eMail-address
                # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
            }
        else:
            payee = {}

        payment = paypalrestsdk.Payment({
            'intent': 'sale',
            'payer': {
                "payment_method": "paypal",
            },
            "redirect_urls": {
                "return_url": build_absolute_uri(request.event, 'plugins:paypal:return', kwargs=kwargs),
                "cancel_url": build_absolute_uri(request.event, 'plugins:paypal:abort', kwargs=kwargs),
            },
            "transactions": [
                {
                    "item_list": {
                        "items": [
                            {
                                "name": __('Order for %s') % str(request.event),
                                "quantity": 1,
                                "price": self.format_price(cart['total']),
                                "currency": request.event.currency
                            }
                        ]
                    },
                    "amount": {
                        "currency": request.event.currency,
                        "total": self.format_price(cart['total'])
                    },
                    "description": __('Event tickets for {event}').format(event=request.event.name),
                    "payee": payee
                }
            ]
        })
        request.session['payment_paypal_order'] = None
        return self._create_payment(request, payment)
Exemple #6
0
    def checkout_prepare(self, request, cart):
        self.init_api()
        kwargs = {}
        if request.resolver_match and 'cart_namespace' in request.resolver_match.kwargs:
            kwargs['cart_namespace'] = request.resolver_match.kwargs[
                'cart_namespace']

        if request.event.settings.payment_paypal_connect_user_id:
            try:
                userinfo = Tokeninfo.create_with_refresh_token(
                    request.event.settings.payment_paypal_connect_refresh_token
                ).userinfo()
            except BadRequest as ex:
                ex = json.loads(ex.content)
                messages.error(
                    request, '{}: {} ({})'.format(
                        _('We had trouble communicating with PayPal'),
                        ex['error_description'], ex['correlation_id']))
                return

            request.event.settings.payment_paypal_connect_user_id = userinfo.email
            payee = {
                "email": request.event.settings.payment_paypal_connect_user_id,
                # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                # we should use it instead of the merchant's eMail-address
                # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
            }
        else:
            payee = {}

        payment = paypalrestsdk.Payment({
            'header': {
                'PayPal-Partner-Attribution-Id': 'ramiioSoftwareentwicklung_SP'
            },
            'intent':
            'sale',
            'payer': {
                "payment_method": "paypal",
            },
            "redirect_urls": {
                "return_url":
                build_absolute_uri(request.event,
                                   'plugins:paypal:return',
                                   kwargs=kwargs),
                "cancel_url":
                build_absolute_uri(request.event,
                                   'plugins:paypal:abort',
                                   kwargs=kwargs),
            },
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name": __('Order for %s') % str(request.event),
                        "quantity": 1,
                        "price": self.format_price(cart['total']),
                        "currency": request.event.currency
                    }]
                },
                "amount": {
                    "currency": request.event.currency,
                    "total": self.format_price(cart['total'])
                },
                "description":
                __('Event tickets for {event}').format(
                    event=request.event.name),
                "payee":
                payee
            }]
        })
        request.session['payment_paypal_order'] = None
        return self._create_payment(request, payment)
Exemple #7
0
    def payment_prepare(self, request, payment_obj):
        self.init_api()

        try:
            if request.event.settings.payment_paypal_connect_user_id:
                try:
                    tokeninfo = Tokeninfo.create_with_refresh_token(
                        request.event.settings.
                        payment_paypal_connect_refresh_token)
                except BadRequest as ex:
                    ex = json.loads(ex.content)
                    messages.error(
                        request, '{}: {} ({})'.format(
                            _('We had trouble communicating with PayPal'),
                            ex['error_description'], ex['correlation_id']))
                    return

                # Even if the token has been refreshed, calling userinfo() can fail. In this case we just don't
                # get the userinfo again and use the payment_paypal_connect_user_id that we already have on file
                try:
                    userinfo = tokeninfo.userinfo()
                    request.event.settings.payment_paypal_connect_user_id = userinfo.email
                except UnauthorizedAccess:
                    pass

                payee = {
                    "email":
                    request.event.settings.payment_paypal_connect_user_id,
                    # If PayPal ever offers a good way to get the MerchantID via the Identifity API,
                    # we should use it instead of the merchant's eMail-address
                    # "merchant_id": request.event.settings.payment_paypal_connect_user_id,
                }
            else:
                payee = {}

            payment = paypalrestsdk.Payment({
                'header': {
                    'PayPal-Partner-Attribution-Id':
                    'ramiioSoftwareentwicklung_SP'
                },
                'intent':
                'sale',
                'payer': {
                    "payment_method": "paypal",
                },
                "redirect_urls": {
                    "return_url":
                    build_absolute_uri(request.event, 'plugins:paypal:return'),
                    "cancel_url":
                    build_absolute_uri(request.event, 'plugins:paypal:abort'),
                },
                "transactions": [{
                    "item_list": {
                        "items": [{
                            "name": ('{} '.format(self.settings.prefix)
                                     if self.settings.prefix else '') +
                            __('Order {slug}-{code}').format(
                                slug=self.event.slug.upper(),
                                code=payment_obj.order.code),
                            "quantity":
                            1,
                            "price":
                            self.format_price(payment_obj.amount),
                            "currency":
                            payment_obj.order.event.currency
                        }]
                    },
                    "amount": {
                        "currency": request.event.currency,
                        "total": self.format_price(payment_obj.amount)
                    },
                    "description":
                    ('{} '.format(self.settings.prefix) if self.settings.prefix
                     else '') + __('Order {order} for {event}').format(
                         event=request.event.name,
                         order=payment_obj.order.code),
                    "payee":
                    payee
                }]
            })
            request.session['payment_paypal_payment'] = payment_obj.pk
            return self._create_payment(request, payment)
        except paypalrestsdk.exceptions.ConnectionError as e:
            messages.error(request,
                           _('We had trouble communicating with PayPal'))
            logger.exception('Error on creating payment: ' + str(e))
Exemple #8
0
def refreshToken(request):

    refreshtoken = request.session['refresh_token']
    tokeninfo = Tokeninfo.create_with_refresh_token(refreshtoken)
    request.session['id_token'] = tokeninfo.id_token
    request.session['access_token'] = tokeninfo.access_token
#  tokeninfo = Tokeninfo.create(code)
#  print(tokeninfo)
#  # Response 200
#  tokeninfo = {
#    token_type: 'Bearer',
#    expires_in: '28800',
#    refresh_token: 'J5yFACP3Y5dqdWCdN3o9lNYz0XyR01IHNMQn-E4r6Ss38rqbQ1C4rC6PSBhJvB_tte4WZsWe8ealMl-U_GMSz30dIkKaovgN41Xf8Sz0EGU55da6tST5I6sg3Rw',
#    id_token: '<some value>',
#    access_token: '<some value>'
#  }
#end

# Step 4. You can use the refresh token to get the third party merchant's email address to use in the invoice, and then you can create an invoice on behalf of the third party merchant.
refresh_token = "J5yFACP3Y5dqdWCdN3o9lNYz0XyR01IHNMQn-E4r6Ss38rqbQ1C4rC6PSBhJvB_tte4WZsWe8ealMl-U_GMSz30dIkKaovgN41Xf8Sz0EGU55da6tST5I6sg3Rw"

token_info = Tokeninfo.create_with_refresh_token(refresh_token)

print(token_info)

user_info = token_info.userinfo()

print(user_info)

invoice = Invoice({
    "merchant_info": {
        # The email address used here would be of a third party.
        "email": user_info.email,
        "first_name": "Dennis",
        "last_name": "Doctor",
        "business_name": "Medical Professionals, LLC",
        "phone": {
Exemple #10
0
#     print("Payment[%s] execute successfully" % (payment.id))
#   else:
#     print(payment.error)

# else:
#   print(payment.error)

from paypalrestsdk.openid_connect import Tokeninfo, Userinfo


# Generate login url
login_url = Tokeninfo.authorize_url({ "scope": "openid profile"})

# Create tokeninfo with Authorize code
tokeninfo = Tokeninfo.create("Replace with Authorize code")

# Refresh tokeninfo
tokeninfo = tokeninfo.refresh()

# Create tokeninfo with refresh_token
tokeninfo = Tokeninfo.create_with_refresh_token("Replace with refresh_token")

# Get userinfo
userinfo  = tokeninfo.userinfo()

# Get userinfo with access_token
userinfo  = Userinfo.get("Replace with access_token")

# Generate logout url
logout_url = tokeninfo.logout_url()