Skip to content

Commit a0d2b20

Browse files
Merge pull request #59 from PaystackOSS/patch/webview-guide
update webview guide code snippet
2 parents df8dc95 + 17a8a67 commit a0d2b20

File tree

8 files changed

+69
-17
lines changed

8 files changed

+69
-17
lines changed

dist/doc/guides/checkout-webview/handling-redirect.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ Widget build(BuildContext context) {
55
initialUrl: 'https://checkout.paystack.com/7zu1ot06d0qn9h6',
66
javascriptMode: JavascriptMode.unrestricted,
77
userAgent: 'Flutter;Webview',
8-
navigationDelegate: (navigation){
9-
//Listen for callback URL
10-
if(navigation.url == "https://hello.pstk.xyz/callback"){
8+
navigationDelegate: (navigation) {
9+
//Listen for callback URL
10+
if (navigation.url == "https://hello.pstk.xyz/callback") {
1111
verifyTransaction(reference);
1212
Navigator.of(context).pop(); //close webview
1313
}
14+
if (navigation.url == "https://your-cancel-url.com") {
15+
//handle webview removal
16+
Navigator.of(context).pop(); //close webview
17+
//Run the cancel payment function if you have one
18+
}
1419
return NavigationDecision.navigate;
1520
},
1621
),
1722
);
18-
}`
23+
}
24+
`
1925

