Skip to content

Commit 4a49ff5

Browse files
Merge pull request #94 from PaystackOSS/4.0.1-fixes
Fixes up to 4.0.4
2 parents 5550f58 + 4d82424 commit 4a49ff5

23 files changed

+1076
-697
lines changed

assets/css/pff-paystack.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@
617617
/* Inputs
618618
=============================== */
619619
.j-forms input[type="text"],
620+
.j-forms input[type="number"],
620621
.j-forms input[type="password"],
621622
.j-forms input[type="email"],
622623
.j-forms input[type="search"],
@@ -650,6 +651,7 @@
650651
transition:all.4s;
651652
}
652653
.j-forms input[type="text"]:hover,
654+
.j-forms input[type="number"]:hover,
653655
.j-forms input[type="password"]:hover,
654656
.j-forms input[type="email"]:hover,
655657
.j-forms input[type="search"]:hover,
@@ -658,6 +660,7 @@
658660
.j-forms select:hover { border:2px solid #3676C8; }
659661

660662
.j-forms input[type="text"]:focus,
663+
.j-forms input[type="number"]:focus,
661664
.j-forms input[type="password"]:focus,
662665
.j-forms input[type="email"]:focus,
663666
.j-forms input[type="search"]:focus,

assets/js/paystack-public.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ function PffPaystackFee()
164164
}
165165
}
166166
);
167+
167168
if ($("#pf-quantity").length) {
169+
$( "#pf-quantity" ).on( 'change', function(event){
170+
checkMinimumVal();
171+
} );
168172
calculateTotal();
169173
};
170174

@@ -202,7 +206,6 @@ function PffPaystackFee()
202206
.find("#pf-amount")
203207
.val();
204208
}
205-
206209

207210
if (Number(amount) > 0) {
208211
} else {
@@ -225,6 +228,7 @@ function PffPaystackFee()
225228
);
226229
return false;
227230
}
231+
228232
if (checkMinimumVal() == false) {
229233
$(this)
230234
.find("#pf-amount")
@@ -593,11 +597,18 @@ function PffPaystackFee()
593597
}
594598
);
595599

