-
Notifications
You must be signed in to change notification settings - Fork 20
/
class-wc-checkoutcom-fawry.php
107 lines (82 loc) · 3.87 KB
/
class-wc-checkoutcom-fawry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
class WC_Gateway_Checkout_Com_Alternative_Payments_Fawry extends WC_Gateway_Checkout_Com_Alternative_Payments {
const PAYMENT_METHOD = 'fawry';
public function __construct()
{
$this->id = 'wc_checkout_com_alternative_payments_fawry';
$this->method_title = __("Checkout.com", 'wc_checkout_com');
$this->method_description = __("The Checkout.com extension allows shop owners to process online payments through the <a href=\"https://www.checkout.com\">Checkout.com Payment Gateway.</a>", 'wc_checkout_com');
$this->title = __("Fawry", 'wc_checkout_com');
$this->supports = array('products', 'refunds');
$this->has_fields = true;
$this->init_form_fields();
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
public function payment_fields()
{
// get available apms depending on currency
$apm_available = WC_Checkoutcom_Utility::get_alternative_payment_methods();
$message = __("Pay with Fawry", 'wc_checkout_com');
?>
<p style="margin-bottom: 0;"> <?php echo $message ?> </p>
<?php
if (! in_array(self::PAYMENT_METHOD, $apm_available) ) {
?>
<script>
jQuery('.payment_method_wc_checkout_com_alternative_payments_fawry').hide();
</script>
<?php
}
}
public function process_payment( $order_id )
{
if (!session_id()) session_start();
global $woocommerce;
$order = wc_get_order( $order_id );
// create alternative payment
$result = (array) WC_Checkoutcom_Api_request::create_apm_payment($order, self::PAYMENT_METHOD);
// check if result has error and return error message
if (isset($result['error']) && !empty($result['error'])) {
WC_Checkoutcom_Utility::wc_add_notice_self(__($result['error']), 'error');
return;
}
$status = WC_Admin_Settings::get_option('ckocom_order_authorised', 'on-hold');
$message = "";
if ($result['source']['type'] == self::PAYMENT_METHOD) {
update_post_meta($order_id, 'cko_fawry_reference_number', $result['source']['reference_number']);
update_post_meta($order_id, 'cko_payment_authorized', true);
// Get cko auth status configured in admin
$message = __("Checkout.com - Fawry payment " ."</br>". " Action ID : {$result['id']} - Fawry reference number : {$result['source']['reference_number']} ", 'wc_checkout_com');
if ($result['status'] == 'Captured') {
$status = WC_Admin_Settings::get_option('ckocom_order_captured', 'processing');
$message = __("Checkout.com Payment Captured " ."</br>". " Action ID - {$result['id']} ", 'wc_checkout_com');
}
}
update_post_meta($order_id, '_transaction_id', $result['id']);
update_post_meta($order_id, '_cko_payment_id', $result['id']);
// add notes for the order and update status
$order->add_order_note($message);
$order->update_status($status);
// Reduce stock levels
wc_reduce_stock_levels( $order_id );
// Remove cart
$woocommerce->cart->empty_cart();
// Return thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
/**
* Process refund for the order.
*
* @param int $order_id Order ID.
* @param int $amount Amount to refund.
* @param string $reason Refund reason.
*
* @return bool
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
return parent::process_refund( $order_id, $amount, $reason );
}
}