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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Admin_Order_Metaboxes {
public static function init() {
add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ), 10, 2 );
}
public static function add_meta_boxes( $post_type, $post ) {
if ( $post_type !== 'shop_order' ) {
return;
}
add_action( 'woocommerce_admin_order_data_after_order_details', array( __CLASS__, 'pay_order_section' ) );
$order = wc_get_order( $post->ID );
$payment_method = $order->get_payment_method();
if ( $payment_method ) {
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways[ $payment_method ] ) ) {
$gateway = WC()->payment_gateways()->payment_gateways()[ $payment_method ];
if ( $gateway instanceof WC_Payment_Gateway_Stripe ) {
add_action( 'woocommerce_admin_order_data_after_billing_address', array( __CLASS__, 'charge_data_view' ) );
add_action( 'woocommerce_admin_order_totals_after_total', array( __CLASS__, 'stripe_fee_view' ) );
}
}
}
self::enqueue_scripts();
}
public static function charge_data_view( $order ) {
if ( ( $transaction_id = $order->get_transaction_id() ) ) {
include 'views/html-order-charge-data.php';
}
}
public static function pay_order_section( $order ) {
if ( $order->get_type() === 'shop_order'
&& $order->has_status( apply_filters( 'wc_stripe_pay_order_statuses', array(
'pending',
'auto-draft'
), $order ) )
) {
include 'views/html-order-pay.php';
$payment_methods = array();
foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
if ( $gateway instanceof WC_Payment_Gateway_Stripe ) {
$payment_methods = array_merge( $payment_methods, WC_Payment_Tokens::get_customer_tokens( $order->get_user_id(), $gateway->id ) );
}
}
wp_enqueue_script( 'wc-stripe-elements', 'https://js.stripe.com/v3/', array(), stripe_wc()->version, true );
wp_localize_script(
'wc-stripe-elements',
'wc_stripe_order_pay_params',
array(
'api_key' => wc_stripe_get_publishable_key(),
'payment_methods' => array_map(
function ( $payment_method ) {
return $payment_method->to_json();
},
$payment_methods
),
'order_status' => $order->get_status(),
'messages' => array(
'order_status' => __( 'You must create the order before payment can be processed.', 'woo-stripe-payment' )
)
)
);
wp_enqueue_script( 'wc-stripe-admin-modals', stripe_wc()->assets_url( 'js/admin/modals.js' ), array(
'wc-backbone-modal',
'jquery-blockui'
), stripe_wc()->version, true );
}
}
public static function stripe_fee_view( $order_id ) {
if ( stripe_wc()->advanced_settings->is_active( 'stripe_fee' ) ) {
$order = wc_get_order( $order_id );
$fee = WC_Stripe_Utils::display_fee( $order );
$net = WC_Stripe_Utils::display_net( $order );
if ( $fee && $net ) {
?>
<tr>
<td class="label wc-stripe-fee"><?php esc_html_e( 'Stripe Fee', 'woo-stripe-payment' ) ?>:</td>
<td width="1%"></td>
<td><?php echo $fee ?></td>
</tr>
<tr>
<td class="label wc-stripe-net"><?php esc_html_e( 'Net payout', 'woo-stripe-payment' ) ?></td>
<td width="1%"></td>
<td class="total"><?php echo $net ?></td>
</tr>
<?php
}
}
}
public static function enqueue_scripts() {
wp_enqueue_script( 'wc-stripe-order-metabox', stripe_wc()->assets_url( 'js/admin/meta-boxes-order.js' ), array(
'jquery',
'jquery-blockui'
), stripe_wc()->version(), true );
wp_localize_script(
'wc-stripe-order-metabox',
'wc_stripe_order_metabox_params',
array(
'_wpnonce' => wp_create_nonce( 'wp_rest' ),
'routes' => array(
'charge_view' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->order_actions->rest_uri( 'charge-view' ) ),
'capture' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->order_actions->rest_uri( 'capture' ) ),
'void' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->order_actions->rest_uri( 'void' ) ),
'pay' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->order_actions->rest_uri( 'pay' ) ),
'payment_methods' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->order_actions->rest_uri( 'customer-payment-methods' ) ),
),
'messages' => array(
'capture_amount' => __( 'If the capture amount is less than the order total, make sure you edit your order line items to reflect the new capture amount.', 'woo-stripe-payment' )
)
)
);
}
}
WC_Stripe_Admin_Order_Metaboxes::init();