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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Controller_Product_Data extends WC_Stripe_Rest_Controller {
protected $namespace = 'product';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'gateway',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'toggle_gateway' ),
'permission_callback' => array( $this, 'admin_permission_check' )
)
);
register_rest_route(
$this->rest_uri(),
'save',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'save' ),
'permission_callback' => array( $this, 'admin_permission_check' ),
)
);
}
public function toggle_gateway( $request ) {
$product = wc_get_product( $request->get_param( 'product_id' ) );
$payment_method = WC()->payment_gateways()->payment_gateways()[ $request->get_param( 'gateway_id' ) ];
$option = new WC_Stripe_Product_Gateway_Option( $product, $payment_method );
$option->set_option( 'enabled', ! $option->enabled() );
$option->save();
return rest_ensure_response( array( 'enabled' => $option->enabled() ) );
}
public function save( $request ) {
$gateways = $request->get_param( 'gateways' );
$charge_types = $request->get_param( 'charge_types' );
$payment_gateways = WC()->payment_gateways()->payment_gateways();
$product = wc_get_product( $request->get_param( 'product_id' ) );
$order = array();
$loop = 0;
foreach ( $gateways as $gateway ) {
$order[ $gateway ] = $loop;
$loop ++;
}
$product->update_meta_data( WC_Stripe_Constants::PRODUCT_GATEWAY_ORDER, $order );
foreach ( $charge_types as $type ) {
$option = new WC_Stripe_Product_Gateway_Option( $product, $payment_gateways[ $type['gateway'] ] );
$option->set_option( 'charge_type', $type['value'] );
$option->save();
}
$product->update_meta_data( '_stripe_button_position', $request->get_param( 'position' ) );
$product->save();
return rest_ensure_response( array( 'order' => $order ) );
}
}