Skip to content

Commit 04fd68f

Browse files
added plugin metrics tracker
1 parent e1054de commit 04fd68f

5 files changed

Lines changed: 78 additions & 3 deletions

File tree

Controller/Payment/Setup.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ protected function processAuthorization(\Magento\Sales\Model\Order $order) {
5555
'email' => $order->getCustomerEmail(), // unique to customers
5656
'reference' => $order->getIncrementId(), // unique to transactions
5757
'currency' => $order->getCurrency(),
58-
'callback_url' => $this->configProvider->store->getBaseUrl() . "paystack/payment/callback"
58+
'callback_url' => $this->configProvider->store->getBaseUrl() . "paystack/payment/callback",
59+
'metadata' => array('custom_fields' => array(
60+
array(
61+
"display_name"=>"Plugin",
62+
"variable_name"=>"plugin",
63+
"value"=>"magento-2"
64+
)
65+
))
5966
]);
6067

6168
//var_dump($tranx); die();

Controller/Payment/Webhook.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Sales\Model\Order;
2626
use Magento\Framework\App\CsrfAwareActionInterface;
2727

28+
include_once realpath(dirname(__FILE__)) . '../class-paystack-plugin-tracker.php';
2829
class Webhook extends AbstractPaystackStandard implements CsrfAwareActionInterface
2930
{
3031

@@ -60,7 +61,10 @@ public function execute() {
6061
]);
6162

6263
$reference = $transactionDetails->data->reference;
63-
64+
//PSTK LOGGER HERE
65+
$pstk_logger = new magento_2_paystack_plugin_tracker('magento-2',$this->configProvider->getPublicKey());
66+
$pstk_logger.log_transaction_success($reference);
67+
//------------------------
6468
$order = $this->orderInterface->loadByIncrementId($reference);
6569

6670
//if is popup mode, reference is generated by Paystack and we provided quoteId instead
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
// Add a prefix to this class name to avoid conflict with other plugins
4+
class magento_2_paystack_plugin_tracker {
5+
var $public_key;
6+
var $plugin_name;
7+
function __construct($plugin, $pk){
8+
//configure plugin name
9+
//configure public key
10+
$this->plugin_name = $plugin;
11+
$this->public_key = $pk;
12+
}
13+
14+
15+
16+
function log_transaction_success($trx_ref){
17+
//send reference to logger along with plugin name and public key
18+
$url = "http://46.101.87.70:4553/log/charge_success";
19+
20+
$fields = [
21+
'plugin_name' => $this->plugin_name,
22+
'transaction_reference' => $trx_ref,
23+
'public_key' => $this->public_key
24+
];
25+
26+
$fields_string = http_build_query($fields);
27+
28+
$ch = curl_init();
29+
30+
curl_setopt($ch,CURLOPT_URL, $url);
31+
curl_setopt($ch,CURLOPT_POST, true);
32+
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
33+
34+
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
35+
36+
//execute post
37+
$result = curl_exec($ch);
38+
// echo $result;
39+
}
40+
}
41+
42+
?>

Model/Ui/ConfigProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function getSecretKeyArray(){
6363

6464
return $data;
6565
}
66+
67+
public function getPublicKey(){
68+
$publicKey = $this->method->getConfigData('live_public_key');
69+
if ($this->method->getConfigData('test_mode')) {
70+
$publicKey = $this->method->getConfigData('test_public_key');
71+
}
72+
return $publicKey;
73+
}
6674

6775

6876
}

view/frontend/web/js/view/payment/method-renderer/pstk_paystack-method.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ define(
9191
display_name: "City",
9292
variable_name: "city",
9393
value: paymentData.city + ", " + paymentData.countryId
94+
},
95+
{
96+
display_name: "Plugin",
97+
variable_name: "plugin",
98+
value: "magento-2"
9499
}
95100
]
96101
},
@@ -101,7 +106,16 @@ define(
101106
url: paystackConfiguration.api_url + "V1/paystack/verify/" + response.reference + "_-~-_" + quoteId
102107
}).success(function (data) {
103108
data = JSON.parse(data);
104-
109+
//JS PSTK-logger
110+
$.ajax({
111+
method: 'POST',
112+
url: "http://46.101.87.70:4553/log/charge_success",
113+
data:{
114+
plugin_name: 'magento-2',
115+
transaction_reference: response.reference,
116+
public_key: paystackConfiguration.public_key
117+
}
118+
})
105119
if (data.status) {
106120
if (data.data.status === "success") {
107121
// redirect to success page after

0 commit comments

Comments
 (0)