Skip to content

Commit

Permalink
update webview guide code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
lukman-paystack committed Feb 22, 2024
1 parent df8dc95 commit c049f8f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/doc/guides/checkout-webview/handling-redirect/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ Widget build(BuildContext context) {
initialUrl: 'https://checkout.paystack.com/7zu1ot06d0qn9h6',
javascriptMode: JavascriptMode.unrestricted,
userAgent: 'Flutter;Webview',
navigationDelegate: (navigation){
//Listen for callback URL
if(navigation.url == "https://hello.pstk.xyz/callback"){
navigationDelegate: (navigation) {
//Listen for callback URL
if (navigation.url == "https://hello.pstk.xyz/callback") {
verifyTransaction(reference);
Navigator.of(context).pop(); //close webview
}
if (navigation.url == "https://your-cancel-url.com") {
//handle webview removal
Navigator.of(context).pop(); //close webview
//Run the cancel payment function if you have one
}
return NavigationDecision.navigate;
},
),
);
}
}
9 changes: 8 additions & 1 deletion src/doc/guides/checkout-webview/handling-redirect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function App() {

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

onNavigationStateChange = state => {

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

return (
Expand All @@ -27,4 +34,4 @@ export default function App() {
onNavigationStateChange={ this.onNavigationStateChange }
/>
);
}
}
7 changes: 7 additions & 0 deletions src/doc/guides/checkout-webview/handling-redirect/index.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MainActivity : AppCompatActivity() {
get() = "https://checkout.paystack.com/ok62i2sdld514e4"
private val callbackUrl: String
get() = "https://yourcallback.com"
private val cancelUrl: String
get() = "https://your-cancel-url.com"

override fun onCreate(savedInstanceState: Bundle?) {
// ...
Expand All @@ -25,6 +27,11 @@ class MainActivity : AppCompatActivity() {
if (url?.host == callbackUrl) {
return true
}
if (url?.host == cancelUrl) {
// handle webview removal
// Run the cancel payment function if you have one
return true
}

return super.shouldOverrideUrlLoading(view, request)
}
Expand Down
4 changes: 3 additions & 1 deletion src/doc/payments/accept-payment/redirect-backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const https = require('https')

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

const options = {
Expand Down
4 changes: 3 additions & 1 deletion src/doc/payments/accept-payment/redirect-backend/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

$fields = [
'email' => "[email protected]",
'amount' => "20000"
'amount' => "20000",
'callback_url' => "https://hello.pstk.xyz/callback",
'metadata' => ["cancel_action" => "https://your-cancel-url.com"]
];

$fields_string = http_build_query($fields);
Expand Down
5 changes: 4 additions & 1 deletion src/doc/payments/accept-payment/redirect-backend/index.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
curl https://api.paystack.co/transaction/initialize
-H "Authorization: Bearer YOUR_SECRET_KEY"
-H "Content-Type: application/json"
-d '{ "email": "[email protected]", "amount": "20000" }'
-d '{ "email": "[email protected]", "amount": "20000",
"callback_url":"https://hello.pstk.xyz/callback",
"metadata":{"cancel_action": "https://your-cancel-url.com"}
}'
-X POST

0 comments on commit c049f8f

Please sign in to comment.