Skip to content

Commit 23d0b57

Browse files
mrsaicharan1uds5501
authored andcommitted
feat: Transaction Processing logic & Wallet linking implementation (#6399)
* feat: Transaction Processing logic & Wallet linking implementation * Added jwt headers
1 parent a408e39 commit 23d0b57

File tree

2 files changed

+127
-12
lines changed

2 files changed

+127
-12
lines changed

app/api/helpers/payment.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from app.api.helpers.utilities import represents_int
1414
from app.models.stripe_authorization import StripeAuthorization
1515
from app.settings import get_settings, Environment
16-
from app.api.helpers.db import safe_query, save_to_db
16+
from app.api.helpers.db import safe_query
1717
from app.models import db
1818
from app.models.order import Order
1919

@@ -325,10 +325,27 @@ class PaytmPaymentsManager(object):
325325
Class to manage PayTM payments
326326
"""
327327

328+
@property
329+
def paytm_endpoint(self):
330+
if get_settings()['paytm_mode'] == 'test':
331+
url = "https://securegw-stage.paytm.in/theia/api/v1/"
332+
else:
333+
url = "https://securegw.paytm.in/theia/api/v1/"
334+
return url
335+
328336
@staticmethod
329337
def generate_checksum(paytm_params):
330-
if get_settings()['paytm_mode'] == 'sandbox':
338+
if get_settings()['paytm_mode'] == 'test':
331339
merchant_key = get_settings()['paytm_sandbox_secret']
332340
else:
333341
merchant_key = get_settings()['paytm_live_secret']
334342
return checksum.generate_checksum_by_str(json.dumps(paytm_params["body"]), merchant_key)
343+
344+
@staticmethod
345+
def hit_paytm_endpoint(url, head, body=None):
346+
paytm_params = {}
347+
paytm_params["body"] = body
348+
paytm_params["head"] = head
349+
post_data = json.dumps(paytm_params)
350+
response = requests.post(url, data=post_data, headers={"Content-type": "application/json"}).json()
351+
return response

app/api/orders.py

Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import logging
22
import json
33
import pytz
4-
from datetime import datetime
4+
import time
5+
import omise
56
import requests
67

7-
8-
import omise
8+
from datetime import datetime
99
from flask import request, jsonify, Blueprint, url_for, redirect
1010
from flask_jwt_extended import current_user
1111
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
@@ -41,6 +41,7 @@
4141
from app.models.ticket_holder import TicketHolder
4242
from app.models.user import User
4343

44+
4445
order_misc_routes = Blueprint('order_misc', __name__, url_prefix='/v1')
4546
alipay_blueprint = Blueprint('alipay_blueprint', __name__, url_prefix='/v1/alipay')
4647

@@ -543,7 +544,6 @@ def create_paypal_payment(order_identifier):
543544
else:
544545
return jsonify(status=False, error=response)
545546

546-
547547
@order_misc_routes.route('/orders/<string:order_identifier>/verify-mobile-paypal-payment', methods=['POST'])
548548
@jwt_required
549549
def verify_mobile_paypal_payment(order_identifier):
@@ -629,6 +629,7 @@ def omise_checkout(order_identifier):
629629

630630

631631
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/initiate-transaction', methods=['POST', 'GET'])
632+
@jwt_required
632633
def initiate_transaction(order_identifier):
633634
"""
634635
Initiating a PayTM transaction to obtain the txn token
@@ -641,14 +642,14 @@ def initiate_transaction(order_identifier):
641642
# body parameters
642643
paytm_params["body"] = {
643644
"requestType": "Payment",
644-
"mid": (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'sandbox'
645+
"mid": (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'test'
645646
else get_settings()['paytm_live_merchant']),
646647
"websiteName": "eventyay",
647-
"orderId": order.id,
648+
"orderId": order_identifier,
648649
"callbackUrl": "",
649650
"txnAmount": {
650651
"value": order.amount,
651-
"currency": order.event.payment_currency,
652+
"currency": "INR",
652653
},
653654
"userInfo": {
654655
"custId": order.user.id,
@@ -660,11 +661,108 @@ def initiate_transaction(order_identifier):
660661
"signature" : checksum
661662
}
662663
post_data = json.dumps(paytm_params)
663-
if paytm_mode == 'sandbox':
664+
if paytm_mode == 'test':
664665
url = "https://securegw-stage.paytm.in/theia/api/v1/initiateTransaction?mid={}&orderId={}".\
665-
format(get_settings()['paytm_sandbox_merchant'], order.id)
666+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
666667
else:
667668
url = "https://securegw.paytm.in/theia/api/v1/initiateTransaction?mid={}&orderId={}".\
668-
format(get_settings()['paytm_sandbox_merchant'], order.id)
669+
format(get_settings()['paytm_live_merchant'], order_identifier)
669670
response = requests.post(url, data=post_data, headers={"Content-type": "application/json"})
670671
return response.json()
672+
673+
674+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/fetch-payment-options/<string:txn_token>')
675+
@jwt_required
676+
def fetch_payment_options(order_identifier, txn_token):
677+
paytm_mode = get_settings()['paytm_mode']
678+
if paytm_mode == 'test':
679+
url = "https://securegw-stage.paytm.in/theia/api/v1/fetchPaymentOptions?mid={}&orderId={}".\
680+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
681+
else:
682+
url = "https://securegw.paytm.in/theia/api/v1/fetchPaymentOptions?mid={}&orderId={}".\
683+
format(get_settings()['paytm_live_merchant'], order_identifier)
684+
head = {
685+
"clientId": "C11",
686+
"version": "v1",
687+
"requestTimestamp": str(int(time.time())),
688+
"channelId": "WEB",
689+
"txnToken": txn_token
690+
}
691+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head)
692+
return response
693+
694+
695+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/send_otp/<string:txn_token>', methods=['POST'])
696+
@jwt_required
697+
def send_otp(order_identifier, txn_token):
698+
paytm_mode = get_settings()['paytm_mode']
699+
if paytm_mode == 'test':
700+
url = "https://securegw-stage.paytm.in/theia/api/v1/login/sendOtp?mid={}&orderId={}".\
701+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
702+
else:
703+
url = "https://securegw.paytm.in/theia/api/v1/login/sendOtp?mid={}&orderId={}".\
704+
format(get_settings()['paytm_live_merchant'], order_identifier)
705+
706+
head = {
707+
"clientId": "C11",
708+
"version": "v1",
709+
"requestTimestamp": str(int(time.time())),
710+
"channelId": "WEB",
711+
"txnToken": txn_token
712+
}
713+
body = {"mobileNumber": request.json['data']['phone']}
714+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
715+
return response
716+
717+
718+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/validate_otp/<string:txn_token>', methods=['POST'])
719+
def validate_otp(order_identifier, txn_token):
720+
paytm_mode = get_settings()['paytm_mode']
721+
if paytm_mode == 'test':
722+
url = "https://securegw-stage.paytm.in/theia/api/v1/login/validateOtp?mid={}&orderId={}".\
723+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
724+
else:
725+
url = "https://securegw.paytm.in/theia/api/v1/login/validateOtp?mid={}&orderId={}".\
726+
format(get_settings()['paytm_live_merchant'], order_identifier)
727+
head = {
728+
"clientId": "C11",
729+
"version": "v1",
730+
"requestTimestamp": str(int(time.time())),
731+
"channelId": "WEB",
732+
"txnToken": txn_token
733+
}
734+
body = {"otp": request.json['data']['otp']}
735+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
736+
return response
737+
738+
739+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/process_transaction/<string:txn_token>')
740+
@jwt_required
741+
def process_transaction(order_identifier, txn_token):
742+
paytm_mode = get_settings()['paytm_mode']
743+
merchant_id = (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'test'
744+
else get_settings()['paytm_live_merchant'])
745+
746+
if paytm_mode == 'test':
747+
url = "https://securegw-stage.paytm.in/theia/api/v1/processTransaction?mid={}&orderId={}".\
748+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
749+
else:
750+
url = "https://securegw.paytm.in/theia/api/v1/processTransaction?mid={}&orderId={}".\
751+
format(get_settings()['paytm_live_merchant'], order_identifier)
752+
753+
head = {
754+
"version": "v1",
755+
"requestTimestamp": str(int(time.time())),
756+
"channelId": "WEB",
757+
"txnToken": txn_token
758+
}
759+
760+
body = {
761+
"requestType": "NATIVE",
762+
"mid": merchant_id,
763+
"orderId": order_identifier,
764+
"paymentMode": "BALANCE"
765+
}
766+
767+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
768+
return response

0 commit comments

Comments
 (0)