/**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     global $wpdb;
     if ('shop_subscription' != $post->post_type || empty($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) {
         return;
     }
     self::init_address_fields();
     // Update meta
     update_post_meta($post_id, '_customer_user', absint($_POST['customer_user']));
     if (self::$billing_fields) {
         foreach (self::$billing_fields as $key => $field) {
             update_post_meta($post_id, '_billing_' . $key, wc_clean($_POST['_billing_' . $key]));
         }
     }
     if (self::$shipping_fields) {
         foreach (self::$shipping_fields as $key => $field) {
             update_post_meta($post_id, '_shipping_' . $key, wc_clean($_POST['_shipping_' . $key]));
         }
     }
     $subscription = wcs_get_subscription($post_id);
     try {
         WCS_Change_Payment_Method_Admin::save_meta($subscription);
         if ('cancelled' == $_POST['order_status']) {
             $subscription->cancel_order();
         } else {
             $subscription->update_status($_POST['order_status'], '', true);
         }
     } catch (Exception $e) {
         // translators: placeholder is error message from by payment gateway
         wcs_add_admin_notice(sprintf(__('Unable to change payment method: %s', 'woocommerce-subscriptions'), $e->getMessage()), 'error');
     }
     do_action('woocommerce_process_shop_subscription_meta', $post_id, $post);
 }
 /**
  * Setup the new payment information to call WC_Subscription::set_payment_method()
  *
  * @param $subscription WC_Subscription
  * @param $payment_details array payment data from api request
  * @since 2.0
  */
 public function update_payment_method($subscription, $payment_details, $updating)
 {
     global $wpdb;
     $payment_gateways = WC()->payment_gateways->get_available_payment_gateways();
     $payment_method = !empty($payment_details['method_id']) ? $payment_details['method_id'] : 'manual';
     $payment_gateway = isset($payment_gateways[$payment_details['method_id']]) ? $payment_gateways[$payment_details['method_id']] : '';
     try {
         $wpdb->query('START TRANSACTION');
         if ($updating && !array_key_exists($payment_method, WCS_Change_Payment_Method_Admin::get_valid_payment_methods($subscription))) {
             throw new Exception('wcs_api_edit_subscription_error', __('Gateway does not support admin changing the payment method on a Subscription.', 'woocommerce-subscriptions'));
         }
         $payment_method_meta = apply_filters('woocommerce_subscription_payment_meta', array(), $subscription);
         if (!empty($payment_gateway) && isset($payment_method_meta[$payment_gateway->id])) {
             $payment_method_meta = $payment_method_meta[$payment_gateway->id];
             if (!empty($payment_method_meta)) {
                 foreach ($payment_method_meta as $meta_table => $meta) {
                     if (!is_array($meta)) {
                         continue;
                     }
                     foreach ($meta as $meta_key => $meta_data) {
                         if (isset($payment_details[$meta_table][$meta_key])) {
                             $payment_method_meta[$meta_table][$meta_key]['value'] = $payment_details[$meta_table][$meta_key];
                         }
                     }
                 }
             }
         }
         if (empty($subscription->payment_gateway)) {
             $subscription->payment_gateway = $payment_gateway;
         }
         $subscription->set_payment_method($payment_gateway, $payment_method_meta);
         $wpdb->query('COMMIT');
     } catch (Exception $e) {
         $wpdb->query('ROLLBACK');
         // translators: 1$: gateway id, 2$: error message
         throw new Exception(sprintf(__('Subscription payment method could not be set to %1$s and has been set to manual with error message: %2$s', 'woocommerce-subscriptions'), !empty($payment_gateway->id) ? $payment_gateway->id : 'manual', $e->getMessage()));
     }
 }