@@ -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 {
0 commit comments