def _validate(self, card): if not isinstance(card, CreditCard): raise InvalidCard('credit_card not an instance of CreditCard') if not self.validate_card(card): raise InvalidCard('Invalid Card') card.month = '%02d' % card.month
def purchase(self, money, credit_card, options=None): if not options: options = {} if isinstance(credit_card, CreditCard) and not self.validate_card(credit_card): raise InvalidCard("Invalid Card") request_hash = self._build_request_hash(options) request_hash["amount"] = money if options.get("merchant_account_id"): request_hash["merchant_account_id"] = options.get("merchant_account_id") if isinstance(credit_card, CreditCard): request_hash["credit_card"] = { "number": credit_card.number, "expiration_date": self._cc_expiration_date(credit_card), "cardholder_name": self._cc_cardholder_name(credit_card), "cvv": credit_card.verification_value, } else: request_hash["payment_method_token"] = credit_card if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") request_hash = self._build_request_hash(options) request_hash["amount"] = money request_hash["credit_card"] = { "number": credit_card.number, "expiration_date": self._cc_expiration_date(credit_card), "cardholder_name": self._cc_cardholder_name(credit_card), "cvv": credit_card.verification_value, } braintree_options = options.get("options", {}) braintree_options.update({"submit_for_settlement": True}) request_hash.update({ "options": braintree_options }) response = braintree.Transaction.sale(request_hash) if response.is_success: status = "SUCCESS" transaction_was_successful.send(sender=self, type="purchase", response=response) else: status = "FAILURE" transaction_was_unsuccessful.send(sender=self, type="purchase", response=response) return {"status": status, "response": response}
def store(self, credit_card, options=None): options = options or {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") token = options.pop("access_token", self.we_pay_settings["ACCESS_TOKEN"]) try: response = self.we_pay.call('/credit_card/create', { 'client_id': self.we_pay_settings["CLIENT_ID"], 'user_name': credit_card.name, 'email': options.pop("customer")["email"], 'cc_number': credit_card.number, 'cvv': credit_card.verification_value, 'expiration_month': credit_card.month, 'expiration_year': credit_card.year, 'address': options.pop("billing_address") }, token=token) except WePayError as error: transaction_was_unsuccessful.send(sender=self, type="store", response=error) return {'status': 'FAILURE', 'response': error} transaction_was_successful.send(sender=self, type="store", response=response) return {'status': 'SUCCESS', 'response': response}
def purchase(self, amount, credit_card, options=None): card = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } try: response = self.stripe.Charge.create( amount=int(amount * 100), currency=self.default_currency.lower(), card=card) except self.stripe.CardError as error: transaction_was_unsuccessful.send(sender=self, type="purchase", response=error) return {'status': 'FAILURE', 'response': error} transaction_was_successful.send(sender=self, type="purchase", response=response) return {'status': 'SUCCESS', 'response': response}
def authorize(self, money, credit_card, options=None): """Using Authorize.net payment gateway, authorize the credit card for specified money""" if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") post = {} self.add_invoice(post, options) self.add_creditcard(post, credit_card) self.add_address(post, options) self.add_customer_data(post, options) response = self.commit("AUTH_ONLY", money, post) status = "SUCCESS" if response.response_code != 1: status = "FAILURE" transaction_was_unsuccessful.send(sender=self, type="authorization", response=response) else: transaction_was_successful.send(sender=self, type="authorization", response=response) return {"status": status, "response": response}
def store(self, credit_card, options=None): if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") payment_method = PaymentMethod.create( credit_card.number, credit_card.verification_value, credit_card.month, credit_card.year) else: # Using the token which has to be retained payment_method = PaymentMethod.find(credit_card) if payment_method.errors: transaction_was_unsuccessful.send(sender=self, type="store", response=payment_method) return {'status': 'FAILURE', 'response': payment_method} response = payment_method.retain() if response.errors: transaction_was_unsuccessful.send(sender=self, type="store", response=response) return {'status': 'FAILURE', 'response': response} transaction_was_successful.send(sender=self, type="store", response=response) return {'status': 'SUCCESS', 'response': response}
def recurring(self, credit_card, options=None): card = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } try: plan_id = options['plan_id'] self.stripe.Plan.retrieve(options['plan_id']) try: response = self.stripe.Customer.create(card=card, plan=plan_id) transaction_was_successful.send(sender=self, type="recurring", response=response) return {"status": "SUCCESS", "response": response} except self.stripe.CardError, error: transaction_was_unsuccessful.send(sender=self, type="recurring", response=error) return {"status": "FAILURE", "response": error} except self.stripe.InvalidRequestError, error: transaction_was_unsuccessful.send(sender=self, type="recurring", response=error) return {"status": "FAILURE", "response": error}
def authorize(self, money, credit_card, options=None): card = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } try: token = self.stripe.Token.create( card=card, amount=int(money * 100), ) transaction_was_successful.send(sender=self, type="authorize", response=token) return {'status': "SUCCESS", "response": token} except self.stripe.InvalidRequestError, error: transaction_was_unsuccessful.send(sender=self, type="authorize", response=error) return {"status": "FAILURE", "response": error}
def authorize(self, money, credit_card, options=None): if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") request_hash = self._build_request_hash(options) request_hash["amount"] = money request_hash["credit_card"] = { "number": credit_card.number, "expiration_date": self._cc_expiration_date(credit_card), "cardholder_name": self._cc_cardholder_name(credit_card), "cvv": credit_card.verification_value, } braintree_options = options.get("options", {}) if braintree_options: request_hash.update({ "options": braintree_options }) response = braintree.Transaction.sale(request_hash) if response.is_success: status = "SUCCESS" transaction_was_successful.send(sender=self, type="authorize", response=response) else: status = "FAILURE" transaction_was_unsuccessful.send(sender=self, type="authorize", response=response) return {"status": status, "response": response}
def authorize(self, money, credit_card, options=None): card = credit_card currency = self.default_currency.lower() if options and options.get('currency', False): currency = options.pop('currency') if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } try: response = self.stripe.Charge.create(amount=int(money * 100), currency=currency, card=card, capture=False, api_key=self.api_key) transaction_was_successful.send(sender=self, transaction_type="authorize", response=response) return {'status': "SUCCESS", "response": response} except self.stripe.CardError as error: transaction_was_unsuccessful.send(sender=self, transaction_type="purchase", response=error) return {'status': 'FAILURE', 'response': error}
def direct_payment(self, credit_card_details, options=None): """ Function that implement Direct Payment functionality provided by eWay. (Reference: http://www.eway.com.au/developers/api/direct-payments) Input Parameters: ( Please find the details here in this Gist: https://gist.github.com/08893221533daad49388 ) credit_card : Customer Credit Card details options : Customer and Recurring Payment details Output Paramters: status: 'SUCCESS' or 'FAILURE' response : eWay Response in Dictionary format. """ error_response = {} try: if (options and options.get('customer_details', False) and options.get('payment_details', False)): customer_details = options.get('customer_details') payment_details = options.get('payment_details') else: error_response = {"reason": "Not enough information Available!"} raise """ # Validate Entered credit card details. """ credit_card = CreditCard(**credit_card_details) is_valid = self.validate_card(credit_card) if not is_valid: raise InvalidCard("Invalid Card") """ # Create direct payment details """ direct_payment_details = self.add_direct_payment_details(credit_card, customer_details, payment_details) """ Process Direct Payment. """ dpObj = DirectPaymentClient(self.direct_payment_url) response = dpObj.process_direct_payment(direct_payment_details) """ Return value based on eWay Response """ eway_response = response.get('ewayResponse', None) if eway_response and eway_response.get('ewayTrxnStatus', 'false').lower() == 'true': status = "SUCCESS" else: status = "FAILURE" except Exception as e: error_response['exception'] = e return {"status": "FAILURE", "response": error_response} return {"status": status, "response": response}
def recurring(self, money, credit_card, options): if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") template_vars = {} template_vars['auth_login'] = self.login template_vars['auth_key'] = self.password template_vars['amount'] = money template_vars['card_number'] = credit_card.number template_vars['exp_date'] = credit_card.expire_date template_vars['start_date'] = options.get( 'start_date') or datetime.date.today().strftime("%Y-%m-%d") template_vars['total_occurrences'] = options.get( 'total_occurences', 9999) template_vars['interval_length'] = options.get('interval_length', 1) template_vars['interval_unit'] = options.get('interval_unit', 'months') template_vars['sub_name'] = options.get('sub_name', '') template_vars['first_name'] = credit_card.first_name template_vars['last_name'] = credit_card.last_name xml = render_to_string('billing/arb/arb_create_subscription.xml', template_vars) if self.test_mode: url = self.arb_test_url else: url = self.arb_live_url headers = {'content-type': 'text/xml'} conn = urllib2.Request(url=url, data=xml, headers=headers) try: open_conn = urllib2.urlopen(conn) xml_response = open_conn.read() except urllib2.URLError: return (5, '1', 'Could not talk to payment gateway.') response = nodeToDic( parseString(xml_response))['ARBCreateSubscriptionResponse'] # successful response # {u'ARBCreateSubscriptionResponse': {u'messages': {u'message': {u'code': u'I00001', # u'text': u'Successful.'}, # u'resultCode': u'Ok'}, # u'subscriptionId': u'933728'}} status = "SUCCESS" if response['messages']['resultCode'].lower() != 'ok': status = "FAILURE" transaction_was_unsuccessful.send(sender=self, type="recurring", response=response) else: transaction_was_successful.send(sender=self, type="recurring", response=response) return {"status": status, "response": response}
def authorize(self, money, credit_card, options=None): payment_method_token = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") pm = PaymentMethod.create(credit_card.number, credit_card.verification_value, credit_card.month, credit_card.year) payment_method_token = pm.payment_method_token response = Processor.authorize(payment_method_token, money) if response.errors: return {'status': 'FAILURE', 'response': response} return {'status': 'SUCCESS', 'response': response}
def store(self, credit_card, options=None): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") customer = self.stripe.Customer.create( card={ 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value }, description="Storing for future use" ) return {'status': 'SUCCESS', 'response': customer}
def purchase(self, money, credit_card): # Cases where token is directly sent for e.g. from Samurai.js payment_method_token = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") pm = PaymentMethod.create(credit_card.number, credit_card.verification_value, credit_card.month, credit_card.year) payment_method_token = pm.payment_method_token response = Processor.purchase(payment_method_token, money) if response.errors: return {'status': 'FAILURE', 'response': response} return {'status': 'SUCCESS', 'response': response}
def store(self, credit_card, options=None): card = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } customer = self.stripe.Customer.create(card=card) return {'status': 'SUCCESS', 'response': customer}
def authorize(self, money, credit_card, options=None): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") try: from samurai.payment_method import PaymentMethod from samurai.processor import Processor pm = PaymentMethod.create(credit_card.number, credit_card.verification_value, credit_card.month, credit_card.year) payment_method_token = pm.payment_method_token response = Processor.authorize(payment_method_token, money) except Exception, error: return {'status': 'FAILURE', 'response': error}
def purchase(self, money, credit_card, options=None): if options is None or 'order_id' not in options: raise ValueError( "Required parameter 'order_id' not found in options") if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") data = { 'amount': money, 'card': credit_card, } data.update(options) xml = self.build_xml(data) return self.handle_response(self.do_request(xml), "purchase")
def purchase(self, amount, credit_card, options=None): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") try: response = self.stripe.Charge.create( amount=amount * 100, currency=self.default_currency.lower(), card={ 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value },) except self.stripe.CardError, error: return {'status': 'FAILURE', 'response': error}
def authorize(self, money, credit_card, options=None): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") try: token = self.stripe.Token.create( card={ 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value }, amount=money * 100 ) return {'status': "SUCCESS", "response": token} except self.stripe.InvalidRequestError, error: return {"status": "FAILURE", "response": error}
def store(self, credit_card, options=None): card = credit_card if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } try: customer = self.stripe.Customer.create(card=card) except (self.stripe.CardError, self.stripe.InvalidRequestError), error: transaction_was_unsuccessful.send(sender=self, type="store", response=error) return {'status': 'FAILURE', 'response': error}
def purchase(self, money, credit_card, options=None): """ Using Eway payment gateway , charge the given credit card for specified money """ if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") client = RebillEwayClient(customer_id=self.customer_id, username=self.eway_username, password=self.eway_password, url=self.service_url, ) hosted_customer = client.client.factory.create("CreditCard") self.add_creditcard(hosted_customer, credit_card) self.add_address(hosted_customer, options) customer_id = client.create_hosted_customer(hosted_customer) pymt_response = client.process_payment( customer_id, money, options.get("invoice", 'test'), options.get("description", 'test') ) if not hasattr(pymt_response, "ewayTrxnStatus"): transaction_was_unsuccessful.send(sender=self, type="purchase", response=pymt_response) return {"status": "FAILURE", "response": pymt_response} if pymt_response.ewayTrxnStatus == "False": transaction_was_unsuccessful.send(sender=self, type="purchase", response=pymt_response) return {"status": "FAILURE", "response": pymt_response} transaction_was_successful.send(sender=self, type="purchase", response=pymt_response) return {"status": "SUCCESS", "response": pymt_response}
def purchase(self, amount, credit_card, metadata=None, options=None): card = credit_card currency = self.default_currency.lower() if options and options.get('currency', False): currency = options.pop('currency') customer = options and options.pop('customer', None) if isinstance(credit_card, CreditCard): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") card = { 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value } stripe_transaction = StripeTransaction() try: print(metadata['invoice']) order = Order.objects.get(pk=metadata['invoice']) response = self.stripe.Charge.create(amount=int(amount * 100), currency=currency, card=card, customer=customer, metadata=metadata, api_key=self.api_key) stripe_transaction.payment_status = State.COMPLETED stripe_transaction.txn_id = response['id'] stripe_transaction.info = response stripe_transaction.save() order.content_object = stripe_transaction order.finish() order.save() return {'status': 'SUCCESS', 'response': response} except self.stripe.CardError as error: stripe_transaction.payment_status = State.ERROR stripe_transaction.payment_message = error stripe_transaction.save() order.canceled() order.content_object = stripe_transaction order.save() return {'status': 'FAILURE', 'response': error}
def recurring(self, credit_card, options=None): if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") response = None try: plan_id = options['plan_id'] self.stripe.Plan.retrieve(options['plan_id']) try: response = self.stripe.Customer.create( card={ 'number': credit_card.number, 'exp_month': credit_card.month, 'exp_year': credit_card.year, 'cvc': credit_card.verification_value }, plan=plan_id, description="Thanks for subscribing" ) return {"status": "SUCCESS", "response": response} except self.stripe.CardError, error: return {"status": "FAILURE", "response": error} except self.stripe.InvalidRequestError, error: return {"status": "FAILURE", "response": error}
def recurring(self, credit_card_details, options=None): """ Recurring Payment Implementation using eWay recurring API (http://www.eway.com.au/developers/api/recurring) Input Parameters: ( Please find the details here in this Gist: https://gist.github.com/df67e02f7ffb39f415e6 ) credit_card : Customer Credit Card details options : Customer and Recurring Payment details Output Dict: status: 'SUCCESS' or 'FAILURE' response : Response list of rebill event request in order of provided input in options["customer_rebill_details"] list. """ error_response = {} try: if not options: error_response = {"reason": "Not enough information Available!"} raise """ # Validate Entered credit card details. """ credit_card = CreditCard(**credit_card_details) if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") rebillClient = RebillEwayClient( customer_id=self.customer_id, username=self.eway_username, password=self.eway_password, url=self.rebill_url, ) # CustomerDetails : To create rebill Customer customer_detail = rebillClient.client.factory.create("CustomerDetails") self.add_customer_details(credit_card, customer_detail, options) """ # Create Rebill customer and retrieve customer rebill ID. """ rebill_customer_response = rebillClient.create_rebill_customer(customer_detail) # Handler error in create_rebill_customer response if rebill_customer_response.ErrorSeverity: transaction_was_unsuccessful.send(sender=self, type="recurringCreateRebill", response=rebill_customer_response) error_response = rebill_customer_response raise rebile_customer_id = rebill_customer_response.RebillCustomerID """ For Each rebill profile # Create Rebill events using rebill customer ID and customer rebill details. """ rebill_event_response_list = [] for each_rebill_profile in options.get("customer_rebill_details", []): rebill_detail = rebillClient.client.factory.create("RebillEventDetails") self.add_rebill_details(rebill_detail, rebile_customer_id, credit_card, each_rebill_profile) rebill_event_response = rebillClient.create_rebill_event(rebill_detail) # Handler error in create_rebill_event response if rebill_event_response.ErrorSeverity: transaction_was_unsuccessful.send(sender=self, type="recurringRebillEvent", response=rebill_event_response) error_response = rebill_event_response raise rebill_event_response_list.append(rebill_event_response) transaction_was_successful.send(sender=self, type="recurring", response=rebill_event_response_list) except Exception as e: error_response['exception'] = e return {"status": "Failure", "response": error_response} return {"status": "SUCCESS", "response": rebill_event_response_list}
def purchase(self, money, credit_card, options=None): """Using PAYPAL DoDirectPayment, charge the given credit card for specified money""" if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") params = {} params['creditcardtype'] = credit_card.card_type.card_name params['acct'] = credit_card.number params['expdate'] = '%02d%04d' % (credit_card.month, credit_card.year) params['cvv2'] = credit_card.verification_value params['ipaddress'] = options['request'].META.get("REMOTE_ADDR", "") params['amt'] = money if options.get("email"): params['email'] = options["email"] address = options.get("billing_address", {}) first_name = None last_name = None try: first_name, last_name = address.get("name", "").split(" ") except ValueError: pass params['firstname'] = first_name or credit_card.first_name params['lastname'] = last_name or credit_card.last_name params['street'] = address.get("address1", '') params['street2'] = address.get("address2", "") params['city'] = address.get("city", '') params['state'] = address.get("state", '') params['countrycode'] = address.get("country", '') params['zip'] = address.get("zip", '') params['phone'] = address.get("phone", "") shipping_address = options.get("shipping_address", None) if shipping_address: params['shiptoname'] = shipping_address["name"] params['shiptostreet'] = shipping_address["address1"] params['shiptostreet2'] = shipping_address.get("address2", "") params['shiptocity'] = shipping_address["city"] params['shiptostate'] = shipping_address["state"] params['shiptocountry'] = shipping_address["country"] params['shiptozip'] = shipping_address["zip"] params['shiptophonenum'] = shipping_address.get("phone", "") wpp = PayPalWPP(options['request']) try: response = wpp.doDirectPayment(params) transaction_was_successful.send(sender=self, type="purchase", response=response) except PayPalFailure as e: transaction_was_unsuccessful.send(sender=self, type="purchase", response=e) # Slight skewness because of the way django-paypal # is implemented. return {"status": "FAILURE", "response": e} return {"status": response.ack.upper(), "response": response}
def authorize(self, money, credit_card, options=None): if not options: options = {} if not self.validate_card(credit_card): raise InvalidCard("Invalid Card") raise NotImplementedError