def purchase(self, money, credit_card, options=None): """Using PAYPAL DoDirectPayment, charge the given credit card for specified money""" if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") params = {} params['creditcardtype'] = credit_card.card_type.card_name params['acct'] = credit_card.number params['expdate'] = '%02d%04d' % (credit_card.month, credit_card.year) params['cvv2'] = credit_card.verification_value params['ipaddress'] = options['request'].META.get("REMOTE_ADDR", "") params['amt'] = money if options.get("email"): params['email'] = options["email"] address = options.get("billing_address", {}) first_name = None last_name = None try: first_name, last_name = address.get("name", "").split(" ") except ValueError: pass params['firstname'] = first_name or credit_card.first_name params['lastname'] = last_name or credit_card.last_name params['street'] = address.get("address1", '') params['street2'] = address.get("address2", "") params['city'] = address.get("city", '') params['state'] = address.get("state", '') params['countrycode'] = address.get("country", '') params['zip'] = address.get("zip", '') params['phone'] = address.get("phone", "") shipping_address = options.get("shipping_address", None) if shipping_address: params['shiptoname'] = shipping_address["name"] params['shiptostreet'] = shipping_address["address1"] params['shiptostreet2'] = shipping_address.get("address2", "") params['shiptocity'] = shipping_address["city"] params['shiptostate'] = shipping_address["state"] params['shiptocountry'] = shipping_address["country"] params['shiptozip'] = shipping_address["zip"] params['shiptophonenum'] = shipping_address.get("phone", "") wpp = PayPalWPP(options['request']) try: response = wpp.doDirectPayment(params) transaction_was_successful.send(sender=self, type="purchase", response=response) except PayPalFailure, e: transaction_was_unsuccessful.send(sender=self, type="purchase", response=e) # Slight skewness because of the way django-paypal # is implemented. return {"status": "FAILURE", "response": e}
def process(self, request, item): """Process a PayPal direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) params = self.cleaned_data params['creditcardtype'] = self.fields['acct'].card_type params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y") try: params['ipaddress'] = request.META["REMOTE_ADDR"] except KeyError: params['ipaddress'] = request.META.get("HTTP_X_FORWARDED_FOR", "0:0:0:0:0:0") params.update(item) try: # Create single payment: if 'billingperiod' not in params: nvp_obj = wpp.doDirectPayment(params) # Create recurring payment: else: nvp_obj = wpp.createRecurringPaymentsProfile(params, direct=True) except PayPalFailure: return False return True
def process(self, request, item): """Process a PayPal direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) params = self.cleaned_data params['creditcardtype'] = self.fields['acct'].card_type params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y") params['ipaddress'] = request.META.get("REMOTE_ADDR", "") params.update(item) try: # Create single payment: if 'billingperiod' not in params: nvp_obj = wpp.doDirectPayment(params) # Create recurring payment: else: if 'profileid' in params: # Updating a payment profile. nvp_obj = wpp.updateRecurringPaymentsProfile(params, direct=True) else: # Creating a payment profile. nvp_obj = wpp.createRecurringPaymentsProfile(params, direct=True) except PayPalFailure: return False return True
def test_doDirectPayment_authenticated_user(self): User = get_user_model() user = User.objects.create(username='******') wpp = PayPalWPP(make_request(user=user)) data = self.get_valid_doDirectPayment_data() data.update(self.item) npm_obj = wpp.doDirectPayment(data) self.assertEqual(npm_obj.user, user)
def save(request): """ Direct Paypal payment with credit card """ try: cart = get_cart_data_from_request(request) if 'order' not in request.GET: raise Exception('no order data') order_json = request.GET['order'] order_json = urllib.unquote(order_json) order = json.loads(order_json) wpp = PayPalWPP() paypal_params = { 'ipaddress': request.META.get('REMOTE_ADDR'), 'creditcardtype': order['creditcardtype'], 'acct': order['acct'], 'expdate': order['cardmonth'] + order['cardyear'], 'cvv2': order['cvv2'], 'email': order['email'], 'firstname': order['card_first_name'], 'lastname': order['card_last_name'], 'street': order['b_street'], 'city': order['b_city'], 'state': order['b_state'], 'countrycode': order['b_countrycode'], 'zip': order['b_zip'], 'amt': cart['paymentrequest_0_amt'], } nvp = wpp.doDirectPayment(paypal_params) order_data = nvp.response_dict.copy() order_data.update(cart) order_data.update(order) order = create_order_from_direct_paypal(order_data) data = { 'order_id': order.id, } send_order_email(order.email, order, order.items.all) return HttpResponse(json.dumps(data), content_type='application/json') except Exception, e: print e data = { 'error': e.message } return HttpResponseServerError(json.dumps(data), content_type='application/json')
def test_doDirectPayment_valid(self): wpp = PayPalWPP(make_request()) data = self.get_valid_doDirectPayment_data() data.update(self.item) nvp = wpp.doDirectPayment(data) self.assertIsNotNone(nvp) for k, v in [('avscode', 'X'), ('amt', '9.95'), ('correlationid', '1025431f33d89'), ('currencycode', 'USD'), ('ack', 'Success')]: self.assertEqual(nvp.response_dict[k], v)
def save(request): """ Direct Paypal payment with credit card """ try: cart = get_cart_data_from_request(request) if 'order' not in request.GET: raise Exception('no order data') order_json = request.GET['order'] order_json = urllib.unquote(order_json) order = json.loads(order_json) wpp = PayPalWPP() paypal_params = { 'ipaddress': request.META.get('REMOTE_ADDR'), 'creditcardtype': order['creditcardtype'], 'acct': order['acct'], 'expdate': order['cardmonth'] + order['cardyear'], 'cvv2': order['cvv2'], 'email': order['email'], 'firstname': order['card_first_name'], 'lastname': order['card_last_name'], 'street': order['b_street'], 'city': order['b_city'], 'state': order['b_state'], 'countrycode': order['b_countrycode'], 'zip': order['b_zip'], 'amt': cart['paymentrequest_0_amt'], } nvp = wpp.doDirectPayment(paypal_params) order_data = nvp.response_dict.copy() order_data.update(cart) order_data.update(order) order = create_order_from_direct_paypal(order_data) data = { 'order_id': order.id, } send_order_email(order.email, order, order.items.all) return HttpResponse(json.dumps(data), content_type='application/json') except Exception, e: print e data = {'error': e.message} return HttpResponseServerError(json.dumps(data), content_type='application/json')
def process(self, request, item): """Process a PayPal direct payment.""" warn_untested() from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) params = self.cleaned_data params["creditcardtype"] = self.fields["acct"].card_type params["expdate"] = self.cleaned_data["expdate"].strftime("%m%Y") params["ipaddress"] = request.META.get("REMOTE_ADDR", "") params.update(item) try: # Create single payment: if "billingperiod" not in params: wpp.doDirectPayment(params) # Create recurring payment: else: wpp.createRecurringPaymentsProfile(params, direct=True) except PayPalFailure: return False return True
def test_doDirectPayment_valid(self): wpp = PayPalWPP(make_request()) data = self.get_valid_doDirectPayment_data() data.update(self.item) nvp = wpp.doDirectPayment(data) self.assertIsNotNone(nvp) for k, v in [ ("avscode", "X"), ("amt", "9.95"), ("correlationid", "1025431f33d89"), ("currencycode", "USD"), ("ack", "Success"), ]: self.assertEqual(nvp.response_dict[k], v)
def test_doDirectPayment_valid_with_signal(self): wpp = PayPalWPP(make_request()) data = self.get_valid_doDirectPayment_data() data.update(self.item) self.got_signal = False self.signal_obj = None def handle_signal(sender, **kwargs): self.got_signal = True self.signal_obj = sender payment_was_successful.connect(handle_signal) self.assertTrue(wpp.doDirectPayment(data)) self.assertTrue(self.got_signal)
def process(self, request, item): """Process a PayPal direct payment.""" warn_untested() from paypal.pro.helpers import PayPalWPP from django.conf import settings import pdb; pdb.set_trace() wpp = PayPalWPP(request) params = self.cleaned_data params['creditcardtype'] = self.fields['acct'].card_type params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y") # params['ipaddress'] = 'https://%s' % settings.ALLOWED_HOSTS[0] params['ipaddress'] = request.META.get("REMOTE_ADDR", settings.ALLOWED_HOSTS[0]) params.update(item) try: # Create single payment: if 'billingperiod' not in params: wpp.doDirectPayment(params) # Create recurring payment: else: wpp.createRecurringPaymentsProfile(params, direct=True) except PayPalFailure: return False return True
def process(self, request, item): """Do a direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) # Change the model information into a dict that PayPal can understand. params = model_to_dict(self, exclude=self.ADMIN_FIELDS) params['acct'] = self.acct params['creditcardtype'] = self.creditcardtype params['expdate'] = self.expdate params['cvv2'] = self.cvv2 params.update(item) # Create recurring payment: if 'billingperiod' in params: return wpp.createRecurringPaymentsProfile(params, direct=True) # Create single payment: else: return wpp.doDirectPayment(params)
def process(self, request, item): """Process a PayPal direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) params = self.cleaned_data params['creditcardtype'] = self.fields['acct'].card_type params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y") params['ipaddress'] = request.META.get("REMOTE_ADDR", "") params.update(item) # Create single payment: if 'billingperiod' not in params: response = wpp.doDirectPayment(params) # Create recurring payment: else: response = wpp.createRecurringPaymentsProfile(params, direct=True) return response
def process(self, request, item): """Do a direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) # Change the model information into a dict that PayPal can understand. params = model_to_dict(self, exclude=self.ADMIN_FIELDS) params["ACCT"] = self.acct params["CREDITCARDTYPE"] = self.creditcardtype params["EXPDATE"] = self.expdate params["CVV2"] = self.cvv2 params.update(item) # Create recurring payment: if "BILLINGPERIOD" in params: return wpp.createRecurringPaymentsProfile(params, direct=True) # Create single payment: else: return wpp.doDirectPayment(params)
def process(self, ipaddress, user, item): """Process a PayPal direct payment.""" from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(ipaddress, user) params = self.cleaned_data params['creditcardtype'] = self.fields['acct'].card_type params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y") params['ipaddress'] = ipaddress params.update(item) try: # Create single payment: if 'billingperiod' not in params: nvp_obj = wpp.doDirectPayment(params) # Create recurring payment: else: nvp_obj = wpp.createRecurringPaymentsProfile(params, direct=True) except PayPalFailure: return None return nvp_obj
def process(self, request, item): """ Do a direct payment. """ from paypal.pro.helpers import PayPalWPP wpp = PayPalWPP(request) params = self.cleaned_data params["creditcardtype"] = self.fields["acct"].card_type params["expdate"] = self.cleaned_data["expdate"].strftime("%m%Y") params["ipaddress"] = request.META.get("REMOTE_ADDR", "") params.update(item) # Create single payment: if "billingperiod" not in params: response = wpp.doDirectPayment(params) # Create recurring payment: else: response = wpp.createRecurringPaymentsProfile(params, direct=True) return response