def submit_transaction(transaction): """ 执行transaction @transaction: transaction对象 """ result = Transaction.submit_for_settlement(transaction.id) return result
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the payment_log instance or None (if charge fails etc.) """ # TODO: refactor! This is not how such operations should be done. amount = Decimal(amount) try: result = Transaction.sale( { 'amount': amount.quantize(Decimal('.01')), 'customer_id': self.vault_id, 'options': { 'submit_for_settlement': True } } ) if result.is_success: # create a payment log payment_log = PaymentLog.objects.create(user=self.user, amount=amount, transaction_id=result.transaction.id) return payment_log else: logging.error("Bad braintree response %s" % result) raise Exception("Logical error in CC transaction") except Exception, e: logging.error("Failed to charge $%s to user:"******" %s with vault_id: %s error was %s" % (amount, self.user, self.vault_id, e)) return None
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the payment_log instance or None (if charge fails etc.) """ try: result = Transaction.sale({ 'amount': amount.quantize(Decimal('.01')), 'customer_id': self.vault_id, "options": { "submit_for_settlement": True } }) if result.is_success: # create a payment log payment_log = PaymentLog.objects.create( user=self.user, amount=amount, transaction_id=result.transaction.id) return payment_log else: raise Exception('Logical error in CC transaction') except Exception: logging.error( 'Failed to charge $%s to user: %s with vault_id: %s' % (amount, self.user, self.vault_id)) return None
def _test_REMOTE_using_braintree_lib(self): # TODO add braintree to our (optional!) REQUIREMENTS import braintree from braintree import Transaction import sys, M2Crypto # TODO document M2Crypto requires SWIG (and that it's a POS!) sudo aptitude install swig, and get python-mcrypto from your package mangler #sys.path.insert(0, '/home/phlip/tools/braintree-2.2.1') #print where_da_cert = Environment.braintree_root() + "/ssl/sandbox_braintreegateway_com.ca.crt" Environment.Sandbox = Environment("sandbox.braintreegateway.com", "443", True, where_da_cert) braintree.Configuration.configure( braintree.Environment.Sandbox, "TODO", "config", "these" ) result = Transaction.sale({ "amount": "100", "credit_card": { "number": "5105105105105100", "expiration_date": "05/2012" } #, # "options": { # "submit_for_settlement": True TODO turn this on for sale (purchase) off for authorize # } }) self.assertTrue(result.is_success) self.assertEquals(Transaction.Status.SubmittedForSettlement, result.transaction.status)
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the payment_log instance or None (if charge fails etc.) """ try: customer = Customer.find(self.vault_id) result = Transaction.sale( { 'amount': amount.quantize(Decimal('.01')), "payment_method_token": customer.credit_cards[0].token, 'customer_id': self.vault_id, "options": { "submit_for_settlement": True } } ) if result.is_success: # create a payment log payment_log = PaymentLog.objects.create(user=self.user, amount=amount, transaction_id=result.transaction.id) print "sucess" return payment_log else: raise Exception('Logical error in CC transaction') except Exception: return None logging.error('Failed to charge $%s to user: %s with vault_id: %s' % (amount, self.user, self.vault_id))
def _test_REMOTE_using_braintree_lib( self): # TODO add braintree to our (optional!) REQUIREMENTS import braintree from braintree import Transaction import sys, M2Crypto # TODO document M2Crypto requires SWIG (and that it's a POS!) sudo aptitude install swig, and get python-mcrypto from your package mangler #sys.path.insert(0, '/home/phlip/tools/braintree-2.2.1') #print where_da_cert = Environment.braintree_root( ) + "/ssl/sandbox_braintreegateway_com.ca.crt" Environment.Sandbox = Environment("sandbox.braintreegateway.com", "443", True, where_da_cert) braintree.Configuration.configure(braintree.Environment.Sandbox, "TODO", "config", "these") result = Transaction.sale({ "amount": "100", "credit_card": { "number": "5105105105105100", "expiration_date": "05/2012" } #, # "options": { # "submit_for_settlement": True TODO turn this on for sale (purchase) off for authorize # } }) self.assertTrue(result.is_success) self.assertEquals(Transaction.Status.SubmittedForSettlement, result.transaction.status)
def authorize(self, money, credit_card, **options): self.options.update(options) self._configure() self.result = Transaction.sale(self.generate_payment_info(money, credit_card)) self.response = self.__class__.Response(self.result.is_success, self.result.transaction.processor_response_text, self.result, is_test = self.gateway_mode == 'test', authorization = self.result.transaction.processor_authorization_code ) return self.response
def create_transaction( self, submerchant, amount, nonce, billed_user, address, ): """ Handles creating a transaction. :param SubmerchantTable submerchant: The submerchant who receives the lion's share :param float amount: The amount to be charged from the requesting user. :param str nonce: The payment nonce which is used to determine the method of payment for the customer. :param AddressTable address: The address used for billing. :param UserTable billed_user: The user who is being charged. :raises: IntegrationException :rtype: Transaction :return: The newly created transaction """ if amount < MINIMUM_AMOUNT_THRESHOLD: raise IntegrationException( 'Invalid amount requested. Minimum amount accepted is 15') try: return Transaction.sale({ 'merchant_account_id': submerchant.braintree_account_id, 'payment_method_nonce': nonce, 'amount': amount, 'service_fee_amount': self._calculate_service_fee(amount, submerchant), 'billing': { address.first_name, address.last_name, address.street_address, address.extended_address, address.locality, address.region, address.postal_code, address.country_code_alpha2, } }) except Exception as e: logging.error( 'Failed to create new transaction with ' 'exception of {0}. For submerchant {1} in amount {2}'.format( e, submerchant, amount)) raise IntegrationException('Failed to create transaction.')
def purchase(self, money, credit_card, **options): self._configure() info = self.generate_payment_info(money, credit_card) info["options"] = {"submit_for_settlement": True} self.result = Transaction.sale(info) self.response = self.__class__.Response(self.result.is_success, self.result.transaction.processor_response_text, self.result, is_test = self.gateway_mode =='test', authorization = self.result.transaction.processor_authorization_code ) return self.response
def payment(self, request): self.configure_braintree() body = request.data payment_method_nonce = body.get('payment_method_nonce') amount = body.get('amount') result = Transaction.sale({ 'amount': amount, 'payment_method_nonce': payment_method_nonce, 'options': { 'submit_for_settlement': True } }) if result.is_success: response = {'braintreeTransactionID': result.transaction.id, 'status': HTTP_200_OK} return Response(response, status=response['status']) else: response = {'message': result.message, 'reason': result.errors.deep_errors, 'status': HTTP_400_BAD_REQUEST} return Response(response, status=response['status'])
def authorize(self, money, credit_card, **options): self._configure() self.result = Transaction.sale( { "amount": "100", "credit_card": {"number": "5105105105105101", "expiration_date": "05/2012"} # , # "options": { # "submit_for_settlement": True TODO turn this on for sale (purchase) off for authorize # } } ) self.response = self.__class__.Response( self.result.is_success, self.result.transaction.processor_response_text, self.result, is_test=self.gateway_mode == "test", authorization=self.result.transaction.processor_authorization_code, ) return self.response
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the Charge instance """ try: result = Transaction.sale( { 'amount': amount.quantize(Decimal('.01')), 'customer_id': self.vault_id, "options": { "submit_for_settlement": True } } ) result = Charge(result, self.user) return result except Exception,e: logging.error('Failed to charge $%s to user: %s with vault_id: %s' % (amount, self.user, self.vault_id)) logging.error(e) return Charge()
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the Charge instance """ try: result = Transaction.sale({ 'amount': amount.quantize(Decimal('.01')), 'customer_id': self.vault_id, "options": { "submit_for_settlement": True } }) result = Charge(result, self.user) return result except Exception, e: logging.error( 'Failed to charge $%s to user: %s with vault_id: %s' % (amount, self.user, self.vault_id)) logging.error(e) return Charge()
def create_transaction(order, payment_method_nonce, user): """ 创建一个transaction进行支付 @order: Order对象 @payment_method_nonce: 前端 JS 返回的payment_method_nonce @user: 发送request的 User 对象 @return: Transaction对象 """ result = None profile = user.profile result = Transaction.sale({ 'amount': str(order.total_price), 'payment_method_nonce': payment_method_nonce, 'order_id': order.order_id, 'options': { 'submit_for_settlement': False } }) if result.is_success: profile.braintree_customer = True profile.save() return json_response(200, 'Create transaction sale successfully', {'transaction': result.transaction}) else: return json_response(500, 'Create transaction sale failed')
def capture_payment(self, testing=False, order=None, amount=None): """Process payments without an authorization step.""" braintree_settings = config_get_group('PAYMENT_SATCHMO_BRAINTREE') # Configure Braintree Configuration.configure( Environment.Production if django_settings.IS_PROD else Environment.Sandbox, braintree_settings.MERCHANT_ID.value, braintree_settings.PUBLIC_KEY.value, braintree_settings.PRIVATE_KEY.value ) if order: self.prepare_data(order) else: order = self.order if order.paid_in_full: self.log_extra('%s is paid in full, no capture attempted.', order) results = ProcessorResult(self.key, True, _("No charge needed, paid in full.")) self.record_payment() else: self.log_extra('Capturing payment for %s', order) amount = order.balance result = Transaction.sale({ "amount": amount, # "order_id": "123", "credit_card": { "number": order.credit_card.decryptedCC, "expiration_date": order.credit_card.expirationDate, # 05/2012 ? "cvv": order.credit_card.ccv }, "customer": { "first_name": order.contact.first_name, "last_name": order.contact.last_name, "email": order.contact.email, }, "billing": { "first_name": order.contact.first_name, "last_name": order.contact.last_name, "street_address": order.full_bill_street, "locality": order.bill_city, "region": order.bill_state, "postal_code": order.bill_postal_code, }, "options": { "submit_for_settlement": True } }) if result.is_success: payment = self.record_authorization(order=order, amount=amount, transaction_id=result.transaction.id) response_text = 'Success' else: response_text = '' for error in result.errors.deep_errors: if response_text != '': response_text += ', %s' % error.message else: response_text = error.message payment = self.record_failure(amount=amount) return ProcessorResult(self.key, result.is_success, response_text, payment=payment) # standard = self.get_standard_charge_data(amount=amount) # results = self.send_post(standard, testing) return results
def capture_payment(self, testing=False, order=None, amount=None): """Process payments without an authorization step.""" braintree_settings = config_get_group('PAYMENT_SATCHMO_BRAINTREE') # Configure Braintree Configuration.configure( Environment.Production if django_settings.IS_PROD else Environment.Sandbox, braintree_settings.MERCHANT_ID.value, braintree_settings.PUBLIC_KEY.value, braintree_settings.PRIVATE_KEY.value) if order: self.prepare_data(order) else: order = self.order if order.paid_in_full: self.log_extra('%s is paid in full, no capture attempted.', order) results = ProcessorResult(self.key, True, _("No charge needed, paid in full.")) self.record_payment() else: self.log_extra('Capturing payment for %s', order) amount = order.balance result = Transaction.sale({ "amount": amount, # "order_id": "123", "credit_card": { "number": order.credit_card.decryptedCC, "expiration_date": order.credit_card.expirationDate, # 05/2012 ? "cvv": order.credit_card.ccv }, "customer": { "first_name": order.contact.first_name, "last_name": order.contact.last_name, }, "billing": { "first_name": order.contact.first_name, "last_name": order.contact.last_name, "street_address": order.full_bill_street, "locality": order.bill_city, "region": order.bill_state, "postal_code": order.bill_postal_code, }, "options": { "submit_for_settlement": True } }) if result.is_success: payment = self.record_authorization( order=order, amount=amount, transaction_id=result.transaction.id) response_text = 'Success' else: response_text = 'Fail' payment = self.record_failure(amount=amount) return ProcessorResult(self.key, result.is_success, response_text, payment=payment) # standard = self.get_standard_charge_data(amount=amount) # results = self.send_post(standard, testing) return results