-
Notifications
You must be signed in to change notification settings - Fork 14
/
wc-bac-payment.php
210 lines (169 loc) · 8.17 KB
/
wc-bac-payment.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/* Authorize.net AIM Payment Gateway Class */
class Bac_Payment_Gateway extends WC_Payment_Gateway {
// Setup our Gateway's id, description and other values
function __construct() {
// The global ID for this Payment method
$this->id = "bac_payment";
// The Title shown on the top of the Payment Gateways Page next to all the other Payment Gateways
$this->method_title = __( "BAC PAYMENT GATEWAY", 'bac-payment' );
// The description for this Payment Gateway, shown on the actual Payment options page on the backend
$this->method_description = __( "BAC Payment Gateway Plug-in for WooCommerce", 'bac-payment' );
// The title to be used for the vertical tabs that can be ordered top to bottom
$this->title = __( "BAC Payment Gateway", 'bac-payment' );
// If you want to show an image next to the gateway's name on the frontend, enter a URL to an image.
$this->icon = null;
// Bool. Can be set to true if you want payment fields to show on the checkout
// if doing a direct integration, which we are doing in this case
$this->has_fields = true;
// Supports the default credit card form
$this->supports = array( 'default_credit_card_form' );
// This basically defines your settings which are then loaded with init_settings()
$this->init_form_fields();
// After init_settings() is called, you can get the settings and load them into variables, e.g:
// $this->title = $this->get_option( 'title' );
$this->init_settings();
// Turn these settings into variables we can use
foreach ( $this->settings as $setting_key => $value ) {
$this->$setting_key = $value;
}
// Lets check for SSL
add_action( 'admin_notices', array( $this, 'do_ssl_check' ) );
// Save settings
if ( is_admin() ) {
// Versions over 2.0
// Save our administration options. Since we are not going to be doing anything special
// we have not defined 'process_admin_options' in this class so the method in the parent
// class will be used instead
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
} // End __construct()
// Build the administration fields for this specific Gateway
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Activar / Desactivar', 'bac-payment' ),
'label' => __( 'Activar este metodo de pago', 'bac-payment' ),
'type' => 'checkbox',
'default' => 'no',
),
'title' => array(
'title' => __( 'Título', 'bac-payment' ),
'type' => 'text',
'desc_tip' => __( 'Título de pago que el cliente verá durante el proceso de pago.', 'bac-payment' ),
'default' => __( 'Tarjeta de crédito', 'bac-payment' ),
),
'description' => array(
'title' => __( 'Descripción', 'bac-payment' ),
'type' => 'textarea',
'desc_tip' => __( 'Descripción de pago que el cliente verá durante el proceso de pago.', 'bac-payment' ),
'default' => __( 'Pague con seguridad usando su tarjeta de crédito.', 'bac-payment' ),
'css' => 'max-width:350px;'
),
'key_id' => array(
'title' => __( 'Key id', 'bac-payment' ),
'type' => 'text',
'desc_tip' => __( 'ID de clave de seguridad del panel de control del comerciante.', 'bac-payment' ),
'default' => '',
),
'api_key' => array(
'title' => __( 'Api key', 'bac-payment' ),
'type' => 'text',
'desc_tip' => __( 'ID de clave de api del panel de control del comerciante.', 'bac-payment' ),
'default' => '',
),
);
}
// Submit payment and handle response
public function process_payment( $order_id ) {
global $woocommerce;
// Get this Order's information so that we know
// who to charge and how much
$customer_order = new WC_Order( $order_id );
$environment_url = 'https://credomatic.compassmerchantsolutions.com/api/transact.php';
$time = time();
$key_id = $this->key_id;
$orderid = str_replace( "#", "", $customer_order->get_order_number() );
$hash = md5($orderid."|".$customer_order->order_total."|".$time."|".$this->api_key);
// This is where the fun stuff begins
$payload = array(
"key_id" => $key_id,
"hash" => $hash,
"time" => $time,
"amount" => $customer_order->order_total,
"ccnumber" => str_replace( array(' ', '-' ), '', $_POST['bac_payment-card-number'] ),
"ccexp" => str_replace( array( '/', ' '), '', $_POST['bac_payment-card-expiry'] ),
"orderid" => $orderid,
"cvv" => ( isset( $_POST['bac_payment-card-cvc'] ) ) ? $_POST['bac_payment-card-cvc'] : '',
"type" => "auth",
);
// Send this payload to Authorize.net for processing
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
if ( is_wp_error( $response ) )
throw new Exception( __( 'We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.'.$payload, 'bac-payment' ) );
if ( empty( $response['body'] ) )
throw new Exception( __( 'BAC\'s Response was empty.', 'bac-payment' ) );
// Retrieve the body's resopnse if no errors found
$response_body = wp_remote_retrieve_body( $response );
// Parse the response into something we can read
$resp_e = explode( "&", $response_body );
$resp = array();
foreach($resp_e as $r) {
$v = explode('=', $r);
$resp[$v[0]] = $v[1];
}
// Test the code to know if the transaction went through or not.
// 1 or 4 means the transaction was a success
if ( ($resp['response'] == 1 ) || ( $resp['response_code'] == 100 ) ) {
// Payment has been successful
$customer_order->add_order_note( __( 'BAC payment completed.', 'bac-payment' ) );
// Saving the bac info
$order_id = method_exists( $customer_order, 'get_id' ) ? $customer_order->get_id() : $customer_order->ID;
update_post_meta($order_id , '_wc_order_bac_authcode', $resp['authcode'] );
update_post_meta($order_id , '_wc_order_bac_transactionid', $resp['transactionid'] );
// Mark order as Paid
$customer_order->payment_complete();
// Empty the cart (Very important step)
$woocommerce->cart->empty_cart();
// Redirect to thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $customer_order ),
);
} else {
// Transaction was not succesful
// Add notice to the cart
wc_add_notice( $resp['responsetext'], 'error' );
// Add note to the order for your reference
$customer_order->add_order_note( 'Error: '. $resp['responsetext'] );
}
// Validate fields
}//end process payment
public function validate_fields() {
return true;
}
// Check if we are forcing SSL on checkout pages
// Custom function not required by the Gateway
public function do_ssl_check() {
if( $this->enabled == "yes" ) {
if( get_option( 'woocommerce_force_ssl_checkout' ) == "no" ) {
echo "<div class=\"error\"><p>". sprintf( __( "<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href=\"%s\">forcing the checkout pages to be secured.</a>" ), $this->method_title, admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) ."</p></div>";
}
}
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_bac_info', 10, 1 );
function show_bac_info( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__('BAC Auth Code').':</strong> ' . get_post_meta( $order_id, '_wc_order_bac_authcode', true ) . '</p>';
echo '<p><strong>'.__('BAC Transaction Id').':</strong> ' . get_post_meta( $order_id, '_wc_order_bac_transactionid', true ) . '</p>';
}
?>