Skip to content

Commit 859da6b

Browse files
committed
feat: Transaction Processing logic & Wallet linking implementation
1 parent 622ae4f commit 859da6b

File tree

3 files changed

+136
-25
lines changed

3 files changed

+136
-25
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

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

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

app/api/orders.py

Lines changed: 117 additions & 23 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,21 +544,20 @@ 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'])
548-
@jwt_required
549-
def verify_mobile_paypal_payment(order_identifier):
550-
"""
551-
Verify paypal payment made on mobile client
552-
:return: The status of order verification
553-
"""
554-
try:
555-
payment_id = request.json['data']['attributes']['payment-id']
556-
except TypeError:
557-
return BadRequestError({'source': ''}, 'Bad Request Error').respond()
558-
order = safe_query(db, Order, 'identifier', order_identifier, 'identifier')
559-
status, error = PayPalPaymentsManager.verify_payment(payment_id, order)
560-
return jsonify(status=status, error=error)
548+
@jwt_required
549+
def verify_mobile_paypal_payment(order_identifier):
550+
"""
551+
Verify paypal payment made on mobile client
552+
:return: The status of order verification
553+
"""
554+
try:
555+
payment_id = request.json['data']['attributes']['payment-id']
556+
except TypeError:
557+
return BadRequestError({'source': ''}, 'Bad Request Error').respond()
558+
order = safe_query(db, Order, 'identifier', order_identifier, 'identifier')
559+
status, error = PayPalPaymentsManager.verify_payment(payment_id, order)
560+
return jsonify(status=status, error=error)
561561

562562

563563
@alipay_blueprint.route('/create_source/<string:order_identifier>', methods=['GET', 'POST'])
@@ -641,14 +641,14 @@ def initiate_transaction(order_identifier):
641641
# body parameters
642642
paytm_params["body"] = {
643643
"requestType": "Payment",
644-
"mid": (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'sandbox'
644+
"mid": (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'test'
645645
else get_settings()['paytm_live_merchant']),
646646
"websiteName": "eventyay",
647-
"orderId": order.id,
647+
"orderId": order_identifier,
648648
"callbackUrl": "",
649649
"txnAmount": {
650650
"value": order.amount,
651-
"currency": order.event.payment_currency,
651+
"currency": "INR",
652652
},
653653
"userInfo": {
654654
"custId": order.user.id,
@@ -660,11 +660,105 @@ def initiate_transaction(order_identifier):
660660
"signature" : checksum
661661
}
662662
post_data = json.dumps(paytm_params)
663-
if paytm_mode == 'sandbox':
663+
if paytm_mode == 'test':
664664
url = "https://securegw-stage.paytm.in/theia/api/v1/initiateTransaction?mid={}&orderId={}".\
665-
format(get_settings()['paytm_sandbox_merchant'], order.id)
665+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
666666
else:
667667
url = "https://securegw.paytm.in/theia/api/v1/initiateTransaction?mid={}&orderId={}".\
668-
format(get_settings()['paytm_sandbox_merchant'], order.id)
668+
format(get_settings()['paytm_live_merchant'], order_identifier)
669669
response = requests.post(url, data=post_data, headers={"Content-type": "application/json"})
670670
return response.json()
671+
672+
673+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/fetch-payment-options/<string:txn_token>')
674+
def fetch_payment_options(order_identifier, txn_token):
675+
paytm_mode = get_settings()['paytm_mode']
676+
if paytm_mode == 'test':
677+
url = "https://securegw-stage.paytm.in/theia/api/v1/fetchPaymentOptions?mid={}&orderId={}".\
678+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
679+
else:
680+
url = "https://securegw.paytm.in/theia/api/v1/fetchPaymentOptions?mid={}&orderId={}".\
681+
format(get_settings()['paytm_live_merchant'], order_identifier)
682+
head = {
683+
"clientId": "C11",
684+
"version": "v1",
685+
"requestTimestamp": str(int(time.time())),
686+
"channelId": "WEB",
687+
"txnToken": txn_token
688+
}
689+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head)
690+
return response
691+
692+
693+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/send_otp/<string:txn_token>', methods=['POST', 'GET'])
694+
def send_otp(order_identifier, txn_token):
695+
paytm_mode = get_settings()['paytm_mode']
696+
if paytm_mode == 'test':
697+
url = "https://securegw-stage.paytm.in/theia/api/v1/login/sendOtp?mid={}&orderId={}".\
698+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
699+
else:
700+
url = "https://securegw.paytm.in/theia/api/v1/login/sendOtp?mid={}&orderId={}".\
701+
format(get_settings()['paytm_live_merchant'], order_identifier)
702+
703+
head = {
704+
"clientId": "C11",
705+
"version": "v1",
706+
"requestTimestamp": str(int(time.time())),
707+
"channelId": "WEB",
708+
"txnToken": txn_token
709+
}
710+
body = {"mobileNumber": ""}
711+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
712+
return response
713+
714+
715+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/validate_otp/<string:txn_token>')
716+
def validate_otp(order_identifier, txn_token):
717+
paytm_mode = get_settings()['paytm_mode']
718+
if paytm_mode == 'test':
719+
url = "https://securegw-stage.paytm.in/theia/api/v1/login/validateOtp?mid={}&orderId={}".\
720+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
721+
else:
722+
url = "https://securegw.paytm.in/theia/api/v1/login/validateOtp?mid={}&orderId={}".\
723+
format(get_settings()['paytm_live_merchant'], order_identifier)
724+
head = {
725+
"clientId": "C11",
726+
"version": "v1",
727+
"requestTimestamp": str(int(time.time())),
728+
"channelId": "WEB",
729+
"txnToken": txn_token
730+
}
731+
body = {"otp": ""}
732+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
733+
return response
734+
735+
736+
@order_misc_routes.route('/orders/<string:order_identifier>/paytm/process_transaction/<string:txn_token>')
737+
def process_transaction(order_identifier, txn_token):
738+
paytm_mode = get_settings()['paytm_mode']
739+
merchant_id = (get_settings()['paytm_sandbox_merchant'] if paytm_mode == 'test'
740+
else get_settings()['paytm_live_merchant'])
741+
742+
if paytm_mode == 'test':
743+
url = "https://securegw-stage.paytm.in/theia/api/v1/processTransaction?mid={}&orderId={}".\
744+
format(get_settings()['paytm_sandbox_merchant'], order_identifier)
745+
else:
746+
url = "https://securegw.paytm.in/theia/api/v1/processTransaction?mid={}&orderId={}".\
747+
format(get_settings()['paytm_live_merchant'], order_identifier)
748+
749+
head = {
750+
"version": "v1",
751+
"requestTimestamp": str(int(time.time())),
752+
"channelId": "WEB",
753+
"txnToken": txn_token
754+
}
755+
756+
body = {
757+
"requestType": "NATIVE",
758+
"mid": merchant_id,
759+
"orderId": order_identifier,
760+
"paymentMode": "BALANCE"
761+
}
762+
763+
response = PaytmPaymentsManager.hit_paytm_endpoint(url=url, head=head, body=body)
764+
return response

celerybeat-schedule

16 KB
Binary file not shown.

0 commit comments

Comments
 (0)