def refund(self): if self.payment.state_actual.state == 'PA': if self.is_manual_payment(): pass if self.is_paypal_payment(): from payments.gateways.paypal import PayPalGateway from payments.models import PayPalShopSettings, PayPalTransaction paypal_gw = PayPalGateway(username=settings.PAYPAL_USERNAME, password=settings.PAYPAL_PASSWORD, sign=settings.PAYPAL_SIGNATURE, debug=settings.PAYPAL_DEBUG) try: txn = PayPalTransaction.objects.filter(sell=self).get() paypal_gw.RefundTransaction( txn.transaction_id, 'Full', 'USD', self.total, "Programatic refund from shop admin") except PayPalTransaction.DoesNotExist: raise SellError( "PayPalTransaction not found. Refund can't be performed..." ) if self.is_google_checkout(): from payments.gateways.googlecheckout import GoogleCheckoutGateway, GoogleCheckoutOrder from payments.models import GoogleCheckoutShopSettings try: google_settings = GoogleCheckoutShopSettings.objects.filter( shop=self.shop).get() except GoogleCheckoutShopSettings.DoesNotExist: raise SellError( "Google Checkout Settings are disabled! Refund can't be performed" ) googlecheckout_gw = GoogleCheckoutGateway( google_settings.merchant_id, google_settings.merchant_key, debug=True) try: order = GoogleCheckoutOrder.objects.filter(sell=self).get() refund = googlecheckout_gw.refund_order( order.order_number, self.total, "Programatic refund from shop admin") except GoogleCheckoutOrder.DoesNotExist: raise SellError( "This sell it's not associated to any GoogleCheckoutOrder! Refund can't be performed" ) if self.is_braintree(): from payments.gateways.braintreegw import BraintreeGateway from payments.models import BrainTreeTransaction try: bt_txn = BrainTreeTransaction.objects.filter( sell=self).get() except BrainTreeTransaction.DoesNotExist: raise SellError( 'There is no Braintree transaction associated to this sell!' ) gw = BraintreeGateway(settings.MERCHANT_ID, settings.PUBLIC_KEY, settings.PRIVATE_KEY) refund = gw.refund_transaction(bt_txn.transaction_id) if not refund.is_success: message = "" if refund.transaction: code = refund.transaction.processor_response_code text = refund.transaction.processor_response_text message = "Refund Failed! %s.\[%s] %s" % ( refund.message, code, text) else: for error in refund.errors.deep_errors: txt = "attribute: %s, code: %s. %s" ( error.attribute, error.code, error.message) message += txt + "\n" raise SellError("Can't do refund. %s" % message) for item in self.sellitem_set.all(): # item.product.increase_qty(item.qty) item.product.activate() item.save() self.cancel = True self.save() self.payment.refunded() else: raise SellError( 'Can not refund this sell, your state is not paid.')