def success(request): from payments.gateways.paypal import PayPalGateway from payments.models import PayPalShopSettings, PayPalToken, PayPalTransaction from preferences.models import Preference from sell.templatetags.sell_tags import money_format cart = request.cart #### Verify Products Availability if not cart.is_available(): request.flash['message'] = 'Items not longer available: ' for item in cart.items_not_availables(): request.flash['message'] += item.product.title cart.remove_not_available_items() return HttpResponseRedirect(reverse('my_shopping')) if request.method == 'GET': payerid = request.GET.get('PayerID', None) token = request.GET.get('token', None) else: payerid = request.POST.get('PayerID', None) token = request.POST.get('token', None) if None in (token, payerid): request.flash['message'] = unicode(_("Payment failed, try other method.")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) shop = request.shop paypal_settings = PayPalShopSettings.objects.filter(shop = shop).get() try: paypaltoken = PayPalToken.objects.filter(token=token).get() except PayPalToken.DoesNotExist: request.flash['message'] = unicode(_("Payment failed, try other method.")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) if paypaltoken.confirmed == True: request.flash['message'] = unicode(_("Payment is already confirmed!")) request.flash['severity'] = "notice" return HttpResponseRedirect(reverse('my_shopping')) cart = paypaltoken.cart #currency = Preference.get_preference(shop).checkout_currency total_amount = "%0.2f" % cart.total_with_taxes() if request.method != 'POST': t = loader.get_template('payments/payment_paypal_confirm.html') c = RequestContext(request, { 'payerid': payerid, 'token': token, #'api_signature': settings.PAYPAL_SIGNATURE, #'api_user': settings.PAYPAL_USERNAME, #'api_password': settings.PAYPAL_PASSWORD }) block = (t.render(c)) param = {'total_amount': money_format(total_amount, shop), 'paypaltoken': paypaltoken, 'cart': cart, 'cancel_url': reverse('payments_cancel'), 'form_paypal_confirm': block, } return HttpResponse(my_render(request, param, 'payment_paypal_confirm')) action = request.POST.get('action', 'cancel').lower() if action == 'confirm': paypal_gw = PayPalGateway(username=settings.PAYPAL_USERNAME, password=settings.PAYPAL_PASSWORD, sign=settings.PAYPAL_SIGNATURE, debug=settings.PAYPAL_DEBUG) #return_url = request.build_absolute_uri(reverse("paypal_success")) #cancel_url = request.build_absolute_uri(reverse("paypal_cancel")) is_token_data = paypal_gw.GetExpressCheckoutDetails(paypaltoken.token, subject=paypal_settings.email) if not is_token_data: logging.critical("Error found when trying to do a GetExpressCheckoutDetails api call on Paypal. RESPONSE: %s" % paypal_gw.api_response) request.flash['message'] = unicode(_("Could not get transaction data from PayPal. Please contact admin to complete your purchase!")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) ack = paypal_gw.api_response['ACK'][0] if ack != "Success": logging.critical("Paypal Api Response Failure. RESPONSE: %s" % paypal_gw.api_response) request.flash['message'] = unicode(_("There was an error when trying to get data from PayPal. Please contact admin to complete your purchase!")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) try: amount = decimal.Decimal(paypal_gw.api_response['PAYMENTREQUEST_0_AMT'][0]) except KeyError: logging.critical("Fail when trying to read the payment amount. The API response don't have an AMT key. RESPONSE: %s" % paypal_gw.api_response) request.flash['message'] = unicode(_("We have found an error when trying to validate your purchase!")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) if amount != cart.total_with_taxes(): request.flash['message'] = unicode(_("You have authorized us to charge you just $%s, but you want buy $%s! Please contact admin if you think this is a mistake!" % (amount, cart.total_with_taxes()))) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) payment_request = { 'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale', 'PAYMENTREQUEST_0_AMT': "%0.2f" % cart.total_with_taxes(), #'PAYMENTREQUEST_0_TAXAMT': "%0.2f" % cart.taxes(), #'PAYMENTREQUEST_n_SHIPPINGAMT': "%0.2f" % cart.shipping_charge(), #'PAYMENTREQUEST_0_ITEMAMT': "%0.2f" % cart.total(), 'PAYMENTREQUEST_0_CURRENCYCODE': Preference.get_preference(shop).checkout_currency, 'PAYMENTREQUEST_0_NOTIFYURL': request.build_absolute_uri(reverse("payments_paypal_ipn")), 'SUBJECT': paypal_settings.email } success = paypal_gw.DoExpressCheckoutPayment(payment_request, paypaltoken.token, payerid) if success: #Close and clean the cart sell = cart.close("PayPal") #Set the sell payments as paid sell.payment.pay() paypaltoken.confirmed = True paypaltoken.save() # {'PAYMENTINFO_0_TRANSACTIONTYPE': 'expresscheckout', 'ACK': 'Success', 'PAYMENTINFO_0_PAYMENTTYPE': 'instant', 'PAYMENTINFO_0_REASONCODE': 'None', 'SHIPPINGOPTIONISDEFAULT': 'false', 'INSURANCEOPTIONSELECTED': 'false', 'CORRELATIONID': '8d20dfd3e3575', 'PAYMENTINFO_0_TAXAMT': '0.00', 'PAYMENTINFO_0_TRANSACTIONID': '6MH53467HE876651A', 'PAYMENTINFO_0_PENDINGREASON': 'None', 'PAYMENTINFO_0_AMT': '57.00', 'PAYMENTINFO_0_PROTECTIONELIGIBILITY': 'Ineligible', 'PAYMENTINFO_0_ERRORCODE': '0', 'TOKEN': 'EC-7MR99474WD5992801', 'VERSION': '63.0', 'SUCCESSPAGEREDIRECTREQUESTED': 'false', 'BUILD': '1482946', 'PAYMENTINFO_0_CURRENCYCODE': 'USD', 'PAYMENTINFO_0_FEEAMT': '1.95', 'TIMESTAMP': '2010-09-08T18:03:24Z', 'PAYMENTINFO_0_ACK': 'Success', 'PAYMENTINFO_0_ORDERTIME': '2010-09-08T18:03:23Z', 'PAYMENTINFO_0_PAYMENTSTATUS': 'Completed'} txn_id = paypal_gw.api_response['PAYMENTINFO_0_TRANSACTIONID'] transaction = PayPalTransaction() transaction.transaction_id = txn_id transaction.sell = sell transaction.save() return HttpResponseRedirect(reverse('payments_success')) else: request.flash['message'] = unicode(_("Payment Failed!")) request.flash['severity'] = "error" return HttpResponseRedirect(reverse('my_shopping')) else: paypaltoken.delete() request.flash['message'] = unicode(_("Payment cancel!")) request.flash['severity'] = "notice" return HttpResponseRedirect(reverse('my_shopping'))