def test_qs_passed(self, _call): data = self.data.copy() data['qs'] = {'foo': 'bar'} _call.return_value = {'payKey': '123', 'paymentExecStatus': ''} paypal.get_paykey(data) qs = _call.call_args[0][1]['returnUrl'].split('?')[1] eq_(dict(urlparse.parse_qsl(qs))['foo'], 'bar')
def test_qs_passed(self, _call): data = self.data.copy() data['qs'] = {'foo': 'bar'} _call.return_value = {'payKey': '123', 'paymentExecStatus': ''} paypal.get_paykey(data) qs = _call.call_args[0][1]['returnUrl'].split('?')[1] assert dict(urlparse.parse_qsl(qs))['foo'] == 'bar'
def test_error_raised(self, opener): opener.return_value.text = other_error.replace('520001', '589023') try: paypal.get_paykey(self.data) except paypal.PaypalError as error: eq_(error.id, '589023') assert 'The amount is too small' in str(error) else: raise ValueError('No PaypalError was raised')
def test_error_no_currency(self, opener): opener.return_value.text = other_error.replace('520001', '559044') try: data = self.data.copy() paypal.get_paykey(data) except paypal.PaypalError as error: eq_(error.id, '559044') else: raise ValueError('No PaypalError was raised')
def test_error_no_currency(self, opener): opener.return_value.text = other_error.replace('520001', '559044') try: data = self.data.copy() paypal.get_paykey(data) except paypal.PaypalError as error: assert error.id == '559044' else: raise ValueError('No PaypalError was raised')
def test_error_raised(self, opener): opener.return_value.text = other_error.replace('520001', '589023') try: paypal.get_paykey(self.data) except paypal.PaypalError as error: assert error.id == '589023' assert 'The amount is too small' in str(error) else: raise ValueError('No PaypalError was raised')
def test_error_one_currency(self, opener): opener.return_value.text = other_error.replace('520001', '559044') try: data = self.data.copy() data['currency'] = 'BRL' paypal.get_paykey(data) except paypal.PaypalError as error: eq_(error.id, '559044') assert 'Real' in str(error), str(error) else: raise ValueError('No PaypalError was raised')
def test_error_one_currency(self, opener): opener.return_value.text = other_error.replace('520001', '559044') try: data = self.data.copy() data['currency'] = 'BRL' paypal.get_paykey(data) except paypal.PaypalError as error: assert error.id == '559044' assert 'Real' in str(error), str(error) else: raise ValueError('No PaypalError was raised')
def test_get_key(self, post_mock): post_mock.return_value.text = good_response assert paypal.get_paykey(self.data) == ('AP-9GD76073HJ780401K', 'CREATED') assert post_mock.call_count == 1 assert post_mock.call_args[0][0] == '%s%s' % (settings.PAYPAL_PAY_URL, 'Pay') data = dict(urlparse.parse_qsl(post_mock.call_args[1]['data'])) expected_data = { u'actionType': u'PAY', u'cancelUrl': (u'%s/en-US/firefox/addon/xx/contribute/cancel?uuid=%s' % (settings.SITE_URL, self.data['uuid'])), u'currencyCode': 'USD', u'ipnNotificationUrl': u'%s/services/paypal' % settings.SITE_URL, u'receiverList.receiver(0).amount': u'10', u'receiverList.receiver(0).email': u'*****@*****.**', u'receiverList.receiver(0).invoiceID': u'mozilla-%s' % self.data['uuid'], u'receiverList.receiver(0).paymentType': u'DIGITALGOODS', u'requestEnvelope.errorLanguage': u'US', 'returnUrl': (u'%s/en-US/firefox/addon/xx/contribute/complete?uuid=%s' % (settings.SITE_URL, self.data['uuid'])), u'trackingId': self.data['uuid'], } assert data == expected_data
def test_get_key(self, post_mock): post_mock.return_value.text = good_response assert paypal.get_paykey(self.data) == ( 'AP-9GD76073HJ780401K', 'CREATED') assert post_mock.call_count == 1 assert post_mock.call_args[0][0] == '%s%s' % ( settings.PAYPAL_PAY_URL, 'Pay') data = dict(urlparse.parse_qsl(post_mock.call_args[1]['data'])) expected_data = { u'actionType': u'PAY', u'cancelUrl': ( u'%s/en-US/firefox/addon/xx/contribute/cancel?uuid=%s' % (settings.SITE_URL, self.data['uuid'])), u'currencyCode': 'USD', u'ipnNotificationUrl': u'%s/services/paypal' % settings.SITE_URL, u'receiverList.receiver(0).amount': u'10', u'receiverList.receiver(0).email': u'*****@*****.**', u'receiverList.receiver(0).invoiceID': u'mozilla-%s' % self.data['uuid'], u'receiverList.receiver(0).paymentType': u'DIGITALGOODS', u'requestEnvelope.errorLanguage': u'US', 'returnUrl': ( u'%s/en-US/firefox/addon/xx/contribute/complete?uuid=%s' % (settings.SITE_URL, self.data['uuid'])), u'trackingId': self.data['uuid'], } assert data == expected_data
def contribute(request, addon): # Enforce paypal-imposed comment length limit commentlimit = PAYPAL_MAX_COMMENT_LENGTH contrib_type = request.POST.get('type', 'suggested') is_suggested = contrib_type == 'suggested' source = request.POST.get('source', '') comment = request.POST.get('comment', '') amount = { 'suggested': addon.suggested_amount, 'onetime': request.POST.get('onetime-amount', '') }.get(contrib_type, '') if not amount: amount = settings.DEFAULT_SUGGESTED_CONTRIBUTION form = ContributionForm({'amount': amount}) if len(comment) > commentlimit or not form.is_valid(): return http.HttpResponse(json.dumps({ 'error': 'Invalid data.', 'status': '', 'url': '', 'paykey': '' }), content_type='application/json') contribution_uuid = hashlib.sha256(str(uuid.uuid4())).hexdigest() if addon.charity: # TODO(andym): Figure out how to get this in the addon authors # locale, rather than the contributors locale. name, paypal_id = (u'%s: %s' % (addon.name, addon.charity.name), addon.charity.paypal) else: name, paypal_id = addon.name, addon.paypal_id # l10n: {0} is the addon name contrib_for = _(u'Contribution for {0}').format(jinja2.escape(name)) paykey, error, status = '', '', '' try: paykey, status = paypal.get_paykey( dict(amount=amount, email=paypal_id, ip=request.META.get('REMOTE_ADDR'), memo=contrib_for, pattern='addons.paypal', slug=addon.slug, uuid=contribution_uuid)) except paypal.PaypalError as error: log.error('Error getting paykey, contribution for addon ' '(addon: %s, contribution: %s)' % (addon.pk, contribution_uuid), exc_info=True) if paykey: contrib = Contribution(addon_id=addon.id, charity_id=addon.charity_id, amount=amount, source=source, source_locale=request.LANG, annoying=addon.annoying, uuid=str(contribution_uuid), is_suggested=is_suggested, suggested_amount=addon.suggested_amount, comment=comment, paykey=paykey) contrib.save() url = '%s?paykey=%s' % (settings.PAYPAL_FLOW_URL, paykey) if request.GET.get('result_type') == 'json' or request.is_ajax(): # If there was an error getting the paykey, then JSON will # not have a paykey and the JS can cope appropriately. return http.HttpResponse(json.dumps({ 'url': url, 'paykey': paykey, 'error': str(error), 'status': status }), content_type='application/json') return http.HttpResponseRedirect(url)
def contribute(request, addon): commentlimit = 255 # Enforce paypal-imposed comment length limit contrib_type = request.POST.get('type', 'suggested') is_suggested = contrib_type == 'suggested' source = request.POST.get('source', '') comment = request.POST.get('comment', '') amount = { 'suggested': addon.suggested_amount, 'onetime': request.POST.get('onetime-amount', '') }.get(contrib_type, '') if not amount: amount = settings.DEFAULT_SUGGESTED_CONTRIBUTION form = ContributionForm({'amount': amount}) if len(comment) > commentlimit or not form.is_valid(): return http.HttpResponse(json.dumps({'error': 'Invalid data.', 'status': '', 'url': '', 'paykey': ''}), content_type='application/json') contribution_uuid = hashlib.md5(str(uuid.uuid4())).hexdigest() if addon.charity: # TODO(andym): Figure out how to get this in the addon authors # locale, rather than the contributors locale. name, paypal_id = (u'%s: %s' % (addon.name, addon.charity.name), addon.charity.paypal) else: name, paypal_id = addon.name, addon.paypal_id # l10n: {0} is the addon name contrib_for = _(u'Contribution for {0}').format(jinja2.escape(name)) paykey, error, status = '', '', '' try: paykey, status = paypal.get_paykey( dict(amount=amount, email=paypal_id, ip=request.META.get('REMOTE_ADDR'), memo=contrib_for, pattern='addons.paypal', slug=addon.slug, uuid=contribution_uuid)) except paypal.PaypalError as error: log.error( 'Error getting paykey, contribution for addon ' '(addon: %s, contribution: %s)' % (addon.pk, contribution_uuid), exc_info=True) if paykey: contrib = Contribution(addon_id=addon.id, charity_id=addon.charity_id, amount=amount, source=source, source_locale=request.LANG, annoying=addon.annoying, uuid=str(contribution_uuid), is_suggested=is_suggested, suggested_amount=addon.suggested_amount, comment=comment, paykey=paykey) contrib.save() url = '%s?paykey=%s' % (settings.PAYPAL_FLOW_URL, paykey) if request.GET.get('result_type') == 'json' or request.is_ajax(): # If there was an error getting the paykey, then JSON will # not have a paykey and the JS can cope appropriately. return http.HttpResponse(json.dumps({'url': url, 'paykey': paykey, 'error': str(error), 'status': status}), content_type='application/json') return http.HttpResponseRedirect(url)
def _test_no_mock(self): # Remove _ and run if you'd like to try unmocked. data = self.data.copy() data['email'] = '*****@*****.**' return paypal.get_paykey(data)
def test_other_currency(self, _call): _call.return_value = {'payKey': '', 'paymentExecStatus': ''} data = self.data.copy() data['currency'] = 'EUR' paypal.get_paykey(data) assert _call.call_args[0][1]['currencyCode'] == 'EUR'
def _test_check_purchase_no_mock(self): # Remove _ and run if you'd like to try this unmocked. key = paypal.get_paykey(self.data) assert paypal.check_purchase(key) == 'CREATED'
def test_usd_default(self, _call): _call.return_value = {'payKey': '', 'paymentExecStatus': ''} paypal.get_paykey(self.data) assert _call.call_args[0][1]['currencyCode'] == 'USD'
def _test_check_purchase_no_mock(self): # Remove _ and run if you'd like to try this unmocked. key = paypal.get_paykey(self.data) eq_(paypal.check_purchase(key), 'CREATED')
def test_usd_default(self, _call): _call.return_value = {'payKey': '', 'paymentExecStatus': ''} paypal.get_paykey(self.data) eq_(_call.call_args[0][1]['currencyCode'], 'USD')
def test_other_currency(self, _call): _call.return_value = {'payKey': '', 'paymentExecStatus': ''} data = self.data.copy() data['currency'] = 'EUR' paypal.get_paykey(data) eq_(_call.call_args[0][1]['currencyCode'], 'EUR')
def test_get_key(self, opener): opener.return_value.text = good_response eq_(paypal.get_paykey(self.data), ('AP-9GD76073HJ780401K', 'CREATED'))