class-swpm-member-subscriptions.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class SWPM_Member_Subscriptions {
  3. private $active_statuses = array( 'trialing', 'active' );
  4. private $active_subs_count = 0;
  5. private $subs_count = 0;
  6. private $subs = array();
  7. private $member_id;
  8. public function __construct( $member_id ) {
  9. $this->member_id = $member_id;
  10. $subscr_id = SwpmMemberUtils::get_member_field_by_id( $member_id, 'subscr_id' );
  11. $query_args = array(
  12. 'post_type' => 'swpm_transactions',
  13. 'meta_query' => array(
  14. 'relation' => 'AND',
  15. array(
  16. 'relation' => 'OR',
  17. array(
  18. 'key' => 'member_id',
  19. 'value' => $member_id,
  20. 'compare' => '=',
  21. ),
  22. array(
  23. 'key' => 'subscr_id',
  24. 'value' => $subscr_id,
  25. 'compare' => '=',
  26. ),
  27. ),
  28. array(
  29. 'key' => 'gateway',
  30. 'value' => 'stripe-sca-subs',
  31. 'compare' => '=',
  32. ),
  33. ),
  34. );
  35. $found_subs = new WP_Query( $query_args );
  36. $this->subs_count = $found_subs->post_count;
  37. foreach ( $found_subs->posts as $found_sub ) {
  38. $sub = array();
  39. $post_id = $found_sub->ID;
  40. $sub['post_id'] = $post_id;
  41. $sub_id = get_post_meta( $post_id, 'subscr_id', true );
  42. $sub['sub_id'] = $sub_id;
  43. $status = get_post_meta( $post_id, 'subscr_status', true );
  44. $sub['status'] = $status;
  45. if ( $this->is_active( $status ) ) {
  46. $this->active_subs_count++;
  47. }
  48. $cancel_token = get_post_meta( $post_id, 'subscr_cancel_token', true );
  49. if ( empty( $cancel_token ) ) {
  50. $cancel_token = md5( $post_id . $sub_id . uniqid() );
  51. update_post_meta( $post_id, 'subscr_cancel_token', $cancel_token );
  52. }
  53. $sub['cancel_token'] = $cancel_token;
  54. $is_live = get_post_meta( $post_id, 'is_live', true );
  55. $is_live = empty( $is_live ) ? false : true;
  56. $sub['is_live'] = $is_live;
  57. $sub['payment_button_id'] = get_post_meta( $post_id, 'payment_button_id', true );
  58. $this->subs[ $sub_id ] = $sub;
  59. }
  60. $this->recheck_status_if_needed();
  61. }
  62. public function get_active_subs_count() {
  63. return $this->active_subs_count;
  64. }
  65. public function is_active( $status ) {
  66. return in_array( $status, $this->active_statuses, true );
  67. }
  68. private function recheck_status_if_needed() {
  69. foreach ( $this->subs as $sub_id => $sub ) {
  70. if ( ! empty( $sub['status'] ) ) {
  71. continue;
  72. }
  73. try {
  74. $api_keys = SwpmMiscUtils::get_stripe_api_keys_from_payment_button( $sub['payment_button_id'], $sub['is_live'] );
  75. SwpmMiscUtils::load_stripe_lib();
  76. \Stripe\Stripe::setApiKey( $api_keys['secret'] );
  77. $stripe_sub = \Stripe\Subscription::retrieve( $sub_id );
  78. $this->subs[ $sub_id ]['status'] = $stripe_sub['status'];
  79. if ( $this->is_active( $stripe_sub['status'] ) ) {
  80. $this->active_subs_count++;
  81. }
  82. update_post_meta( $sub['post_id'], 'subscr_status', $stripe_sub['status'] );
  83. } catch ( \Exception $e ) {
  84. return false;
  85. }
  86. }
  87. }
  88. public function get_stripe_subs_cancel_url( $args, $sub_id = false ) {
  89. if ( empty( $this->active_subs_count ) ) {
  90. return SwpmUtils::_( 'No active subscriptions' );
  91. }
  92. if ( false === $sub_id ) {
  93. $sub_id = array_key_first( $this->subs );
  94. }
  95. $sub = $this->subs[ $sub_id ];
  96. $token = $sub['cancel_token'];
  97. $nonce = wp_nonce_field( $token, 'swpm_cancel_sub_nonce', false, false );
  98. $anchor_text = isset( $args['anchor_text'] ) ? $args['anchor_text'] : SwpmUtils::_( 'Cancel Subscription' );
  99. $out = '<form method="POST">%s<input type="hidden" name="swpm_cancel_sub_token" value="%s"></input>
  100. <button type="submit" name="swpm_do_cancel_sub" value="1" onclick="return confirm(\'' . esc_js( SwpmUtils::_( 'Are you sure that you want to cancel the subscription?' ) ) . '\');">' . $anchor_text . '</button></form>';
  101. $out = sprintf( $out, $nonce, $token );
  102. return $out;
  103. }
  104. public function find_by_token( $token ) {
  105. foreach ( $this->subs as $sub_id => $sub ) {
  106. if ( $sub['cancel_token'] === $token ) {
  107. return $sub;
  108. }
  109. }
  110. }
  111. public function cancel( $sub_id ) {
  112. $sub = $this->subs[ $sub_id ];
  113. try {
  114. $api_keys = SwpmMiscUtils::get_stripe_api_keys_from_payment_button( $sub['payment_button_id'], $sub['is_live'] );
  115. SwpmMiscUtils::load_stripe_lib();
  116. \Stripe\Stripe::setApiKey( $api_keys['secret'] );
  117. $stripe_sub = \Stripe\Subscription::retrieve( $sub_id );
  118. if ( $this->is_active( $stripe_sub['status'] ) ) {
  119. $stripe_sub->cancel();
  120. }
  121. update_post_meta( $sub['post_id'], 'subscr_status', $stripe_sub['status'] );
  122. } catch ( \Exception $e ) {
  123. return $e->getMessage();
  124. }
  125. return true;
  126. }
  127. }