Exemple #1
0
def charge_transaction(user, transaction_id, campaign_id):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if bid.is_charged():
        return True, None

    if transaction_id < 0:
        bid.charged()
        return True, None

    profile_id = CustomerID.get_id(user._id)

    try:
        api.capture_authorization_hold(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=bid.bid,
            transaction_id=transaction_id,
        )
    except api.AuthorizationHoldNotFound:
        # authorization hold has expired
        bid.void()
        return False, api.TRANSACTION_NOT_FOUND
    except api.TransactionError as e:
        return False, e.message

    bid.charged()
    return True, None
Exemple #2
0
def add_payment_method(user, address, credit_card, validate=False):
    profile_id = CustomerID.get_id(user._id)
    payment_method_id = api.create_payment_profile(profile_id, address, credit_card, validate)

    if payment_method_id:
        PayID.add(user, payment_method_id)
        return payment_method_id
Exemple #3
0
def auth_transaction(amount, user, payment_method_id, link, campaign_id):
    if payment_method_id not in PayID.get_ids(user._id):
        return None, "invalid payment method"

    profile_id = CustomerID.get_id(user._id)
    invoice = "T%dC%d" % (link._id, campaign_id)

    try:
        transaction_id = api.create_authorization_hold(profile_id,
                                                       payment_method_id,
                                                       amount, invoice,
                                                       request.ip)
    except api.DuplicateTransactionError as e:
        transaction_id = e.transaction_id
        try:
            bid = Bid.one(transaction_id, campaign=campaign_id)
        except NotFound:
            bid = Bid._new(transaction_id, user, payment_method_id, link._id,
                           amount, campaign_id)
        g.log.error("%s on campaign %d" % (e.message, campaign_id))
        return transaction_id, None
    except api.TransactionError as e:
        return None, e.message

    bid = Bid._new(transaction_id, user, payment_method_id, link._id, amount,
                   campaign_id)
    return transaction_id, None
