getPaymentStatus( $id ) before fulfilling. * * The button also dispatches DOM events: sablepay:success, sablepay:failure. */ final class Shortcodes { public static function register() { add_shortcode( 'sablepay_button', array( __CLASS__, 'button' ) ); } public static function button( $atts ) { $atts = shortcode_atts( array( 'amount' => '', 'label' => __( 'Pay with SablePay', 'sablepay' ), 'metadata' => '', 'item' => '', 'quantity' => '1', 'success_url' => '', 'class' => '', ), $atts, 'sablepay_button' ); $amount = is_numeric( $atts['amount'] ) ? (float) $atts['amount'] : 0; if ( $amount < 0.01 || $amount > 1000000 ) { return current_user_can( 'edit_posts' ) ? '
' . esc_html__( 'SablePay: the amount attribute must be between 0.01 and 1,000,000.', 'sablepay' ) . '
' : ''; } $metadata = array(); foreach ( array_filter( array_map( 'trim', explode( ',', (string) $atts['metadata'] ) ) ) as $pair ) { $parts = array_map( 'trim', explode( ':', $pair, 2 ) ); if ( 2 === count( $parts ) && '' !== $parts[0] ) { $metadata[ sanitize_key( $parts[0] ) ] = sanitize_text_field( $parts[1] ); } } $metadata['source'] = $metadata['source'] ?? 'wordpress-shortcode'; $items = array(); if ( '' !== $atts['item'] ) { $qty = max( 1, (int) $atts['quantity'] ); $items[] = array( 'name' => sanitize_text_field( $atts['item'] ), 'quantity' => $qty, 'price' => round( $amount / $qty, 2 ), ); } Assets::registerScripts(); wp_enqueue_script( 'sablepay-button' ); wp_enqueue_style( 'sablepay-button' ); $payload = array( 'amount' => round( $amount, 2 ), 'metadata' => $metadata, 'items' => $items, 'successUrl' => '' !== $atts['success_url'] ? esc_url_raw( $atts['success_url'] ) : '', ); return sprintf( '', esc_attr( $atts['class'] ), esc_attr( wp_json_encode( $payload ) ), esc_html( $atts['label'] ) ); } }