Files
aza/AzA march 2026/wordpress/aza-license-bridge/snippets/phase1f-period-endpoint-snippet.php
2026-05-20 00:09:28 +02:00

118 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* AZA Phase 1f Woo Subscription Perioden → Hetzner (OHNE Woo /wc/v3/subscriptions REST)
*
* Nicht automatisch aktivieren: Inhalt z. B. per „Code Snippets“ einbinden oder ins
* Plugin `aza-license-bridge` übernehmen. Keine Secrets in Theme-Dateien.
*
* Voraussetzungen:
* - gleiche Optionen wie License Bridge: aza_lb_api_url, aza_lb_wc_secret
* - WC_PROVISION_SECRET auf dem Server = aza_lb_wc_secret
*
* Endpunkte:
* - POST {api}/wc/subscription_period
* Header: X-WC-Secret, Content-Type: application/json
* Body: siehe aza_lb_send_subscription_period()
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Sendet aktuelle Abrechnungsperiode für eine WooCommerce-Subscription-ID an AZA.
*
* @param WC_Subscription|object $subscription Subscription-Objekt (WCS).
* @return array|WP_Error
*/
function aza_lb_send_subscription_period( $subscription ) {
if ( ! is_object( $subscription ) || ! method_exists( $subscription, 'get_id' ) ) {
return new WP_Error( 'aza_lb_invalid_sub', 'Kein Subscription-Objekt.' );
}
$sub_numeric = (int) $subscription->get_id();
if ( $sub_numeric <= 0 ) {
return new WP_Error( 'aza_lb_bad_sub_id', 'Ungültige Subscription-ID.' );
}
$api_url = rtrim( (string) get_option( 'aza_lb_api_url', 'https://api.aza-medwork.ch' ), '/' );
$wc_secret = (string) get_option( 'aza_lb_wc_secret', '' );
if ( $wc_secret === '' ) {
return new WP_Error( 'aza_lb_no_secret', 'WC Provision Secret nicht konfiguriert.' );
}
$order_id = 0;
if ( function_exists( 'aza_lb_get_parent_order_id_from_subscription' ) ) {
$order_id = (int) aza_lb_get_parent_order_id_from_subscription( $subscription );
} elseif ( method_exists( $subscription, 'get_parent_id' ) ) {
$order_id = (int) $subscription->get_parent_id();
}
$payload = array(
'wc_subscription_id' => $sub_numeric,
'wc_order_id' => $order_id,
);
// WooCommerce Subscriptions: nächste Zahlung & Abrechnungsrhythmus
if ( method_exists( $subscription, 'get_date' ) ) {
$next = $subscription->get_date( 'next_payment' );
if ( is_string( $next ) && $next !== '' ) {
$payload['next_payment_date'] = $next;
}
}
if ( method_exists( $subscription, 'get_billing_period' ) ) {
$payload['billing_period'] = (string) $subscription->get_billing_period();
}
if ( method_exists( $subscription, 'get_billing_interval' ) ) {
$payload['billing_interval'] = (int) $subscription->get_billing_interval();
}
if ( method_exists( $subscription, 'get_status' ) ) {
$payload['subscription_status'] = (string) $subscription->get_status();
}
$url = $api_url . '/wc/subscription_period';
$response = wp_remote_post(
$url,
array(
'timeout' => 20,
'headers' => array(
'Content-Type' => 'application/json',
'X-WC-Secret' => $wc_secret,
'User-Agent' => 'AZA-License-Bridge-Period/1.0',
),
'body' => wp_json_encode( $payload ),
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
if ( $code >= 200 && $code < 300 ) {
$data = json_decode( (string) wp_remote_retrieve_body( $response ), true );
return is_array( $data ) ? $data : array( 'ok' => true );
}
return new WP_Error( 'aza_lb_period_failed', 'HTTP ' . $code );
}
/**
* Beispiel-Hook: nach Zahlung Perioden pushen.
* Bei Bedarf Cron oder Admin-Aktion ergänzen.
*/
add_action(
'woocommerce_subscription_payment_complete',
function ( $subscription ) {
if ( function_exists( 'aza_lb_send_subscription_period' ) ) {
aza_lb_send_subscription_period( $subscription );
}
},
10,
1
);