Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Stripe Sync Order
- * Description: REST API endpoint for creating and completing WooCommerce orders via Stripe webhook or other external service.
- * Version: 1.0
- * Author: T+GM Consulting
- */
- // Register custom REST endpoint
- add_action('rest_api_init', function () {
- register_rest_route('stripe-sync/v1', '/create-order', [
- 'methods' => 'POST',
- 'callback' => 'stripe_sync_create_order',
- 'permission_callback' => '__return_true', // Biztonság: majd érdemes lehet hitelesítést tenni rá
- ]);
- });
- function stripe_sync_create_order(WP_REST_Request $request) {
- $product_id = 123; // ← IDE írd be a termék ID-ját
- $user_id = 0; // 0 ha guest user
- $quantity = 1;
- // Dummy billing address
- $address = [
- 'first_name' => 'Stripe',
- 'last_name' => 'User',
- 'phone' => '06301234567',
- 'address_1' => 'Stripe Street 1',
- 'city' => 'Budapest',
- 'postcode' => '1111',
- 'country' => 'HU',
- ];
- try {
- $order = wc_create_order(['customer_id' => $user_id]);
- $product = wc_get_product($product_id);
- if (!$product) {
- return new WP_Error('invalid_product', 'A megadott termék nem létezik', ['status' => 400]);
- }
- $order->add_product($product, $quantity);
- $order->set_address($address, 'billing');
- $order->calculate_totals();
- $order->update_status('completed', 'Rendelés automatikusan lezárva Stripe-ból.');
- return [
- 'success' => true,
- 'order_id' => $order->get_id(),
- 'status' => $order->get_status(),
- ];
- } catch (Exception $e) {
- return new WP_Error('order_creation_failed', $e->getMessage(), ['status' => 500]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement