Skip to content

Commit 754146a

Browse files
authored
Merge pull request #18 from skyverge/add-payment-results
Add payment result options
2 parents fc9c432 + 54561b2 commit 754146a

1 file changed

Lines changed: 58 additions & 3 deletions

File tree

includes/Bogus_Gateway.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
class Bogus_Gateway extends \WC_Payment_Gateway {
3333

3434

35+
/** @var string the "accepted" payment result code */
36+
const PAYMENT_RESULT_APPROVED = 'approved';
37+
38+
/** @var string the "declined" payment result code */
39+
const PAYMENT_RESULT_DECLINED = 'declined';
40+
41+
/** @var string the "held" payment result code */
42+
const PAYMENT_RESULT_HELD = 'held';
43+
44+
3545
/** @var string subscriptions setting */
3646
private $subscriptions;
3747

@@ -130,6 +140,28 @@ public function init_form_fields() {
130140
}
131141

132142

143+
/**
144+
* Renders the gateway payment fields.
145+
*
146+
* @since x.y.z
147+
*/
148+
public function payment_fields() {
149+
150+
parent::payment_fields();
151+
152+
woocommerce_form_field( "{$this->id}_payment_result", [
153+
'type' => 'select',
154+
'label' => __( 'Result', 'woocommerce-dev-helper' ),
155+
'required' => true,
156+
'options' => [
157+
self::PAYMENT_RESULT_APPROVED => __( 'Approved', 'woocommerce-dev-helper' ),
158+
self::PAYMENT_RESULT_HELD => __( 'Held', 'woocommerce-dev-helper' ),
159+
self::PAYMENT_RESULT_DECLINED => __( 'Declined', 'woocommerce-dev-helper' ),
160+
],
161+
] );
162+
}
163+
164+
133165
/**
134166
* Returns true if Subscriptions support is enabled.
135167
*
@@ -152,9 +184,32 @@ public function process_payment( $order_id ) {
152184

153185
$order = wc_get_order( $order_id );
154186

155-
// Update order status and add a transaction note
156-
$order->payment_complete();
157-
$order->add_order_note( __( 'Bogus is always approved 😎', 'woocommerce-dev-helper' ) );
187+
$result = ! empty( $_POST[ "{$this->id}_payment_result" ] ) ? $_POST[ "{$this->id}_payment_result" ] : self::PAYMENT_RESULT_APPROVED;
188+
189+
// halt on decline
190+
if ( self::PAYMENT_RESULT_DECLINED === $result ) {
191+
192+
$message = __( 'Sorry, the payment was bogus 😳', 'woocommerce-dev-helper' );
193+
194+
if ( ! $order->has_status( 'failed' ) ) {
195+
$order->update_status( 'failed', $message );
196+
} else {
197+
$order->add_order_note( $message );
198+
}
199+
200+
throw new \Exception( $message );
201+
}
202+
203+
// update the order status accordingly
204+
if ( self::PAYMENT_RESULT_HELD === $result ) {
205+
206+
$order->update_status( 'on-hold', __( 'The payment may be bogus, holding the order 🤔', 'woocommerce-dev-helper' ) );
207+
208+
} else {
209+
210+
$order->payment_complete();
211+
$order->add_order_note( __( 'Payment approved 😎', 'woocommerce-dev-helper' ) );
212+
}
158213

159214
// Remove cart
160215
WC()->cart->empty_cart();

0 commit comments

Comments
 (0)