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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Braintree_Payment_Gateway' ) ) {
return;
}
use \PaymentPlugins\WC_Braintree_Constants as Constants;
abstract class WC_Braintree_Local_Payment_Gateway extends WC_Braintree_Payment_Gateway {
protected $currencies = array();
public $countries = array();
public $default_title = '';
public $payment_id_key;
public function __construct() {
$this->template = 'local-payment.php';
$this->token_type = 'Local_Payment';
parent::__construct();
$this->settings['method_format'] = 'source_and_payer';
$this->settings['charge_type'] = 'capture';
$this->payment_id_key = $this->id . '_payment_id';
$this->order_button_text = $this->get_option( 'order_button_text' );
}
public function add_hooks() {
parent::add_hooks();
remove_filter( 'wc_braintree_admin_settings_tabs', array( $this, 'admin_settings_tabs' ) );
add_filter( 'wc_braintree_local_gateways_tab', array( $this, 'admin_settings_tabs' ) );
add_action( 'woocommerce_update_options_checkout_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function set_supports() {
$this->supports = array( 'products', 'refunds', 'wc_braintree_fees' );
}
public function init_form_fields() {
$this->form_fields = apply_filters( 'wc_' . $this->id . '_form_fields', include WC_BRAINTREE_PATH . 'includes/gateways/settings/local-payment-settings.php', $this );
}
public function is_local_payment_available() {
global $wp;
if ( isset( $wp->query_vars['order-pay'] ) ) {
$order = wc_get_order( absint( $wp->query_vars['order-pay'] ) );
$currency = $order->get_currency();
$country = $order->get_billing_country();
} else {
$currency = get_woocommerce_currency();
$country = WC()->customer ? WC()->customer->get_billing_country() : null;
}
$_available = false;
if ( in_array( $currency, $this->currencies ) ) {
$type = $this->get_option( 'allowed_countries' );
if ( 'all_except' === $type ) {
$_available = ! in_array( $country, $this->get_option( 'except_countries', array() ) );
} elseif ( 'specific' === $type ) {
$_available = in_array( $country, $this->get_option( 'specific_countries', array() ) );
} else {
$_available = $this->countries ? in_array( $country, $this->countries ) : true;
}
}
return apply_filters( 'wc_braintree_is_local_payment_available', $_available, $this, $currency, $country );
}
public function enqueue_checkout_scripts( $scripts ) {
if ( ! in_array( $scripts->get_handle( 'local-payment' ), $scripts->get_enqueued_scripts() ) ) {
$scripts->enqueue_script(
'local-payment',
$scripts->assets_url( 'js/frontend/local-payment.js' ),
array(
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'local-payment-v3' ),
)
);
$scripts->localize_script( 'local-payment', array() );
}
}
public function localize_script_params() {
return array_merge(
$this->get_localized_standard_params(),
array(
'payment_key' => $this->payment_id_key,
'payment_type' => str_replace( 'braintree_', '', $this->id ),
'button_text' => $this->order_button_text,
'return_url' => wc_get_checkout_url(),
'routes' => array(
'payment_data' => WC_Braintree_Rest_API::get_endpoint( braintree()->rest_api->local_payment->rest_uri() . '/payment-data' ),
'complete_payment' => WC_Braintree_Rest_API::get_endpoint( braintree()->rest_api->local_payment->rest_uri() . '/payment/complete' )
),
)
);
}
public function output_settings_nav() {
global $current_section;
parent::output_settings_nav();
include braintree()->plugin_path() . 'includes/admin/views/html-local-gateways-nav.php';
}
public function get_payment_method_from_transaction( $transaction ) {
return $transaction->localPayment;
}
public function get_gateway_supports_description() {
$text = '';
if ( $this->currencies ) {
$text = sprintf(
__( '%1$s is available when store currency is %2$s', 'woo-payment-gateway' ),
$this->get_method_title(),
'<b>' .
implode( ', ', $this->currencies ) . '</b>'
);
}
if ( $this->countries ) {
$text .= sprintf( __( ' and billing country is %s', 'woo-payment-gateway' ), '<b>' . implode( ', ', $this->countries ) . '</b>' );
}
return $text;
}
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
if ( $this->has_order_lock( $order ) ) {
return $this->order_success_result( $order );
} else {
if ( $order->get_total() == 0 ) {
return parent::process_payment( $order_id );
}
if ( ( $payment_id = $order->get_meta( Constants::PAYMENT_ID ) ) ) {
if ( ! $this->get_payment_method_nonce() ) {
return array(
'result' => 'success',
'redirect' => $this->get_order_created_url( $order ),
);
}
$this->set_order_lock( $order );
return parent::process_payment( $order_id );
} else {
return array(
'result' => 'success',
'redirect' => $this->get_order_created_url( $order ),
);
}
}
}
private function get_order_created_url( $order ) {
return sprintf( '#local_payment=%s', base64_encode( wp_json_encode(
array(
'payment_method' => $order->get_payment_method(),
'order_id' => $order->get_id(),
'order_key' => $order->get_order_key(),
'timestamp' => time()
)
) ) );
}
public function set_order_lock( $order ) {
$order_id = ( is_object( $order ) ? $order->get_id() : $order );
set_transient( 'braintree_lock_order_' . $order_id, $order_id, apply_filters( 'wc_braintree_set_order_lock', 2 * MINUTE_IN_SECONDS ) );
}
public function remove_order_lock( $order ) {
delete_transient( 'braintree_lock_order_' . ( is_object( $order ) ? $order->get_id() : $order ) );
}
public function has_order_lock( $order ) {
$lock = get_transient( 'braintree_lock_order_' . ( is_object( $order ) ? $order->get_id() : $order ) );
return $lock !== false;
}
public function add_order_line_items( &$args, $order, &$items = array() ) {
return $this;
}
public function has_enqueued_scripts( $scripts, $context = 'checkout' ) {
switch ( $context ) {
case 'checkout':
return wp_script_is( $scripts->get_handle( 'local-payment' ) );
}
}
public function get_braintree_documentation_url() {
return 'https://docs.paymentplugins.com/wc-braintree/config/#/braintree_local_gateways';
}
protected function get_default_available_countries() {
return array();
}
}