Exemple #4
0
def charge_transaction(user, campaign_id, link_id, transaction_id, references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if bid.is_charged():
        return True, None

    if transaction_id < 0:
        bid.charged()
        return True, None

    references['pay_id'] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.capture_authorization_hold(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=bid.bid,
            transaction_id=transaction_id,
        )
        AuthorizeTransaction._new(authorize_response, **references)
    except api.AuthorizationHoldNotFound:
        # authorization hold has expired
        bid.void()
        return False, api.TRANSACTION_NOT_FOUND
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.charged()
    return True, None
Exemple #5
0
def auth_transaction(user, campaign_id, link_id, amount, payment_method_id,
                     references):
    if payment_method_id not in PayID.get_ids(user._id):
        return None, "invalid payment method"

    profile_id = CustomerID.get_id(user._id)
    invoice = "T%dC%d" % (link_id, campaign_id)

    references['pay_id'] = payment_method_id

    try:
        authorize_response = api.create_authorization_hold(
            profile_id, payment_method_id, amount, invoice, request.ip)
        AuthorizeTransaction._new(authorize_response, **references)
    except api.DuplicateTransactionError as e:
        transaction_id = e.transaction_id
        try:
            bid = Bid.one(transaction_id, campaign=campaign_id)
        except NotFound:
            bid = Bid._new(transaction_id, user, payment_method_id, link_id,
                           amount, campaign_id)
        g.log.error("%s on campaign %d" % (e.message, campaign_id))
        return transaction_id, None
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return None, e.message

    bid = Bid._new(authorize_response.trans_id, user, payment_method_id,
                   link_id, amount, campaign_id)
    return authorize_response.trans_id, None
Exemple #6
0
def charge_transaction(user, campaign_id, link_id, transaction_id, references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if bid.is_charged():
        return True, None

    if transaction_id < 0:
        bid.charged()
        return True, None

    references["pay_id"] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.capture_authorization_hold(
            customer_id=profile_id, payment_profile_id=bid.pay_id, amount=bid.bid, transaction_id=transaction_id
        )
        AuthorizeTransaction._new(authorize_response, **references)
    except api.AuthorizationHoldNotFound:
        # authorization hold has expired
        bid.void()
        return False, api.TRANSACTION_NOT_FOUND
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.charged()
    return True, None
Exemple #7
0
def refund_transaction(user, campaign_id, link_id, amount, transaction_id,
                       references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if transaction_id < 0:
        bid.refund(amount)
        return True, None

    references['pay_id'] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.refund_transaction(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=amount,
            transaction_id=transaction_id,
        )
        AuthorizeTransaction._new(authorize_response, **references)
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.refund(amount)
    return True, None
Exemple #8
0
def _make_transaction(trans_cls, amount, user, pay_id,
                      order=None, trans_id=None, test=None):
    """
    private function for handling transactions (since the data is
    effectively the same regardless of trans_cls)
    """
    # format the amount
    if amount:
        amount = "%.2f" % amount
    # lookup customer ID
    cust_id = CustomerID.get_id(user)
    # create a new transaction
    trans = trans_cls(amount, cust_id, pay_id, trans_id=trans_id,
                      order=order)
    extra = {}
    # the optional test field makes the transaction a test, and will
    # make the response be the error code corresponding to int(test).
    if isinstance(test, int):
        extra = dict(x_test_request="TRUE",
                     x_card_num=test_card.ERRORCARD.cardNumber,
                     x_amount=test)

    # using the transaction, generate a transaction request and make it
    req = CreateCustomerProfileTransactionRequest(transaction=trans,
                                                  extraOptions=extra)
    return req.make_request()
Exemple #9
0
def auth_transaction(user, campaign_id, link_id, amount, payment_method_id, references):
    if payment_method_id not in PayID.get_ids(user._id):
        return None, "invalid payment method"

    profile_id = CustomerID.get_id(user._id)
    invoice = "T%dC%d" % (link_id, campaign_id)

    references["pay_id"] = payment_method_id

    try:
        authorize_response = api.create_authorization_hold(profile_id, payment_method_id, amount, invoice, request.ip)
        AuthorizeTransaction._new(authorize_response, **references)
    except api.DuplicateTransactionError as e:
        transaction_id = e.transaction_id
        try:
            bid = Bid.one(transaction_id, campaign=campaign_id)
        except NotFound:
            bid = Bid._new(transaction_id, user, payment_method_id, link_id, amount, campaign_id)
        g.log.error("%s on campaign %d" % (e.message, campaign_id))
        return transaction_id, None
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return None, e.message

    bid = Bid._new(authorize_response.trans_id, user, payment_method_id, link_id, amount, campaign_id)
    return authorize_response.trans_id, None
Exemple #10
0
def charge_transaction(user, transaction_id, campaign_id):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if bid.is_charged():
        return True, None

    if transaction_id < 0:
        bid.charged()
        return True, None

    profile_id = CustomerID.get_id(user._id)

    try:
        api.capture_authorization_hold(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=bid.bid,
            transaction_id=transaction_id,
        )
    except api.AuthorizationHoldNotFound:
        # authorization hold has expired
        bid.void()
        return False, api.TRANSACTION_NOT_FOUND
    except api.TransactionError as e:
        return False, e.message

    bid.charged()
    return True, None
Exemple #11
0
def auth_transaction(amount, user, payment_method_id, link, campaign_id):
    if payment_method_id not in PayID.get_ids(user._id):
        return None, "invalid payment method"

    profile_id = CustomerID.get_id(user._id)
    invoice = "T%dC%d" % (link._id, campaign_id)

    try:
        transaction_id = api.create_authorization_hold(
            profile_id, payment_method_id, amount, invoice, request.ip)
    except api.DuplicateTransactionError as e:
        transaction_id = e.transaction_id
        try:
            bid = Bid.one(transaction_id, campaign=campaign_id)
        except NotFound:
            bid = Bid._new(transaction_id, user, payment_method_id, link._id,
                           amount, campaign_id)
        g.log.error("%s on campaign %d" % (e.message, campaign_id))
        return transaction_id, None
    except api.TransactionError as e:
        return None, e.message

    bid = Bid._new(transaction_id, user, payment_method_id, link._id, amount,
                   campaign_id)
    return transaction_id, None
Exemple #12
0
 def __init__(self, user, paymentProfiles, address,
              validationMode = None):
     SimpleXMLObject.__init__(self, merchantCustomerId = user._fullname,
                              description = user.name, email = "",
                              paymentProfiles = paymentProfiles,
                              shipToList = address,
                              validationMode = validationMode,
                              customerProfileId=CustomerID.get_id(user))
Exemple #13
0
 def __init__(self, user, **kw):
     if isinstance(user, int):
         cust_id = user
         self._user = None
     else:
         cust_id = CustomerID.get_id(user)
         self._user = user
     AuthorizeNetRequest.__init__(self, customerProfileId=cust_id, **kw)
Exemple #14
0
def add_payment_method(user, address, credit_card, validate=False):
    profile_id = CustomerID.get_id(user._id)
    payment_method_id = api.create_payment_profile(profile_id, address,
                                                   credit_card, validate)

    if payment_method_id:
        PayID.add(user, payment_method_id)
        return payment_method_id
Exemple #15
0
 def __init__(self, user, **kw):
     if isinstance(user, int):
         cust_id = user
         self._user = None
     else:
         cust_id = CustomerID.get_id(user)
         self._user = user
     AuthorizeNetRequest.__init__(self, customerProfileId=cust_id, **kw)
Exemple #16
0
 def __init__(self, user, paymentProfiles, validationMode=None):
     SimpleXMLObject.__init__(self,
                              merchantCustomerId=user._fullname,
                              description=user.name,
                              email="",
                              paymentProfiles=paymentProfiles,
                              validationMode=validationMode,
                              customerProfileId=CustomerID.get_id(user))
Exemple #17
0
 def process_error(self, res):
     if self.is_error_code(res, Errors.DUPLICATE_RECORD):
         # D'oh.  We lost one
         m = self.re_lost_id.match(res.find("text").contents[0]).groups()
         CustomerID.set(self._user, m[0])
     # otherwise, we might have sent a user that already had a customer ID
     cust_id = CustomerID.get_id(self._user)
     if cust_id:
         return cust_id
     return AuthorizeNetRequest.process_error(self, res)
Exemple #18
0
def update_payment_method(user,
                          payment_method_id,
                          address,
                          credit_card,
                          validate=False):
    profile_id = CustomerID.get_id(user._id)
    payment_method_id = api.update_payment_profile(profile_id,
                                                   payment_method_id, address,
                                                   credit_card, validate)
    return payment_method_id
Exemple #19
0
 def process_error(self, res):
     if self.is_error_code(res, Errors.DUPLICATE_RECORD):
         # D'oh.  We lost one
         m = self.re_lost_id.match(res.find("text").contents[0]).groups()
         CustomerID.set(self._user, m[0])
     # otherwise, we might have sent a user that already had a customer ID
     cust_id = CustomerID.get_id(self._user)
     if cust_id:
         return cust_id
     return AuthorizeNetRequest.process_error(self, res)
Exemple #20
0
def void_transaction(user, transaction_id, campaign_id):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)

    if transaction_id <= 0:
        bid.void()
        return True, None

    profile_id = CustomerID.get_id(user._id)
    try:
        api.void_authorization_hold(profile_id, bid.pay_id, transaction_id)
    except api.TransactionError as e:
        return False, e.message

    bid.void()
    return True, None
Exemple #21
0
def void_transaction(user, transaction_id, campaign_id):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)

    if transaction_id <= 0:
        bid.void()
        return True, None

    profile_id = CustomerID.get_id(user._id)
    try:
        api.void_authorization_hold(profile_id, bid.pay_id, transaction_id)
    except api.TransactionError as e:
        return False, e.message

    bid.void()
    return True, None
Exemple #22
0
def get_or_create_customer_profile(user):
    profile_id = CustomerID.get_id(user._id)
    if not profile_id:
        profile_id = api.create_customer_profile(merchant_customer_id=user._fullname, description=user.name)
        CustomerID.set(user, profile_id)

    profile = api.get_customer_profile(profile_id)

    if not profile or profile.merchantCustomerId != user._fullname:
        raise ValueError("error getting customer profile")

    for payment_profile in profile.paymentProfiles:
        PayID.add(user, payment_profile.customerPaymentProfileId)

    return profile
Exemple #23
0
def get_or_create_customer_profile(user):
    profile_id = CustomerID.get_id(user._id)
    if not profile_id:
        profile_id = api.create_customer_profile(
            merchant_customer_id=user._fullname, description=user.name)
        CustomerID.set(user, profile_id)

    profile = api.get_customer_profile(profile_id)

    if not profile or profile.merchantCustomerId != user._fullname:
        raise ValueError("error getting customer profile")

    for payment_profile in profile.paymentProfiles:
        PayID.add(user, payment_profile.customerPaymentProfileId)

    return profile
Exemple #24
0
def refund_transaction(user, transaction_id, campaign_id, amount):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if transaction_id < 0:
        bid.refund(amount)
        return True, None

    profile_id = CustomerID.get_id(user._id)
    try:
        api.refund_transaction(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=amount,
            transaction_id=transaction_id,
        )
    except api.TransactionError as e:
        return False, e.message

    bid.refund(amount)
    return True, None
Exemple #25
0
def refund_transaction(user, transaction_id, campaign_id, amount):
    bid =  Bid.one(transaction=transaction_id, campaign=campaign_id)
    if transaction_id < 0:
        bid.refund(amount)
        return True, None

    profile_id = CustomerID.get_id(user._id)
    try:
        api.refund_transaction(
            customer_id=profile_id,
            payment_profile_id=bid.pay_id,
            amount=amount,
            transaction_id=transaction_id,
        )
    except api.TransactionError as e:
        return False, e.message

    bid.refund(amount)
    return True, None
Exemple #26
0
def get_account_info(user, recursed=False): 
    # if we don't have an ID for the user, try to make one
    if not CustomerID.get_id(user):
        cust_id = CreateCustomerProfileRequest(user).make_request()

    # if we do have a customerid, we should be able to fetch it from authorize
    try:
        u, data = GetCustomerProfileRequest(user).make_request()
    except AuthorizeNetException:
        u = None

    # if the user and the returned user don't match, delete the
    # current customer_id and recurse
    if u != user:
        if not recursed:
            CustomerID.delete(user)
            return get_account_info(user, True)
        else:
            raise AuthorizeNetException, "error creating user"
    return data
Exemple #27
0
def void_transaction(user, campaign_id, link_id, transaction_id, references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)

    if transaction_id <= 0:
        bid.void()
        return True, None

    references["pay_id"] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.void_authorization_hold(profile_id, bid.pay_id, transaction_id)
        AuthorizeTransaction._new(authorize_response, **references)
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.void()
    return True, None
Exemple #28
0
 def process_error(self, res):
     if self.is_error_code(res, Errors.DUPLICATE_RECORD):
         # authorize.net has a record for this customer but we don't. get
         # the correct id from the error message and update our db
         matches = self.re_lost_id.match(res.find("text").contents[0])
         if matches:
             match_groups = matches.groups()
             CustomerID.set(self._user, match_groups[0])
             g.log.debug("Updated missing authorize.net id for user %s" % self._user._id)
         else:
             # could happen if the format of the error message changes.
             msg = ("Failed to fix duplicate authorize.net profile id. "
                    "re_lost_id regexp may need to be updated. Response: %r" 
                    % res)
             raise AuthorizeNetException(msg)
     # otherwise, we might have sent a user that already had a customer ID
     cust_id = CustomerID.get_id(self._user)
     if cust_id:
         return cust_id
     return AuthorizeNetRequest.process_error(self, res)
Exemple #29
0
 def process_error(self, res):
     if self.is_error_code(res, Errors.DUPLICATE_RECORD):
         # authorize.net has a record for this customer but we don't. get
         # the correct id from the error message and update our db
         matches = self.re_lost_id.match(res.find("text").contents[0])
         if matches:
             match_groups = matches.groups()
             CustomerID.set(self._user, match_groups[0])
             g.log.debug("Updated missing authorize.net id for user %s" % self._user._id)
         else:
             # could happen if the format of the error message changes.
             msg = ("Failed to fix duplicate authorize.net profile id. "
                    "re_lost_id regexp may need to be updated. Response: %r" 
                    % res)
             raise AuthorizeNetException(msg)
     # otherwise, we might have sent a user that already had a customer ID
     cust_id = CustomerID.get_id(self._user)
     if cust_id:
         return cust_id
     return AuthorizeNetRequest.process_error(self, res)
Exemple #30
0
def void_transaction(user, campaign_id, link_id, transaction_id, references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)

    if transaction_id <= 0:
        bid.void()
        return True, None

    references['pay_id'] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.void_authorization_hold(
            profile_id, bid.pay_id, transaction_id)
        AuthorizeTransaction._new(authorize_response, **references)
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.void()
    return True, None
Exemple #31
0
def refund_transaction(user, campaign_id, link_id, amount, transaction_id, references):
    bid = Bid.one(transaction=transaction_id, campaign=campaign_id)
    if transaction_id < 0:
        bid.refund(amount)
        return True, None

    references["pay_id"] = bid.pay_id

    profile_id = CustomerID.get_id(user._id)
    try:
        authorize_response = api.refund_transaction(
            customer_id=profile_id, payment_profile_id=bid.pay_id, amount=amount, transaction_id=transaction_id
        )
        AuthorizeTransaction._new(authorize_response, **references)
    except api.TransactionError as e:
        authorize_response = e.authorize_response
        AuthorizeTransaction._new(authorize_response, **references)
        return False, e.message

    bid.refund(amount)
    return True, None
Exemple #32
0
 def make_request(self):
     # don't send a new request if the user already has an id
     return (CustomerID.get_id(self._user)
             or AuthorizeNetRequest.make_request(self))
Exemple #33
0
def delete_payment_method(user, payment_method_id):
    profile_id = CustomerID.get_id(user._id)
    success = api.delete_payment_profile(profile_id, payment_method_id)
    if success:
        PayID.delete(user, payment_method_id)
Exemple #34
0
def delete_payment_method(user, payment_method_id):
    profile_id = CustomerID.get_id(user._id)
    success = api.delete_payment_profile(profile_id, payment_method_id)
    if success:
        PayID.delete(user, payment_method_id)
Exemple #35
0
def update_payment_method(user, payment_method_id, address, credit_card, validate=False):
    profile_id = CustomerID.get_id(user._id)
    payment_method_id = api.update_payment_profile(profile_id, payment_method_id, address, credit_card, validate)
    return payment_method_id
Exemple #36
0
 def make_request(self):
     # don't send a new request if the user already has an id
     return CustomerID.get_id(self._user) or AuthorizeNetRequest.make_request(self)