def _get_order_items(self, order): output = [] item_ids = [] for item in order.items.all(): item_ids.append(item.pk) formatted_price = moneyfmt(item.unit_price / Decimal(rate_exchange)) transaction = { 'name': item.product.fullname, 'sku': 'prod-' + item.product.fullname, 'price': formatted_price, 'currency': 'USD', 'quantity': item.quantity } output.append(transaction) # then try to add extra price there for eo in ExtraPriceOrderField.objects.filter(order=order): formatted_price = moneyfmt(eo.value / Decimal(rate_exchange)) transaction = { 'name': eo.label, 'sku': 'extra-' + eo.label, 'price': formatted_price, 'quantity': '1' } output.append(transaction) for eio in ExraPriceOrderItemField.objects.filter(order_item__pk__in=item_ids): formatted_price = moneyfmt(eio.value / Decimal(rate_exchange)) transaction = { 'name': eio.label, 'sku': 'extra-item-' + eo.label, 'price': formatted_price, 'quantity': '1' } output.append(transaction) return output
def get_redirect(self, order, request): absolute_uri = request.build_absolute_uri items = self._get_order_items(order) amount_total = sum([Decimal(it['price']) for it in items]) payment_arguments = { 'intent': 'sale', 'payer': { 'payment_method': 'paypal' }, 'redirect_urls': { 'return_url': absolute_uri(reverse('shopie:paypal_payment', kwargs={ 'order_key': order.order_key })), 'cancel_url': absolute_uri(reverse('shopie:checkout')) }, 'transactions': [{ 'item_list': { 'items': items }, 'amount': { 'total': moneyfmt(amount_total), 'currency': 'USD' }, 'description': 'Make sure to include' }] } payment = PaypalPaymentSDK(payment_arguments, api=shopiepaypal) if payment.create(): for link in payment.links: if link.method == 'REDIRECT': return str(link.href) else: raise PaymentProcessingError( "There was an error contacting the payment processor" )