@@ -388,21 +388,45 @@ public function handle_webhook() {
388388 );
389389
390390 $ this ->_api ->set_gateway ( $ this );
391+ $ secret_key = $ this ->get_secret_key ();
392+ \Stripe \Stripe::setApiKey ( $ secret_key );
391393
392- $ secret_key = $ this ->get_secret_key ();
393- \Stripe \Stripe::setApiKey ( $ secret_key );
394-
395- // Make sure everyone is using the same API version. we can update this if/when necessary.
396- // If we don't set this, Stripe will use latest version, which may break our implementation.
397- //\Stripe\Stripe::setApiVersion( '2018-02-28' );
398-
399- // retrieve the request's body and parse it as JSON
400394 $ body = @file_get_contents ( 'php://input ' );
401-
402395 $ this ->log ( $ body );
403- // grab the event information
404396 $ event_json = json_decode ( $ body );
405397
398+ // NEU: Payment Element Flow
399+ if (isset ($ event_json ->type ) && $ event_json ->type === 'payment_intent.succeeded ' ) {
400+ $ payment_intent_id = $ event_json ->data ->object ->id ;
401+ $ customer_id = $ event_json ->data ->object ->customer ;
402+
403+ $ intent = \Stripe \PaymentIntent::retrieve ($ payment_intent_id );
404+ $ payment_method_id = $ intent ->payment_method ;
405+
406+ $ plan_id = isset ($ intent ->metadata ->plan_id ) ? $ intent ->metadata ->plan_id : null ;
407+ if (!$ plan_id ) {
408+ $ this ->log ('Plan-ID nicht gefunden! ' );
409+ http_response_code (200 );
410+ exit ;
411+ }
412+
413+ $ existing_subs = \Stripe \Subscription::all ([
414+ 'customer ' => $ customer_id ,
415+ 'limit ' => 1 ,
416+ ]);
417+ if (count ($ existing_subs ->data ) === 0 ) {
418+ $ subscription = \Stripe \Subscription::create ([
419+ 'customer ' => $ customer_id ,
420+ 'items ' => [['plan ' => $ plan_id ]],
421+ 'default_payment_method ' => $ payment_method_id ,
422+ 'expand ' => ['latest_invoice.payment_intent ' ],
423+ ]);
424+ $ this ->log ('Stripe-Abo angelegt: ' . $ subscription ->id );
425+ }
426+ http_response_code (200 );
427+ exit ;
428+ }
429+
406430 if ( isset ( $ event_json ->id ) ) {
407431 try {
408432 $ event_id = $ event_json ->id ;
@@ -412,46 +436,46 @@ public function handle_webhook() {
412436 if ( $ event && $ this ->valid_event ( $ event ->type ) ) {
413437 $ stripe_invoice = $ event ->data ->object ;
414438 if ( $ stripe_invoice && isset ( $ stripe_invoice ->id ) ) {
415- $ stripe_invoice_amount = $ stripe_invoice ->total / 100.0 ;
416- $ stripe_invoice_subtotal = $ stripe_invoice ->subtotal / 100.0 ;
417- $ stripe_customer = \Stripe \Customer::retrieve ( $ stripe_invoice ->customer );
418- $ current_date = MS_Helper_Period::current_date ( null , true );
439+ $ stripe_invoice_amount = $ stripe_invoice ->total / 100.0 ;
440+ $ stripe_invoice_subtotal = $ stripe_invoice ->subtotal / 100.0 ;
441+ $ stripe_customer = \Stripe \Customer::retrieve ( $ stripe_invoice ->customer );
442+ $ current_date = MS_Helper_Period::current_date ( null , true );
419443 if ( $ stripe_customer ) {
420- $ email = $ stripe_customer ->email ;
444+ $ email = $ stripe_customer ->email ;
421445
422446 if ( !function_exists ( 'get_user_by ' ) ) {
423- include_once ( ABSPATH . 'wp-includes/pluggable.php ' );
447+ include_once ( ABSPATH . 'wp-includes/pluggable.php ' );
424448 }
425449
426- $ user = get_user_by ( 'email ' , $ email );
427- if ( $ user && !is_wp_error ( $ user ) ) {
428- $ member = MS_Factory::load ( 'MS_Model_Member ' , $ user ->ID );
429- $ success = false ;
430- if ( $ member ) {
450+ $ user = get_user_by ( 'email ' , $ email );
451+ if ( $ user && !is_wp_error ( $ user ) ) {
452+ $ member = MS_Factory::load ( 'MS_Model_Member ' , $ user ->ID );
453+ $ success = false ;
454+ if ( $ member ) {
431455
432- foreach ( $ member ->subscriptions as $ subscription ) {
433- if ( $ subscription ) {
434- if ( $ subscription ->is_system () ) { continue ; }
435- $ membership = $ subscription ->get_membership ();
436- $ stripe_sub = $ this ->_api ->get_subscription_data (
437- $ stripe_invoice ->lines ->data ,
438- $ membership
439- );
440- if ( $ stripe_sub ) {
441- switch ( $ event ->type ){
442- case 'invoice.created ' :
443- if ( $ membership ->has_trial () ) {
444- //$subscription->check_membership_status();
445- //if ( $subscription->trial_period_completed ) {
446- if ( $ subscription ->status == MS_Model_Relationship::STATUS_TRIAL_EXPIRED &&
447- MS_Model_Addon::is_enabled ( MS_Model_Addon::ADDON_TRIAL )){
456+ foreach ( $ member ->subscriptions as $ subscription ) {
457+ if ( $ subscription ) {
458+ if ( $ subscription ->is_system () ) { continue ; }
459+ $ membership = $ subscription ->get_membership ();
460+ $ stripe_sub = $ this ->_api ->get_subscription_data (
461+ $ stripe_invoice ->lines ->data ,
462+ $ membership
463+ );
464+ if ( $ stripe_sub ) {
465+ switch ( $ event ->type ){
466+ case 'invoice.created ' :
467+ if ( $ membership ->has_trial () ) {
468+ //$subscription->check_membership_status();
469+ //if ( $subscription->trial_period_completed ) {
470+ if ( $ subscription ->status == MS_Model_Relationship::STATUS_TRIAL_EXPIRED &&
471+ MS_Model_Addon::is_enabled ( MS_Model_Addon::ADDON_TRIAL )){
448472
449- $ subscription ->status = MS_Model_Relationship::STATUS_PENDING ;
450- $ subscription ->save ();
451- }
452- }
453- break ;
454- case 'invoice.payment_succeeded ' :
473+ $ subscription ->status = MS_Model_Relationship::STATUS_PENDING ;
474+ $ subscription ->save ();
475+ }
476+ }
477+ break ;
478+ case 'invoice.payment_succeeded ' :
455479 if ( $ current_date != $ subscription ->start_date ) {
456480 $ invoice = $ subscription ->get_current_invoice ();
457481
0 commit comments