def create(cls, card_number, cvv, expiry_month, expiry_year, **other_args): """ Creates a payment method. Transparent redirects are favored method for creating payment methods. Using this call places the burden of PCI compliance on the client since the data passes through it. :: pm = PaymentMethod.create('4242424242424242', '133', '07', '12') assert pm.is_sensitive_data_valid """ payload = { 'payment_method': { 'card_number': card_number, 'cvv': cvv, 'expiry_month': expiry_month, 'expiry_year': expiry_year, } } optional_data = dict((k, v) for k, v in other_args.iteritems() if k in cls.create_data) payload['payment_method'].update(**optional_data) payload = dict_to_xml(payload) # Send payload and return payment method. req = Request(cls.create_url, payload, method='post') req.add_header("Content-Type", "application/xml") return cls(fetch_url(req))
def update(self, **other_args): """ Updates a payment method. Payment method can't be updated once it has been retained or redacted. :: pm = PaymentMethod.create('4242424242424242', '133', '07', '12') assert pm.is_sensitive_data_valid pm.update(first_name='dummy') if not pm.errors: assert pm.first_name == 'dummy' else: # deal with pm.errors """ payload = { 'payment_method': { } } optional_data = dict((k, v) for k, v in other_args.iteritems() if k in self.create_data) payload['payment_method'].update(**optional_data) payload = dict_to_xml(payload) # Send payload and return payment method. req = Request(self.update_url % self.payment_method_token, payload, method='put') req.add_header("Content-Type", "application/xml") res = fetch_url(req) self._update_fields(res) return self
def update(self, **other_args): """ Updates a payment method. Payment method can't be updated once it has been retained or redacted. :: pm = PaymentMethod.create('4242424242424242', '133', '07', '12') assert pm.is_sensitive_data_valid pm.update(first_name='dummy') if not pm.errors: assert pm.first_name == 'dummy' else: # deal with pm.errors """ payload = {'payment_method': {}} optional_data = dict( (k, v) for k, v in other_args.iteritems() if k in self.create_data) payload['payment_method'].update(**optional_data) payload = dict_to_xml(payload) # Send payload and return payment method. req = Request(self.update_url % self.payment_method_token, payload, method='put') req.add_header("Content-Type", "application/xml") res = fetch_url(req) self._update_fields(res) return self
def authorize( cls, api, pg, pm, amount, currency, order_id = None, ip = None, description = None, retain_on_success = False, callback_url = '', redirect_url = '' ): ''' Works just like the add method except no funds are actually transfered. Use capture method to make the actual transfer or void method to cancel it. ''' data = { 'transaction': { 'amount': amount, 'currency_code': currency, 'payment_method_token': pm.token } } if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description if callback_url: data['transaction']['callback_url'] = callback_url if redirect_url: data['transaction']['redirect_url'] = redirect_url if retain_on_success: data['transaction']['retain_on_success'] = 'true' data = dict_to_xml( data ) return APIRequest( api, 'gateways/%s/authorize.xml' % pg.token, 'POST', data ).to_object( Transaction )
def add( cls, api, pg, pm, amount, currency, order_id = None, ip = None, description = None ): ''' Creates a new transaction pg is the payment gateway to use pm is the payment method to use order_id is optional and would be used to link a transaction to an order in your system ip is the ip address that initiated the transaction. This should be the ip of the user using your system. ''' data = { 'transaction': { 'amount': amount, 'currency_code': currency, 'payment_method_token': pm.token } } if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description data = dict_to_xml( data ) return APIRequest( api, 'gateways/%s/purchase.xml' % pg.token, 'POST', data ).to_object( Transaction )
def authorize(cls, api, pg, pm, amount, currency, order_id=None, ip=None, description=None): ''' Works just like the add method except no funds are actually transfered. Use capture method to make the actual transfer or void method to cancel it. ''' data = { 'transaction': { 'amount': amount, 'currency_code': currency, 'payment_method_token': pm.token } } if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description data = dict_to_xml(data) return APIRequest(api, 'gateways/%s/authorize.xml' % pg.token, 'POST', data).to_object(Transaction)
def create(cls, card_number, cvv, expiry_month, expiry_year, **other_args): """ Creates a payment method. Transparent redirects are favored method for creating payment methods. Using this call places the burden of PCI compliance on the client since the data passes through it. :: pm = PaymentMethod.create('4242424242424242', '133', '07', '12') assert pm.is_sensitive_data_valid """ payload = { 'payment_method': { 'card_number': card_number, 'cvv': cvv, 'expiry_month': expiry_month, 'expiry_year': expiry_year, } } optional_data = dict( (k, v) for k, v in other_args.iteritems() if k in cls.create_data) payload['payment_method'].update(**optional_data) payload = dict_to_xml(payload) # Send payload and return payment method. req = Request(cls.create_url, payload, method='post') req.add_header("Content-Type", "application/xml") return cls(fetch_url(req))
def update( self, **params ): ''' This will update any non-sensitive attributes. Attempting to change things such as the card number will cause an error. ''' data = dict_to_xml( { 'payment_method': params } ) APIRequest( self.api, 'payment_methods/%s.xml' % self.token, 'PUT', data ).to_object( PaymentMethod, target = self ) return self
def update(self, **params): ''' This will update any non-sensitive attributes. Attempting to change things such as the card number will cause an error. ''' data = dict_to_xml({'payment_method': params}) APIRequest(self.api, 'payment_methods/%s.xml' % self.token, 'PUT', data).to_object(PaymentMethod, target=self) return self
def _transact(self, endpoint, amount=None): """ Meant to be used internally and shouldn't be called from outside. Makes the specified call and returns resultant `transaction`. """ if not getattr(self, 'transaction_token', None): raise UnauthorizedTransactionError('Transaction token is missing. Only authorized' 'transactions can make this call.') data = dict_to_xml({'transaction':{'amount': amount}}) req = Request(endpoint % self.transaction_token, data, method='post') res = fetch_url(req) return type(self)(res)
def _transact(self, endpoint, amount=None): """ Meant to be used internally and shouldn't be called from outside. Makes the specified call and returns resultant `transaction`. """ if not getattr(self, 'transaction_token', None): raise UnauthorizedTransactionError( 'Transaction token is missing. Only authorized' 'transactions can make this call.') data = dict_to_xml({'transaction': {'amount': amount}}) req = Request(endpoint % self.transaction_token, data, method='post') res = fetch_url(req) return type(self)(res)
def void( self, order_id = None, ip = None ): ''' Cancels a Transaction created with authorize ''' if order_id or ip: data = { 'transaction': { } } if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip data = dict_to_xml( data ) else: data = '' return APIRequest( self.api, 'transactions/%s/void.xml' % self.token, 'POST', '' ).to_object( Transaction, target = self )
def add( cls, api, type, **params ): ''' Creates a new Payment Gateway. type is the type name of the payment gateway to be created. Any extra parameters required by the gateway type should be passed as keyword arguments. Run api.gateway_types() to get a list of supported gateway types and what parameters they require. ''' data = { 'gateway': { 'gateway_type': type } } data['gateway'].update( params ) data = dict_to_xml( data ) ret = APIRequest( api, 'gateways.xml', data = data ).to_object( PaymentGateway ) return ret
def add(cls, api, type, **params): ''' Creates a new Payment Gateway. type is the type name of the payment gateway to be created. Any extra parameters required by the gateway type should be passed as keyword arguments. Run api.gateway_types() to get a list of supported gateway types and what parameters they require. ''' data = {'gateway': {'gateway_type': type}} data['gateway'].update(params) data = dict_to_xml(data) ret = APIRequest(api, 'gateways.xml', 'POST', data=data).to_object(PaymentGateway) return ret
def void(self, order_id=None, ip=None, description=None): ''' Cancels a Transaction created with authorize ''' if order_id or ip or description: data = {'transaction': {}} if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description data = dict_to_xml(data) else: data = '' return APIRequest(self.api, 'transactions/%s/void.xml' % self.token, 'POST', '').to_object(Transaction, target=self)
def credit( self, amount = None, order_id = None, ip = None ): ''' Cancels or refunds any Transaction created in the past. ''' if amount or order_id or ip: data = { 'transaction': { } } if amount: data['transaction']['amount'] = amount if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip data = dict_to_xml( data ) else: data = '' return APIRequest( self.api, 'transactions/%s/credit.xml' % self.token, 'POST', '' ).to_object( Transaction, target = self )
def capture( self, amount = None, order_id = None, ip = None ): ''' Does the actual transfer for a Transaction created with authorize ''' if amount or order_id or ip: data = { 'transaction': { } } if amount: data['transaction']['amount'] = amount if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip data = dict_to_xml( data ) else: data = '' return APIRequest( self.api, 'transactions/%s/capture.xml' % self.token, 'POST', data ).to_object( Transaction, target = self )
def _construct_options(cls, payment_method_token, transaction_type, amount, options): """ Constructs XML payload to be sent for the transaction. """ # Pick relevant options and construct xml payload. purchase_data = { 'transaction': { 'type': transaction_type, 'currency_code': 'USD', 'amount': amount } } options = dict((k, v) for k, v in options.iteritems() if k in cls.purchase_optional_data) options['payment_method_token'] = payment_method_token purchase_data['transaction'].update(options) purchase_data = dict_to_xml(purchase_data) return purchase_data
def authorize( cls, api, pg, pm, amount, currency, order_id = None, ip = None ): ''' Works just like the add method except no funds are actually transfered. Use capture method to make the actual transfer or void method to cancel it. ''' data = { 'transaction': { 'amount': amount, 'currency_code': currency, 'payment_method_token': pm.token } } if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip data = dict_to_xml( data ) return APIRequest( api, 'gateways/%s/authorize.xml' % pg.token, 'POST', data ).to_object( Transaction )
def capture(self, amount=None, order_id=None, ip=None, description=None): ''' Does the actual transfer for a Transaction created with authorize ''' if amount or order_id or ip or description: data = {'transaction': {}} if amount: data['transaction']['amount'] = amount if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description data = dict_to_xml(data) else: data = '' return APIRequest(self.api, 'transactions/%s/capture.xml' % self.token, 'POST', data).to_object(Transaction, target=self)
def credit(self, amount=None, order_id=None, ip=None, description=None): ''' Cancels or refunds any Transaction created in the past. ''' if amount or order_id or ip or description: data = {'transaction': {}} if amount: data['transaction']['amount'] = amount if order_id: data['transaction']['order_id'] = order_id if ip: data['transaction']['ip'] = ip if description: data['transaction']['description'] = description data = dict_to_xml(data) else: data = '' return APIRequest(self.api, 'transactions/%s/credit.xml' % self.token, 'POST', '').to_object(Transaction, target=self)
def add(cls, api, credit_card): ''' Creates a new Payment Method. WARNING: Using this method to create payment methods should only be used if you are unable to use the standard method outlined in: https://spreedlycore.com/manual/quickstart The standard method is safer because you do not have to touch the credit card data as it is sent straight to spreedly core via the form. credit_card is a dictionary with credit card details with the following keys: first_name, last_name, number, verification_value, month, year Where number is the credit card number and month and year are for the expiration date. The year is expressed using 4 digits (ie 2012) ''' d = dict_to_xml({'credit_card': credit_card}) req = APIRequest(api, 'payment_methods.xml', 'POST', data=d) o = PaymentMethod.__new__(cls) o.from_dict(xml_to_dict(req.xml().find('payment_method'))) return o
def add( cls, api, credit_card ): ''' Creates a new Payment Method. WARNING: Using this method to create payment methods should only be used if you are unable to use the standard method outlined in: https://spreedlycore.com/manual/quickstart The standard method is safer because you do not have to touch the credit card data as it is sent straight to spreedly core via the form. credit_card is a dictionary with credit card details with the following keys: first_name, last_name, number, verification_value, month, year Where number is the credit card number and month and year are for the expiration date. The year is expressed using 4 digits (ie 2012) ''' d = dict_to_xml( { 'credit_card': credit_card } ) req = APIRequest( api, 'payment_methods.xml', 'POST', data = d ) o = PaymentMethod.__new__( cls ) o.from_dict( xml_to_dict( req.xml().find( 'payment_method' ) ) ) return o