2026
const js = `import React from 'react';
2127
import { WebView } from 'react-native-webview';
@@ -25,6 +31,7 @@ export default function App() {
2531
2632
const authorization_url = 'https://checkout.paystack.com/luKuasMan';
2733
const callback_url = 'https://yourcallback.com';
34+
const cancel_url = "https://your-cancel-url.com";
2835
2936
onNavigationStateChange = state => {
3037
@@ -37,6 +44,12 @@ export default function App() {
3744
const redirectTo = 'window.location = "' + callback_url + '"';
3845
this.webview.injectJavaScript(redirectTo);
3946
}
47+
if (url === cancel_url) {
48+
// handle webview removal
49+
// You can either unmount the component, or
50+
// Use a navigator to pop off the view
51+
// Run the cancel payment function if you have one
52+
}
4053
};
4154
4255
return (
@@ -46,15 +59,16 @@ export default function App() {
4659
onNavigationStateChange={ this.onNavigationStateChange }
4760
/>
4861
);
49-
}
50-
`
62+
}`
5163

5264
const kt = `class MainActivity : AppCompatActivity() {
5365
5466
private val authorizationUrl: String
5567
get() = "https://checkout.paystack.com/ok62i2sdld514e4"
5668
private val callbackUrl: String
5769
get() = "https://yourcallback.com"
70+
private val cancelUrl: String
71+
get() = "https://your-cancel-url.com"
5872
5973
override fun onCreate(savedInstanceState: Bundle?) {
6074
// ...
@@ -76,6 +90,11 @@ const kt = `class MainActivity : AppCompatActivity() {
7690
if (url?.host == callbackUrl) {
7791
return true
7892
}
93+
if (url?.host == cancelUrl) {
94+
// handle webview removal
95+
// Run the cancel payment function if you have one
96+
return true
97+
}
7998
8099
return super.shouldOverrideUrlLoading(view, request)
81100
}

dist/doc/payments/accept-payment/redirect-backend.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const sh = `curl https://api.paystack.co/transaction/initialize
22
-H "Authorization: Bearer YOUR_SECRET_KEY"
33
-H "Content-Type: application/json"
4-
-d '{ "email": "[email protected]", "amount": "20000" }'
4+
-d '{ "email": "[email protected]", "amount": "20000",
5+
"callback_url":"https://hello.pstk.xyz/callback",
6+
"metadata":{"cancel_action": "https://your-cancel-url.com"}
7+
}'
58
-X POST`
69

710
const js = `const https = require('https')
811
912
const params = JSON.stringify({
1013
"email": "[email protected]",
11-
"amount": "20000"
14+
"amount": "20000",
15+
"callback_url":"https://hello.pstk.xyz/callback",
16+
"metadata":{"cancel_action": "https://your-cancel-url.com"}
1217
})
1318
1419
const options = {
@@ -44,7 +49,9 @@ const php = `<?php
4449
4550
$fields = [
4651
'email' => "[email protected]",
47-
'amount' => "20000"
52+
'amount' => "20000",
53+
'callback_url' => "https://hello.pstk.xyz/callback",
54+
'metadata' => ["cancel_action" => "https://your-cancel-url.com"]
4855
];
4956
5057
$fields_string = http_build_query($fields);

src/doc/guides/checkout-webview/handling-redirect/index.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ Widget build(BuildContext context) {
55
initialUrl: 'https://checkout.paystack.com/7zu1ot06d0qn9h6',
66
javascriptMode: JavascriptMode.unrestricted,
77
userAgent: 'Flutter;Webview',
8-
navigationDelegate: (navigation){
9-
//Listen for callback URL
10-
if(navigation.url == "https://hello.pstk.xyz/callback"){
8+
navigationDelegate: (navigation) {
9+
//Listen for callback URL
10+
if (navigation.url == "https://hello.pstk.xyz/callback") {
1111
verifyTransaction(reference);
1212
Navigator.of(context).pop(); //close webview
1313
}
14+
if (navigation.url == "https://your-cancel-url.com") {
15+
//handle webview removal
16+
Navigator.of(context).pop(); //close webview
17+
//Run the cancel payment function if you have one
18+
}
1419
return NavigationDecision.navigate;
1520
},
1621
),
1722
);
18-
}
23+
}

src/doc/guides/checkout-webview/handling-redirect/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default function App() {
66

77
const authorization_url = 'https://checkout.paystack.com/luKuasMan';
88
const callback_url = 'https://yourcallback.com';
9+
const cancel_url = "https://your-cancel-url.com";
910

1011
onNavigationStateChange = state => {
1112

@@ -18,6 +19,12 @@ export default function App() {
1819
const redirectTo = 'window.location = "' + callback_url + '"';
1920
this.webview.injectJavaScript(redirectTo);
2021
}
22+
if (url === cancel_url) {
23+
// handle webview removal
24+
// You can either unmount the component, or
25+
// Use a navigator to pop off the view
26+
// Run the cancel payment function if you have one
27+
}
2128
};
2229

2330
return (
@@ -27,4 +34,4 @@ export default function App() {
2734
onNavigationStateChange={ this.onNavigationStateChange }
2835
/>
2936
);
30-
}
37+
}

src/doc/guides/checkout-webview/handling-redirect/index.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class MainActivity : AppCompatActivity() {
44
get() = "https://checkout.paystack.com/ok62i2sdld514e4"
55
private val callbackUrl: String
66
get() = "https://yourcallback.com"
7+
private val cancelUrl: String
8+
get() = "https://your-cancel-url.com"
79

810
override fun onCreate(savedInstanceState: Bundle?) {
911
// ...
@@ -25,6 +27,11 @@ class MainActivity : AppCompatActivity() {
2527
if (url?.host == callbackUrl) {
2628
return true
2729
}
30+
if (url?.host == cancelUrl) {
31+
// handle webview removal
32+
// Run the cancel payment function if you have one
33+
return true
34+
}
2835

2936
return super.shouldOverrideUrlLoading(view, request)
3037
}

src/doc/payments/accept-payment/redirect-backend/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const https = require('https')
22

33
const params = JSON.stringify({
44
"email": "[email protected]",
5-
"amount": "20000"
5+
"amount": "20000",
6+
"callback_url":"https://hello.pstk.xyz/callback",
7+
"metadata":{"cancel_action": "https://your-cancel-url.com"}
68
})
79

810
const options = {

src/doc/payments/accept-payment/redirect-backend/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
$fields = [
55
'email' => "[email protected]",
6-
'amount' => "20000"
6+
'amount' => "20000",
7+
'callback_url' => "https://hello.pstk.xyz/callback",
8+
'metadata' => ["cancel_action" => "https://your-cancel-url.com"]
79
];
810

911
$fields_string = http_build_query($fields);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
curl https://api.paystack.co/transaction/initialize
22
-H "Authorization: Bearer YOUR_SECRET_KEY"
33
-H "Content-Type: application/json"
4-
-d '{ "email": "[email protected]", "amount": "20000" }'
4+
-d '{ "email": "[email protected]", "amount": "20000",
5+
"callback_url":"https://hello.pstk.xyz/callback",
6+
"metadata":{"cancel_action": "https://your-cancel-url.com"}
7+
}'
58
-X POST

0 commit comments

Comments
 (0)