596-
597600
function checkMinimumVal() {
598-
if ($("#pf-minimum-hidden").length) {
599-
var min_amount = Number($("#pf-minimum-hidden").val());
601+
if ( $("#pf-amount").length ) {
602+
var min_amount = Number($("#pf-amount").attr('min'));
600603
var amt = Number($("#pf-amount").val());
604+
var quantity = 1;
605+
606+
if ( $("#pf-quantity").length ) {
607+
quantity = $("#pf-quantity").val();
608+
}
609+
610+
amt = amt * quantity;
611+
601612
if (min_amount > 0 && amt < min_amount) {
602613
$("#pf-min-val-warn").text( "Amount cannot be less than the minimum amount");
603614
return false;
@@ -640,12 +651,14 @@ function PffPaystackFee()
640651

641652
function calculateTotal() {
642653
var unit;
654+
643655
if ($("#pf-vamount").length) {
644656
unit = $("#pf-vamount").val();
645657
} else {
646658
unit = $("#pf-amount").val();
647659
}
648660
var quant = $("#pf-quantity").val();
661+
649662
var newvalue = unit * quant;
650663

651664
if (quant == "" || quant == null) {

includes/classes/class-activation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public static function create_tables( $table_name ) {
6666
public static function maybe_upgrade( $table_name ) {
6767
global $wpdb;
6868

69+
$table_name = esc_sql( $table_name );
70+
6971
// Get the current version number, defaults to 1.0
7072
$version = get_option( 'kkd_db_version', '1.0' );
7173

@@ -87,7 +89,7 @@ public static function maybe_upgrade( $table_name ) {
8789
$wpdb->query(
8890
$wpdb->prepare(
8991
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
90-
"ALTER TABLE %i ADD `plan` VARCHAR(255) NOT NULL AFTER `paid`;",
92+
"ALTER TABLE `%s` ADD `plan` VARCHAR(255) NOT NULL AFTER `paid`;",
9193
$table_name
9294
)
9395
);
@@ -109,7 +111,7 @@ public static function maybe_upgrade( $table_name ) {
109111
$wpdb->query(
110112
$wpdb->prepare(
111113
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
112-
"ALTER TABLE %i ADD `txn_code_2` VARCHAR(255) DEFAULT '' NULL AFTER `txn_code`;",
114+
"ALTER TABLE `%s` ADD `txn_code_2` VARCHAR(255) DEFAULT '' NULL AFTER `txn_code`;",
113115
$table_name
114116
)
115117
);
@@ -131,7 +133,7 @@ public static function maybe_upgrade( $table_name ) {
131133
$wpdb->query(
132134
$wpdb->prepare(
133135
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
134-
"ALTER TABLE %i ADD `paid_at` timestamp AFTER `created_at`;",
136+
"ALTER TABLE `%s` ADD `paid_at` timestamp AFTER `created_at`;",
135137
$table_name
136138
)
137139
);

includes/classes/class-confirm-payment.php

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Confirm_Payment {
3838
protected $transaction = false;
3939

4040
/**
41-
* Holds the current payment meta retrieved from the DB.
41+
* Holds the verified payment meta from the DB
4242
*
4343
* @var object
4444
*/
@@ -65,6 +65,13 @@ class Confirm_Payment {
6565
*/
6666
protected $oamount = 0;
6767

68+
/**
69+
* The quantity bought.
70+
*
71+
* @var integer
72+
*/
73+
protected $quantity = 1;
74+
6875
/**
6976
* The transaction column to update.
7077
* Defaults to 'txn_code' and 'txn_code_2' when a payment retry is triggered.
@@ -73,6 +80,14 @@ class Confirm_Payment {
7380
*/
7481
protected $txn_column = 'txn_code';
7582

83+
/**
84+
* The transaction reference
85+
* Defaults to the 'txn_code' and 'txn_code_2' when a payment retry is triggered.
86+
*
87+
* @var integer
88+
*/
89+
protected $reference = '';
90+
7691
/**
7792
* Constructor
7893
*/
@@ -89,12 +104,12 @@ public function __construct() {
89104
protected function setup_data( $payment ) {
90105
$this->payment_meta = $payment;
91106
$this->meta = $this->helpers->parse_meta_values( get_post( $this->payment_meta->post_id ) );
92-
$this->amount = $this->payment_meta->amount;
93-
$this->oamount = $this->meta['amount'];
94107
$this->form_id = $this->payment_meta->post_id;
95-
96-
if ( 'customer' === $this->meta['txncharge'] ) {
97-
$this->oamount = $this->helpers->process_transaction_fees( $this->oamount );
108+
$this->amount = $this->payment_meta->amount;
109+
$this->oamount = $this->amount;
110+
$this->reference = $this->payment_meta->txn_code;
111+
if ( isset( $this->payment_meta->txn_code_2 ) && ! empty( $this->payment_meta->txn_code_2 ) ) {
112+
$this->reference = $this->payment_meta->txn_code_2;
98113
}
99114
}
100115

@@ -106,7 +121,7 @@ public function confirm_payment() {
106121
if ( ! isset( $_POST['nonce'] ) || false === wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'pff-paystack-confirm' ) ) {
107122
$response = array(
108123
'error' => true,
109-
'error_message' => __( 'Nonce verification is required.', 'pff-paystack' ),
124+
'error_message' => esc_html__( 'Nonce verification is required.', 'pff-paystack' ),
110125
);
111126

112127
exit( wp_json_encode( $response ) );
@@ -117,16 +132,23 @@ public function confirm_payment() {
117132
if ( ! isset( $_POST['code'] ) || '' === trim( wp_unslash( $_POST['code'] ) ) ) {
118133
$response = array(
119134
'error' => true,
120-
'error_message' => __( 'Did you make a payment?', 'pff-paystack' ),
135+
'error_message' => esc_html__( 'Did you make a payment?', 'pff-paystack' ),
121136
);
122137

123138
exit( wp_json_encode( $response ) );
124139
}
125140

126141
// If this is a retry payment then set the colum accordingly.
142+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
127143
if ( isset( $_POST['retry'] ) ) {
128144
$this->txn_column = 'txn_code_2';
129145
}
146+
147+
// This is a false positive, we are using isset as WPCS suggest in the PCP plugin.
148+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
149+
if ( isset( $_POST['quantity'] ) ) {
150+
$this->quantity = sanitize_text_field( wp_unslash( $_POST['quantity'] ) );
151+
}
130152

131153
$this->helpers = new Helpers();
132154
$code = sanitize_text_field( wp_unslash( $_POST['code'] ) );
@@ -147,62 +169,105 @@ public function confirm_payment() {
147169
}
148170
} else {
149171
$response = [
150-
'message' => __( 'Failed to connect to Paystack.', 'pff-paystack' ),
172+
'message' => esc_html__( 'Failed to connect to Paystack.', 'pff-paystack' ),
151173
'result' => 'failed',
152174
];
153175
}
154176

155177
} else {
156178
$response = [
157-
'message' => __( 'Payment Verification Failed', 'pff-paystack' ),
179+
'message' => esc_html__( 'Payment Verification Failed', 'pff-paystack' ),
158180
'result' => 'failed',
159181
];
160182
}
161-
162183

163184
// Create plan and send reciept.
164185
if ( 'success' === $response['result'] ) {
165186

166187
// Create a plan that the user will be subscribed to.
167-
168-
/*$pstk_logger = new kkd_pff_paystack_plugin_tracker( 'pff-paystack', Kkd_Pff_Paystack_Public::fetchPublicKey() );
169-
$pstk_logger->log_transaction_success( $code );*/
170-
171188
$this->maybe_create_subscription();
172189

173-
174190
$sendreceipt = $this->meta['sendreceipt'];
175-
if ( 'yes' === $sendreceipt ) {
176-
$decoded = json_decode( $this->payment_meta->metadata );
177-
$fullname = $decoded[1]->value;
191+
$decoded = json_decode( $this->payment_meta->metadata );
192+
$fullname = $decoded[1]->value;
178193

194+
if ( 'yes' === $sendreceipt ) {
179195
/**
180196
* Allow 3rd Party Plugins to hook into the email sending.
181197
*
182198
* 10: Email_Receipt::send_receipt();
183199
* 11: Email_Receipt_Owner::send_receipt_owner();
184200
*/
201+
185202
do_action( 'pff_paystack_send_receipt',
186203
$this->payment_meta->post_id,
187204
$this->payment_meta->currency,
188-
$this->payment_meta->amount_paid,
205+
$this->payment_meta->amount,
189206
$fullname,
190207
$this->payment_meta->email,
191-
$this->payment_meta->reference,
208+
$this->reference,
209+
$this->payment_meta->metadata
210+
);
211+
212+
/**
213+
* Allow 3rd Party Plugins to hook into the email sending.
214+
* 11: Email_Receipt_Owner::send_receipt_owner();
215+
*/
216+
217+
do_action( 'pff_paystack_send_receipt_owner',
218+
$this->payment_meta->post_id,
219+
$this->payment_meta->currency,
220+
$this->payment_meta->amount,
221+
$fullname,
222+
$this->payment_meta->email,
223+
$this->reference,
192224
$this->payment_meta->metadata
193225
);
194226
}
195227
}
196228

197229
if ( 'success' === $response['result'] && '' !== $this->meta['redirect'] ) {
198230
$response['result'] = 'success2';
199-
$response['link'] = $this->meta['redirect'];
231+
$response['link'] = $this->add_param_to_url( $this->meta['redirect'], $this->reference );
200232
}
201233

202234
echo wp_json_encode( $response );
203235
die();
204236
}
205237

238+
/**
239+
* Adds parameters to a URL.
240+
*
241+
* @param string $url The original URL.
242+
* @param string $ref The reference value to add as a parameter.
243+
* @return string The modified URL with added parameters.
244+
*/
245+
public function add_param_to_url( $url, $ref ) {
246+
// Parse the URL.
247+
$parsed_url = wp_parse_url( $url );
248+
249+
// Parse query parameters into an array.
250+
parse_str( isset( $parsed_url['query'] ) ? $parsed_url['query'] : '', $query_params );
251+
252+
// Add the "trxref" and "reference" parameters to the query parameters.
253+
$query_params['trxref'] = $ref;
254+
$query_params['reference'] = $ref;
255+
256+
// Rebuild the query string.
257+
$query_string = http_build_query( $query_params );
258+
259+
// Construct the new URL.
260+
$new_url = ( isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '' );
261+
$new_url .= ( isset( $parsed_url['user'] ) ? $parsed_url['user'] . ( isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : '' ) . '@' : '' );
262+
$new_url .= ( isset( $parsed_url['host'] ) ? $parsed_url['host'] : '' );
263+
$new_url .= ( isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '' );
264+
$new_url .= ( isset( $parsed_url['path'] ) ? $parsed_url['path'] : '' );
265+
$new_url .= ( ! empty( $query_string ) ? '?' . $query_string : '' );
266+
$new_url .= ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
267+
268+
return $new_url;
269+
}
270+
206271
/**
207272
* Update the sold invetory with the amount of payments made.
208273
*
@@ -220,10 +285,10 @@ protected function update_sold_inventory() {
220285
// phpcs:ignore WordPress.Security.NonceVerification
221286
$quantity = (int) sanitize_text_field( wp_unslash( $_POST['quantity'] ) );
222287
}
223-
$sold = $this->meta['sold'];
288+
$sold = $this->meta['sold'];
224289

225290
if ( '' === $sold ) {
226-
$sold = '0';
291+
$sold = 0;
227292
}
228293
$sold += $quantity;
229294
} else {
@@ -247,7 +312,7 @@ protected function update_payment_dates( $data ) {
247312
global $wpdb;
248313
$table = $wpdb->prefix . PFF_PAYSTACK_TABLE;
249314
$return = [
250-
'message' => __( 'DB not updated.', 'pff-paystack' ),
315+
'message' => esc_html__( 'DB not updated.', 'pff-paystack' ),
251316
'result' => 'failed',
252317
];
253318

@@ -287,10 +352,10 @@ protected function update_payment_dates( $data ) {
287352
'result' => 'success',
288353
];
289354
} else {
290-
if ( $this->oamount !== $amount_paid ) {
355+
if ( (int) $this->oamount !== (int) $amount_paid ) {
291356
$return = [
292357
// translators: %1$s: currency, %2$s: formatted amount required
293-
'message' => sprintf( __( 'Invalid amount Paid. Amount required is %1$s<b>%2$s</b>', 'pff-paystack' ), $this->meta['currency'], number_format( $this->oamount ) ),
358+
'message' => sprintf( esc_html__( 'Invalid amount Paid. Amount required is %1$s<b>%2$s</b>', 'pff-paystack' ), $this->meta['currency'], number_format( $this->oamount ) ),
294359
'result' => 'failed',
295360
];
296361
} else {

includes/classes/class-email-invoice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function send_invoice( $form_id, $currency, $amount, $name, $email, $code
6060

6161
$this->subject = sprintf(
6262
// Translators: %1$s is the currency code, %2$s is the formatted amount
63-
__( 'Payment Invoice for %1$s %2$s', 'text-domain' ),
63+
esc_html__( 'Payment Invoice for %1$s %2$s', 'text-domain' ),
6464
$currency,
6565
number_format( $amount )
6666
);

0 commit comments

Comments
 (0)