Skip to content

Commit 65f6120

Browse files
authored
Merge pull request #4 from TechTailor/dev
Adding Option for Redirect
2 parents d73f091 + fdc5970 commit 65f6120

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ Start by adding the `@paytmScripts` tag into the `<head>` tag of your page. For
103103
@paytmScripts
104104
</head>
105105
```
106-
Next, to initiate the Checkout Payment Page, you need to callout `openJsCheckoutPopup(orderId, txnToken, amount)` and pass it the `orderId`, `txntoken` and the `amount` received in **Step # 1**.
106+
107+
Next, to initiate the Checkout Payment Page, you have 2 available methods -
108+
109+
##### Method # 1
110+
111+
This method will invoke the payment gateway and upon completion, will redirect the user to the callback url set earlier (or in the config file).
112+
113+
To achieve that, you need to call the `openJsCheckoutPopup(orderId, txnToken, amount)` function and pass it the `orderId`, `txntoken` and the `amount` received in **Step # 1**.
107114

108115
```php
109116
// Somewhere in your page
@@ -122,8 +129,36 @@ Next, to initiate the Checkout Payment Page, you need to callout `openJsCheckout
122129
```
123130
Upon clicking the `Pay Now` button, a pop-up for the Paytm PG will open with all the options to make the payment. Once the payment is complete, you will be redirected to the `callback_url` set in the .env file as `PAYTM_CALLBACK_URL`.
124131

125-
### Step # 3 - Receiving Callback
126-
The **callback** from the Paytm PG will provide an array containing the following (for a successful transaction) -
132+
##### Method # 2
133+
134+
This method will allow you to handle the response on the same page (and ignore any callback urls set for this transaction). Useful when you want to process transaction without a redirect.
135+
136+
```php
137+
// Somewhere in your page
138+
<button type="button" id="JsCheckoutPayment" name="submit" class="btn btn-primary">Pay Now</button>
139+
140+
// Before the closing </body> tag
141+
<script type="application/javascript">
142+
document.getElementById("JsCheckoutPayment").addEventListener("click", function() {
143+
var orderId = "{{ $response['orderId'] }}";
144+
var txnToken = "{{ $response['txnToken'] }}";
145+
var amount = "{{ $response['amount'] }}";
146+
// Pass an additional "false" attribute which marks redirect as false.
147+
openJsCheckoutPopup(orderId, txnToken, amount, false);
148+
}
149+
);
150+
151+
// To be executed upon completion of the payment (only if false is passed above).
152+
function paymentCompleted(paymentStatus) {
153+
window.Paytm.CheckoutJS.close(); // Close the Paytm PG Pop-up.
154+
console.log(paymentStatus); // Log or use the payment status/details returned.
155+
}
156+
</script>
157+
```
158+
Once you set the redirect flag as false, you can use the `paymentCompleted` function to excute further queries using the returned data.
159+
160+
### Step # 3 - Receiving Response
161+
The **response** from the Paytm PG (via callback or same page) will provide an array containing the following (for a successful transaction) -
127162
```php
128163
array:14 [▼
129164
"BANKNAME" => "State Bank of India"

resources/dist/js/paytm.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function openJsCheckoutPopup(orderId, txnToken, amount) {
1+
function openJsCheckoutPopup(orderId, txnToken, amount, redirect) {
22
var config = {
33
"root": "",
44
"flow": "DEFAULT",
@@ -9,13 +9,15 @@ function openJsCheckoutPopup(orderId, txnToken, amount) {
99
"amount": amount
1010
},
1111
"merchant": {
12-
"redirect": true
12+
"redirect": redirect ?? true
1313
},
1414
"handler": {
15-
"notifyMerchant": function (eventName, data) {
16-
console.log("notifyMerchant handler function called");
15+
notifyMerchant: function notifyMerchant(eventName, data) {
1716
console.log("eventName => ", eventName);
18-
console.log("data => ", data);
17+
// console.log("data => ", data); // only enable for debugging
18+
},
19+
transactionStatus: function transactionStatus(paymentStatus) {
20+
paymentCompleted(paymentStatus); // only called when redirect is set to false
1921
}
2022
}
2123
};

0 commit comments

Comments
 (0)