11# Part of OpenG2P. See LICENSE file for full copyright and licensing details.
2+ import json
23import logging
34import os
45from datetime import datetime , timedelta
@@ -87,6 +88,12 @@ class G2PPaymentManagerG2PConnect(models.Model):
8788 send_payments_domain = fields .Text ("Filter Batches to Send" , default = "[]" )
8889 sender_id = fields .Char ("Sender ID" , default = DEFAULT_SENDER_ID , required = False )
8990
91+ def create_jwt_token (self , payload : str ):
92+ self .ensure_one ()
93+ enc_provider = self .get_encryption_provider ()
94+ token = enc_provider .jwt_sign (payload , include_payload = False )
95+ return token
96+
9097 @api .onchange ("payee_id_type" )
9198 def _onchange_payee_id_type (self ):
9299 prefix_mapping = {
@@ -172,11 +179,14 @@ def _send_payments(self, batches):
172179 )
173180 try :
174181 _logger .info ("G2P Bridge Disbursement Batch Data: %s" , batch_data )
175-
182+ token = self .create_jwt_token (json .dumps (batch_data , separators = ("," , ":" )))
183+ headers = {
184+ "Accept" : "application/json" ,
185+ "Content-Type" : "application/json" ,
186+ "Signature" : token ,
187+ }
176188 response = requests .post (
177- self .payment_endpoint_url ,
178- json = batch_data ,
179- timeout = self .api_timeout ,
189+ self .payment_endpoint_url , json = batch_data , timeout = self .api_timeout , headers = headers
180190 )
181191 _logger .info ("G2P Bridge Disbursement response: %s" , response .content )
182192 response .raise_for_status ()
@@ -242,7 +252,6 @@ def payments_status_check(self, id_):
242252 _logger .info (f"Payment for Status Check: { len (payments )} " )
243253
244254 status_data = {
245- "signature" : "string" ,
246255 "header" : {
247256 "version" : "1.0.0" ,
248257 "message_id" : "string" ,
@@ -259,11 +268,17 @@ def payments_status_check(self, id_):
259268 }
260269 try :
261270 _logger .info ("G2P Connect Disbursement Status Data: %s" , status_data )
262-
271+ token = self .create_jwt_token (json .dumps (status_data , separators = ("," , ":" )))
272+ headers = {
273+ "Accept" : "application/json" ,
274+ "Content-Type" : "application/json" ,
275+ "Signature" : token ,
276+ }
263277 res = requests .post (
264278 payment_manager .status_endpoint_url ,
265279 json = status_data ,
266280 timeout = payment_manager .api_timeout ,
281+ headers = headers ,
267282 )
268283 _logger .info ("G2P Connect Disbursement Status response: %s" , res .content )
269284 res .raise_for_status ()
@@ -353,7 +368,7 @@ def _create_envelope_g2p_bridge(self, cycle):
353368 "version" : "1.0.0" ,
354369 "message_id" : "string" ,
355370 "message_ts" : "string" ,
356- "action" : "string " ,
371+ "action" : "create_disbursement_envelope " ,
357372 "sender_id" : self .sender_id ,
358373 "sender_uri" : "" ,
359374 "receiver_id" : "" ,
@@ -373,10 +388,17 @@ def _create_envelope_g2p_bridge(self, cycle):
373388 },
374389 }
375390 try :
391+ token = self .create_jwt_token (json .dumps (envelope_request_data , separators = ("," , ":" )))
392+ headers = {
393+ "Accept" : "application/json" ,
394+ "Content-Type" : "application/json" ,
395+ "Signature" : token ,
396+ }
376397 response = requests .post (
377398 self .envelope_creation_url ,
378399 json = envelope_request_data ,
379400 timeout = 10 ,
401+ headers = headers ,
380402 )
381403 # Store the id from respose to the model
382404 disbursement_envelope_id = response .json ().get ("message" ).get ("disbursement_envelope_id" )
0 commit comments