Skip to content

Instantly share code, notes, and snippets.

@slav0nic
Created September 5, 2016 12:03
Show Gist options
  • Save slav0nic/1ba03a3dce91f5880e6473fbdfe5328d to your computer and use it in GitHub Desktop.
Save slav0nic/1ba03a3dce91f5880e6473fbdfe5328d to your computer and use it in GitHub Desktop.
Django oscar authoprize.net backend
from django.db import transaction
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import DeleteView
from oscar.core.loading import get_model
from apps.oscar_authorize.facade import Facade
facade = Facade()
Order = get_model('order', 'Order')
Bankcard = get_model('payment', 'Bankcard')
class DeleteBankCardView(DeleteView):
template_name = 'customer/profile/bankcard_delete.html'
def get_queryset(self):
return Bankcard.objects.filter(user=self.request.user)
@transaction.atomic
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
facade.delete_credit_card(self.request.user, self.object.partner_reference)
return HttpResponseRedirect(success_url)
def get_success_url(self):
return reverse('customer:summary')
import authorize # py-authorize==1.2.4
from django.conf import settings
from django.utils.translation import ugettext as _
from oscar.core.loading import get_model
from oscar.apps.payment.exceptions import UnableToTakePayment
from .models import AuthorizeTransaction
Bankcard = get_model('payment', 'Bankcard')
class Facade(object):
def __init__(self):
authorize.Configuration.configure(
authorize.Environment.TEST if getattr(settings, 'AUTHORIZE_SANDBOX_MODE', True) else authorize.Environment.Production ,
settings.AUTHORIZE_LOGIN_ID,
settings.AUTHORIZE_TRANSACTION_KEY,
)
def sale(self, order_number, amount, bankcard, billing_address=None):
address_data = {}
if billing_address:
address_data['billing'] = {
'first_name': billing_address['first_name'],
'last_name': billing_address['last_name'],
'address': billing_address['line1'],
'city': billing_address['line4'],
'state': billing_address['state'],
'zip': billing_address['postcode'],
'country': billing_address['country'].code,
#'phone_number': billing_address['billing_phone_number'],
}
try:
response = authorize.Transaction.sale({
'amount': amount,
'credit_card': {
'card_number': bankcard.number,
'card_code': bankcard.cvv,
'expiration_date': bankcard.expiry_month("%m/%Y"),
},
'billing': address_data['billing']
})
except authorize.AuthorizeResponseError as e:
response = e.full_response
raise UnableToTakePayment(_("Your payment can't be completed."))
finally:
self.record_txn(order_number, amount, response)
return response
def sale_cim(self, order_number, amount, customer_id, payment_id):
"""
Run sale transaction with CIM data (customer id and bank card payment id)
"""
try:
response = authorize.Transaction.sale({
'amount': amount,
'customer_id': customer_id,
'payment_id': payment_id,
})
except authorize.AuthorizeResponseError as e:
response = e.full_response
raise UnableToTakePayment(_("Your payment can't be completed."))
finally:
self.record_txn(order_number, amount, response)
return response
def record_txn(self, order_number, amount, response):
# TODO: currency
AuthorizeTransaction.objects.create(
order_number=order_number,
amount=amount,
response_code=response.transaction_response.get('response_code', ''),
auth_code=response.transaction_response.get('auth_code', ''),
trans_id=response.transaction_response.get('trans_id', ''),
account_number=response.transaction_response.get('account_number', ''),
account_type=response.transaction_response.get('account_type', ''),
trans_hash=response.transaction_response.get('trans_hash', ''),
avs_result_code=response.transaction_response.get('avs_result_code', ''),
cavv_result_code=response.transaction_response.get('cavv_result_code', ''),
cvv_result_code=response.transaction_response.get('cvv_result_code', ''),
)
def create_customer(self, user):
result = authorize.Customer.create({
'email': user.email,
'description': '[{}] {}'.format(user.id, user.get_full_name()),
'customer_type': 'individual',
})
user.customer_id = result.customer_id
user.save(update_fields=['customer_id'])
return user
def save_credit_card(self, user, bankcard):
if user.customer_id is None:
user = self.create_customer(user)
try:
result = authorize.CreditCard.create(str(user.customer_id), {
'card_number': bankcard.number,
'expiration_date': bankcard.expiry_month("%m/%Y"),
'card_code': bankcard.cvv,
#'billing': {
#'first_name': 'Rob',
#'last_name': 'Oteron',
#'company': 'Robotron Studios',
#'address': '101 Computer Street',
#'city': 'Tucson',
#'state': 'AZ',
#'zip': '85704',
#'country': 'US',
#'phone_number': '520-123-4567',
#'fax_number': '520-456-7890',
#},
})
except authorize.AuthorizeResponseError:
# Ignoring errors like "A duplicate customer payment profile already exists"
pass
else:
bankcard.user = user
bankcard.partner_reference = result.payment_id
bankcard.save()
return bankcard
def get_credit_cards(self, user):
return Bankcard.objects.filter(user=user)
def delete_credit_card(self, user, payment_id):
try:
authorize.CreditCard.delete(str(user.customer_id), payment_id)
except authorize.AuthorizeResponseError:
pass
Bankcard.objects.filter(user=user, partner_reference=payment_id).delete()
from django.db import models
class AuthorizeTransaction(models.Model):
order_number = models.CharField(max_length=128, db_index=True)
amount = models.DecimalField(max_digits=12, decimal_places=2, null=True,
blank=True)
response_code = models.CharField(max_length=2)
auth_code = models.CharField(max_length=10, null=True, blank=True)
trans_id = models.CharField(max_length=255, db_index=True)
trans_hash = models.CharField(max_length=32)
account_number = models.CharField(max_length=10, default="", blank=True)
account_type = models.CharField(max_length=10, default="", blank=True)
date_created = models.DateTimeField(auto_now_add=True)
avs_result_code = models.CharField(max_length=10, null=True, blank=True)
cavv_result_code = models.CharField(max_length=2, null=True, blank=True)
cvv_result_code = models.CharField(max_length=2, null=True, blank=True)
class Meta:
ordering = ('-date_created',)
@property
def is_approved(self):
return self.response_code == '1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment