From 3de2fffdbda7abdec9d7aa51a4babe7ebd9b9f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ch=C3=A9rifBALDE?= Date: Sun, 9 Oct 2022 20:32:32 +0200 Subject: [PATCH] 09/10/22 - 20H30 --- ela_factures_mgt.py | 68 ++++++++++++++++++++++++++++++++++--- main.py | 17 +++++++++- prj_common.py | 20 +++++++++++ strype_payement.py | 82 +++++++++++++++++++++++++++++++++++++++------ 4 files changed, 171 insertions(+), 16 deletions(-) diff --git a/ela_factures_mgt.py b/ela_factures_mgt.py index 2946039..4437c6d 100644 --- a/ela_factures_mgt.py +++ b/ela_factures_mgt.py @@ -168,6 +168,20 @@ def add_payement_mode(diction): if (tmp_count <= 0): return False, "Les identifiants sont incorrectes" + tmp_account = coll_tmp.find({'pwd': str(my_pwd), 'active': '1', 'token':str(my_secret)}) + + """ + Stripe : recuperation du stripe_account_id + """ + partner_stripe_account_id = tmp_account[0]['stripe_account_id'] + + """ + Recuperation du PaymentMethod s'il en a un : 'stripe_paymentmethod_id' + """ + stripe_paymentmethod_id = "" + if ("stripe_paymentmethod_id" in tmp_account[0].keys()): + if tmp_account[0]['stripe_paymentmethod_id']: + stripe_paymentmethod_id = tmp_account[0]['stripe_paymentmethod_id'] user_recid = "None" @@ -191,6 +205,7 @@ def add_payement_mode(diction): return False, "Impossible d'ajouter le mode de payement" + coll_name = MYSY_GV.dbname['payement_mode'] new_data = {} new_data['valide'] = "1" @@ -257,6 +272,38 @@ def add_payement_mode(diction): str(inspect.stack()[0][3]) + " : Les données de la carte sont incorrectes") return False, "Impossible d'ajouter le mode de paiement. Les données de la carte sont incorrectes" + + + # SI stripe_paymentmethod_id est vide alors on créé la carte + + print(" #### Creation de la carte dans STRIPE") + tmp_tab = str(new_data['date_exp_carte']).split("/") + if( len(tmp_tab) != 2): + mycommon.myprint( + str(inspect.stack()[0][3]) + " : La date d'expiration de la carte est incorrecte") + return False, "Impossible d'ajouter le mode de paiement. Les données de la carte sont incorrectes" + + + new_stripe_card = {} + new_stripe_card['customerid'] = partner_stripe_account_id + new_stripe_card['number'] = new_data['num_carte'] + new_stripe_card['exp_month'] = str(tmp_tab[0]) + new_stripe_card['exp_year'] = str(tmp_tab[1]) + new_stripe_card['cvc'] = new_data['cvv_carte'] + new_stripe_card['stripe_paymentmethod_id'] = stripe_paymentmethod_id + + + print("### la carte à créer est "+str(new_stripe_card)) + + local_status, created_stripe_cart = Stripe.create_update_payment_card(new_stripe_card) + if( local_status is False): + mycommon.myprint( + str(inspect.stack()[0][3]) + " : Impossible de créer la carte de payement dans Stripe") + return False, "Impossible d'ajouter le mode de paiement." + + print(" ### la carte est créée : "+str(created_stripe_cart)) + + if (str(new_data['type']) == "sepa"): if (len(str(new_data['iban'])) <= 0 or len(str(new_data['bic']) <= 0) or len(str(new_data['nom_compte'])) <= 0): @@ -264,8 +311,7 @@ def add_payement_mode(diction): str(inspect.stack()[0][3]) + " : Les données bancaires sont incorrectes") return False, "Impossible d'ajouter le mode de paiement. Les données bancaires sont incorrectes" - - + new_data['stripe_paymentmethod_id'] = str(created_stripe_cart.id) ret_val = coll_name.find_one_and_update( {'client_recid': user_recid, 'valide': '1'}, {"$set": new_data}, @@ -284,7 +330,7 @@ def add_payement_mode(diction): en mettant : ispending à 0 ''' coll_name = MYSY_GV.dbname['partnair_account'] - data_update = {'date_update':str(datetime.now()), 'ispending':'0'} + data_update = {'date_update':str(datetime.now()), 'ispending':'0', 'stripe_paymentmethod_id':str(created_stripe_cart.id)} ret_val = coll_name.find_one_and_update( {'recid': user_recid, }, {"$set": data_update}, @@ -366,13 +412,26 @@ def get_payement_mode(diction): stripe_account_id = mycommon.get_parnter_stripe_account_id_from_recid(user_recid) if stripe_account_id is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le stripe_account_id de l'utilisateur") - return False, " Impossible de recuperer les mode de payement" + return False, " Impossible de recuperer les modes de payement" + + """ + Recuperation de clé de payement stripe : stripe_paymentmethod_id + """ + stripe_paymentmethod_id = mycommon.get_parnter_stripe_stripe_paymentmethod_id_from_recid(user_recid) + if stripe_paymentmethod_id is False: + mycommon.myprint( + str(inspect.stack()[0][3]) + " - Impossible de recuperer le stripe_paymentmethod_id de l'utilisateur") + return False, " Impossible de recuperer les modes de payement" + + RetObject_stripe = [] tmp_diction = {} tmp_diction['stripe_account_id'] = str(stripe_account_id) + print("## recherche mod de payement stripe_account_id = "+str(stripe_account_id) ) + localStatus, payment_cards = Stripe.get_customer_payement_cards(tmp_diction) for tmp in payment_cards: RetObject_stripe.append(JSONEncoder().encode(tmp)) @@ -384,6 +443,7 @@ def get_payement_mode(diction): for retVal in coll_facture.find({'client_recid': user_recid, 'valide': '1'}): user = retVal + user['stripe_paymentmethod_id'] = stripe_paymentmethod_id RetObject.append(JSONEncoder().encode(user)) return True, RetObject diff --git a/main.py b/main.py index bb5ae0d..6e886f7 100644 --- a/main.py +++ b/main.py @@ -1615,6 +1615,8 @@ def strip_create_subscription(): payload = request.form.to_dict() print(" ### strip_create_subscription : payload = ",payload) localStatus, subscription_id, payment_intent_client_secret = Stripe.create_subscription(payload) + + print(" ### subscription_id = "+str(subscription_id)+" -- payment_intent_client_secret = "+str(payment_intent_client_secret)) return jsonify(status=localStatus, subscription_id=subscription_id,payment_intent_client_secret = payment_intent_client_secret ) """ @@ -1651,10 +1653,23 @@ def strip_create_payment_card(): # On recupere le corps (payload) de la requete payload = request.form.to_dict() print(" ### create_payment_card : payload = ",payload) - localStatus= Stripe.create_payment_card(payload) + localStatus= Stripe.create_update_payment_card(payload) return jsonify(status=localStatus, ) +""" +Stripe Payement : Confirmation d'un payement +""" +@app.route('/myclass/api/confirm_payment/', methods=['POST','GET']) +@crossdomain(origin='*') +def strip_confirm_payment(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### create_payment_card : payload = ",payload) + localStatus, payement_id, payement_status= Stripe.confirm_suscription(payload) + return jsonify(status=localStatus, payement_id=payement_id, payement_status=payement_status ) + + if __name__ == '__main__': print(" debut api") context = SSL.Context(SSL.SSLv23_METHOD) diff --git a/prj_common.py b/prj_common.py index fdeab69..44a1424 100644 --- a/prj_common.py +++ b/prj_common.py @@ -247,6 +247,26 @@ def get_parnter_stripe_account_id_from_recid(recid = ""): +""" +Cette fonction retour le stripe_paymentmethod_id +""" +def get_parnter_stripe_stripe_paymentmethod_id_from_recid(recid = ""): + try: + if len(str(recid)) <= 0 : + myprint(" get_parnter_stripe_account_id_from_token : Le recid partner est vide") + return False + + print(" #### recid = "+str(recid)) + coll_partner = MYSY_GV.dbname['partnair_account'] + tmp_val = coll_partner.find({'recid': str(recid), 'active': '1', 'locked':'0'}) + stripe_paymentmethod_id = tmp_val[0]['stripe_paymentmethod_id'] + return stripe_paymentmethod_id + + except Exception as e: + exc_type, exc_obj, exc_tb = sys.exc_info() + myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) + return False + ''' diff --git a/strype_payement.py b/strype_payement.py index ea6c08d..dab82a2 100644 --- a/strype_payement.py +++ b/strype_payement.py @@ -164,16 +164,17 @@ def get_customer_payement_cards(diction): """ Creation d'une carte de payement """ -def create_payment_card(diction): +def create_update_payment_card(diction): try: - field_list_obligatoire = ['number', 'exp_month', 'exp_year', 'cvc', 'customerid'] - + field_list_obligatoire = ['number', 'exp_month', 'exp_year', 'cvc', 'customerid', 'stripe_paymentmethod_id'] for val in field_list_obligatoire: if val not in diction: mycommon.myprint( str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ") return False, " Impossible de créer el moyen de payement" + + number = "" if ("number" in diction.keys()): if diction['number']: @@ -199,21 +200,44 @@ def create_payment_card(diction): if diction['customerid']: customer_id = diction['customerid'] + stripe_paymentmethod_id = "" + if ("stripe_paymentmethod_id" in diction.keys()): + if diction['stripe_paymentmethod_id']: + stripe_paymentmethod_id = diction['stripe_paymentmethod_id'] + """ + /!\ IMPORTANT : + Si le champs "stripe_paymentmethod_id" est rempli alors c'est une mise à jour : + a) on detache + b) on créer la nouvelle carte + c) on rattacjhe + + Si le champs "stripe_paymentmethod_id" est vide, alors c'est une creation + Attachement au client + """ + #print(" ## stripe_paymentmethod_id = "+str(stripe_paymentmethod_id)) + if (len(stripe_paymentmethod_id) > 2): + #print(" ### UPDATE ==> detache") + detache = stripe.PaymentMethod.detach( + stripe_paymentmethod_id, + ) + + + + #print(" ### CREATE ") new_card = stripe.PaymentMethod.create( type="card", card={ - "number": "4242424242424242", - "exp_month": 10, - "exp_year": 2012, - "cvc": "314", + "number": number, + "exp_month": int(exp_month), + "exp_year": int(exp_year), + "cvc": cvc, }, ) - print(" new_card = "+str(new_card)) + #print(" new_card = "+str(new_card)) - print(" l'ID de la carte est : "+str(new_card.id)) + #print(" l'ID de la carte est : "+str(new_card.id)) @@ -222,7 +246,8 @@ def create_payment_card(diction): customer=str(customer_id), ) - print(" attache = "+str(attache)) + #print(" attache = "+str(attache)) + return True, new_card except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() @@ -230,6 +255,39 @@ def create_payment_card(diction): return False, False +""" +Cette fonction confirme un abonnement +""" +def confirm_suscription(diction): + try: + field_list_obligatoire = ['stripe_payment_id',] + for val in field_list_obligatoire: + if val not in diction: + mycommon.myprint( + str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ") + return False, " Impossible de créer el moyen de payement" + + + stripe_payment_id = "" + if ("stripe_payment_id" in diction.keys()): + if diction['stripe_payment_id']: + stripe_payment_id = diction['stripe_payment_id'] + + + confirmation_payement = stripe.PaymentIntent.confirm( + stripe_payment_id, + payment_method="pm_card_visa", + ) + + print("### confirmation_payement.data = "+str(confirmation_payement)) + return True, confirmation_payement.id, confirmation_payement.status + + + except Exception as e: + exc_type, exc_obj, exc_tb = sys.exc_info() + mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) + return False, False, False, + def create_subscription(diction): @@ -265,7 +323,9 @@ def create_subscription(diction): payment_behavior='default_incomplete', expand=['latest_invoice.payment_intent'], ) - return True, subscription.id, subscription.latest_invoice.payment_intent.client_secret + + print(" ### subscription = "+str(subscription)) + return True, subscription.latest_invoice.payment_intent.id, subscription.latest_invoice.payment_intent.client_secret except Exception as e: