Skip to content

Commit 8ac961d

Browse files
committed
Updating the confirm payment to allowing the checking of the retry action as well.
1 parent 9147ab6 commit 8ac961d

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

includes/classes/class-confirm-payment.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Confirm_Payment {
1919
/**
2020
* The helpers class.
2121
*
22-
* @var object
22+
* @var \paystack\payment_forms\Helpers
2323
*/
2424
public $helpers;
2525

@@ -65,6 +65,14 @@ class Confirm_Payment {
6565
*/
6666
protected $oamount = 0;
6767

68+
/**
69+
* The transaction column to update.
70+
* Defaults to 'txn_code' and 'txn_code_2' when a payment retry is triggered.
71+
*
72+
* @var integer
73+
*/
74+
protected $txn_column = 'txn_code';
75+
6876
/**
6977
* Constructor
7078
*/
@@ -74,13 +82,12 @@ public function __construct() {
7482
}
7583

7684
/**
77-
* Undocumented function
85+
* Sets up our data for processing.
7886
*
7987
* @return void
8088
*/
8189
protected function setup_data( $payment ) {
8290
$this->payment_meta = $payment;
83-
$this->helpers = new Helpers();
8491
$this->meta = $this->helpers->parse_meta_values( get_post( $this->payment_meta->post_id ) );
8592
$this->amount = $this->payment_meta->amount;
8693
$this->oamount = $this->meta['amount'];
@@ -103,13 +110,18 @@ public function confirm_payment() {
103110

104111
exit( wp_json_encode( $response ) );
105112
}
113+
114+
// If this is a retry payment then set the colum accordingly.
115+
if ( isset( $_POST['retry'] ) ) {
116+
$this->txn_column = 'txn_code_2';
117+
}
106118

107-
108-
$code = sanitize_text_field( $_POST['code'] );
109-
$record = $this->helpers->get_db_record( $code );
119+
$this->helpers = new Helpers();
120+
$code = sanitize_text_field( $_POST['code'] );
121+
$record = $this->helpers->get_db_record( $code, $this->txn_column );
110122

111123
if ( false !== $record ) {
112-
124+
113125
$this->setup_data( $record );
114126

115127
// Verify our transaction with the Paystack API.
@@ -221,7 +233,6 @@ protected function update_payment_dates( $data ) {
221233
'result' => 'failed',
222234
];
223235

224-
$customer_code = $data->customer->customer_code;
225236
$amount_paid = $data->amount / 100;
226237
$paystack_ref = $data->reference;
227238
$paid_at = $data->transaction_date;
@@ -233,7 +244,7 @@ protected function update_payment_dates( $data ) {
233244
'amount' => $amount_paid,
234245
'paid_at' => $paid_at,
235246
),
236-
array( 'txn_code' => $paystack_ref )
247+
array( $this->txn_column => $paystack_ref )
237248
);
238249
$return = [
239250
'message' => $this->meta['successmsg'],
@@ -249,7 +260,7 @@ protected function update_payment_dates( $data ) {
249260
'amount' => $amount_paid,
250261
'paid_at' => $paid_at,
251262
),
252-
array( 'txn_code' => $paystack_ref )
263+
array( $this->txn_column => $paystack_ref )
253264
);
254265
$return = [
255266
'message' => $this->meta['successmsg'],
@@ -268,7 +279,7 @@ protected function update_payment_dates( $data ) {
268279
'paid' => 1,
269280
'paid_at' => $paid_at,
270281
),
271-
array( 'txn_code' => $paystack_ref )
282+
array( $this->txn_column => $paystack_ref )
272283
);
273284
$return = [
274285
'message' => $this->meta['successmsg'],

includes/classes/class-email-invoice.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ class Email_Invoice extends Email {
2323
*/
2424
public $slug = 'invoice';
2525

26+
/**
27+
* The form in which this was submitted.
28+
*
29+
* @var string
30+
*/
31+
public $referer_url = '';
32+
2633
/**
2734
* Constructor
2835
*/
2936
public function __construct() {
30-
add_action( 'pff_paystack_send_invoice', [ $this, 'send_invoice' ], 10, 6 );
37+
add_action( 'pff_paystack_send_invoice', [ $this, 'send_invoice' ], 10, 7 );
3138
}
3239

3340
/**
@@ -41,13 +48,15 @@ public function __construct() {
4148
* @param string $code
4249
* @return void
4350
*/
44-
public function send_invoice( $form_id, $currency, $amount, $name, $email, $code ) {
45-
$this->form_id = $form_id;
46-
$this->amount = $amount;
47-
$this->currency = $currency;
48-
$this->code = $code;
49-
$this->name = $name;
50-
$this->email = stripslashes( $email );
51+
public function send_invoice( $form_id, $currency, $amount, $name, $email, $code, $referer_url ) {
52+
$this->form_id = $form_id;
53+
$this->amount = $amount;
54+
$this->currency = $currency;
55+
$this->code = $code;
56+
$this->name = $name;
57+
$this->email = stripslashes( $email );
58+
$this->referer_url = $referer_url;
59+
5160
/* Translators: %1$s is the currency code, %2$s is the formatted amount */
5261
$this->subject = sprintf(
5362
__( 'Payment Invoice for %1$s %2$s', 'text-domain' ),
@@ -163,7 +172,7 @@ public function get_html_body() {
163172
<tbody>
164173
<tr>
165174
<td class="font_default" style="padding:12px 24px;font-family:Helvetica,Arial,sans-serif;font-size:16px;mso-line-height-rule:exactly;text-align:center;vertical-align:middle;-webkit-border-radius:4px;border-radius:4px;background-color:#666">
166-
<a target="_blank" href="<?php echo esc_url( get_permalink( $this->form_id ) . '/?code=' . $this->code ); ?>" style="display:block;text-decoration:none;font-family:Helvetica,Arial,sans-serif;color:#fff;font-weight:bold;text-align:center">
175+
<a target="_blank" href="<?php echo esc_url( $this->referer_url . '?code=' . $this->code ); ?>" style="display:block;text-decoration:none;font-family:Helvetica,Arial,sans-serif;color:#fff;font-weight:bold;text-align:center">
167176
<span style="text-decoration:none;color:#fff;text-align:center;display:block">
168177
<?php echo esc_html__( 'Try Again', 'pff-paystack' ); ?>
169178
</span>

includes/classes/class-form-submit.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class Form_Submit {
7373
*/
7474
protected $fixed_metadata = array();
7575

76+
/**
77+
* The URL from where this was submitted.
78+
*
79+
* @var string
80+
*/
81+
protected $referer_url = '';
82+
7683
/**
7784
* Constructor
7885
*/
@@ -133,6 +140,11 @@ protected function setup_data() {
133140
if ( ! isset( $this->form_data['pf-quantity'] ) ) {
134141
$this->form_data['pf-quantity'] = 1;
135142
}
143+
144+
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
145+
// Get the referer URL
146+
$this->referer_url = sanitize_url( $_SERVER['HTTP_REFERER'] );
147+
}
136148
}
137149

138150
/**
@@ -343,7 +355,7 @@ public function submit_action() {
343355
* 11: Email_Invoice::send_invoice();
344356
*/
345357
if ( 'yes' === $this->meta['sendinvoice'] ) {
346-
do_action( 'pff_paystack_send_invoice', $this->form_id, $this->meta['currency'], $insert['amount'], $this->form_data['pf-fname'], $insert['email'], $code );
358+
do_action( 'pff_paystack_send_invoice', $this->form_id, $this->meta['currency'], $insert['amount'], $this->form_data['pf-fname'], $insert['email'], $code, $this->referer_url );
347359
}
348360

349361
$transaction_charge = (int) $this->meta['merchantamount'];

0 commit comments

Comments
 (0)