def create_stripe_customer(source_token: str, email: str, userid: str, name: str, idempotency_key: str) -> Customer: """ Create a new Stripe Customer. :param source_token: :param email: :param userid: :param name: :param idempotency_key: :return: Customer """ try: customer = Customer.create( source=source_token, email=email, description=userid, name=name, metadata={"userid": userid}, idempotency_key=idempotency_key, ) return customer except ( InvalidRequestError, APIConnectionError, APIError, RateLimitError, IdempotencyError, CardError, StripeErrorWithParamCode, ) as e: logger.error("create customer error", error=e) raise e
def token_for_customer(self, token, user): """Create a user in Stipe's system using a one time use token as returned from `stripe.js`. This function does not alter the user object in any way. :params token: The one time use token returned by stripe.js when submitting payment details. :type token: string :param user: The user with which the new stripe customer will be associated, used only for error logging. :type user: :class:`pooldlib.postgresql.models.User` :returns: string, the Customer ID returned by Stripe. :raises: stripe.AuthenticationError stripe.InvalidRequestError stripe.APIConnectionError stripe.APIError """ kwargs = dict(card=token, description='Poold user: %s' % user.id, email=user.email) try: stripe_user = _Customer.create(api_key=self.api_key, **kwargs) msg = 'New Stripe Customer Created' logger.transaction(msg, **kwargs) except stripe.StripeError, e: self._handle_error(e, user, kwargs)
def stripe(): print '--- got stripe request ---' stripe.api_key = stripe_keys['secret_key'] form = stripeform() output = render_template('stripe.html', key=stripe_keys['publishable_key'], form=form) if form.amount.data: drill("xTcR Donation","Thanks for donating to xTcR!",request.form["stripeEmail"]) #stripe.api_key = "sk_test_KBnACrVyXtFPcHyGTd5cot9D" customer = Customer.create( email= request.form["stripeEmail"], card=request.form['stripeToken'] ) charge = Charge.create( customer=customer.id, amount=form.amount.data * 100, currency='usd', description='xTcR Donation' ) amnt = form.amount.data * 100 custom = '0' if request.form.has_key('custom'): custom = str(request.form['custom']) cb_data = { #'app': 'donate', #'do': 'payment', #'gateway': '1', 'mc_gross': float(charge['amount']) / 100, 'mc_currency': charge['currency'], 'custom': custom, 'payment_status': 'Completed' if charge['status'] == 'succeeded' else charge['status'], 'business': charge['receipt_email'], 'option_selection1': '', 'option_selection2': '', 'txn_id': charge['id'], 'memo': 'stripe', 'fees': (float(charge['amount']) / 100) * 0.029 + 0.30, 'item_name': str(request.form['item_name']), 'item_number': str(request.form['item_number']) } r = requests.post("http://192.3.130.131/index.php?app=donate&do=payment&gateway=1", data=cb_data) try: r = requests.get("http://kc1.freebieservers.com/1/donate.php?name=%s&amount=%s"%(g.user.nickname,amnt)) print r print r.url except: print "Server Down" print ' ----- ' cmd("echo '%s' >> stripe.log"%r.text) output = redirect("http://xtcr.net/success.html") return output
def billing_profile_created_receiver(sender, instance, *args, **kwargs): if not instance.customer_id and instance.email: # print("ACTUAL API REQUEST Send to Stripe") customer = Customer.create( email=instance.email ) # print(customer) instance.customer_id = customer.id
def create_customer( subhub_account: SubHubAccount, user_id: str, email: str, source_token: str, origin_system: str, display_name: str, ) -> Customer: _validate_origin_system(origin_system) # First search Stripe to ensure we don't have an unlinked Stripe record # already in Stripe customer = None customers = Customer.list(email=email) for possible_customer in customers.data: if possible_customer.email == email: # If the userid doesn't match, the system is damaged. if possible_customer.metadata.get("userid") != user_id: raise ServerError("customer email exists but userid mismatch") customer = possible_customer # If we have a mis-match on the source_token, overwrite with the # new one. if customer.default_source != source_token: Customer.modify(customer.id, source=source_token) break # No existing Stripe customer, create one. if not customer: try: customer = Customer.create( source=source_token, email=email, description=user_id, name=display_name, metadata={"userid": user_id}, ) except InvalidRequestError as e: logger.error("create customer error", error=e) raise InvalidRequestError( message="Unable to create customer.", param=str(e) ) # Link the Stripe customer to the origin system id db_account = subhub_account.new_user( uid=user_id, origin_system=origin_system, cust_id=customer.id ) if not subhub_account.save_user(db_account): # Clean-up the Stripe customer record since we can't link it Customer.delete(customer.id) e = IntermittentError("error saving db record") logger.error("unable to save user or link it", error=e) raise e return customer
def charge(): if not check_body(["plan", "transaction"]): return errors.missing_charge_parameters() plan = int(request.json["plan"]) token = request.json["transaction"]["id"] amount = [499, 2748, 4992][plan] customer = Customer.create(email=current_user.email, source=token) charge = Charge.create(customer=customer.id, amount=amount, currency="usd", description="Flask Charge") return "", 204
def stripe(): print '--- got stripe request ---' stripe.api_key = stripe_keys['secret_key'] form = stripeform() output = render_template('stripe.html', key=stripe_keys['publishable_key'], form=form) if form.amount.data: drill("xTcR Donation", "Thanks for donating to xTcR!", request.form["stripeEmail"]) #stripe.api_key = "sk_test_KBnACrVyXtFPcHyGTd5cot9D" customer = Customer.create(email=request.form["stripeEmail"], card=request.form['stripeToken']) charge = Charge.create(customer=customer.id, amount=form.amount.data * 100, currency='usd', description='xTcR Donation') amnt = form.amount.data * 100 custom = '0' if request.form.has_key('custom'): custom = str(request.form['custom']) cb_data = { #'app': 'donate', #'do': 'payment', #'gateway': '1', 'mc_gross': float(charge['amount']) / 100, 'mc_currency': charge['currency'], 'custom': custom, 'payment_status': 'Completed' if charge['status'] == 'succeeded' else charge['status'], 'business': charge['receipt_email'], 'option_selection1': '', 'option_selection2': '', 'txn_id': charge['id'], 'memo': 'stripe', 'fees': (float(charge['amount']) / 100) * 0.029 + 0.30, 'item_name': str(request.form['item_name']), 'item_number': str(request.form['item_number']) } r = requests.post( "http://192.3.130.131/index.php?app=donate&do=payment&gateway=1", data=cb_data) try: r = requests.get( "http://kc1.freebieservers.com/1/donate.php?name=%s&amount=%s" % (g.user.nickname, amnt)) print r print r.url except: print "Server Down" print ' ----- ' cmd("echo '%s' >> stripe.log" % r.text) output = redirect("http://xtcr.net/success.html") return output