MemberPressService.php
253 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\MemberPress; |
| 4 | |
| 5 | use SureCart\Integrations\Contracts\IntegrationInterface; |
| 6 | use SureCart\Integrations\Contracts\PurchaseSyncInterface; |
| 7 | use SureCart\Integrations\IntegrationService; |
| 8 | use SureCart\Models\Purchase; |
| 9 | use SureCart\Support\Currency; |
| 10 | |
| 11 | /** |
| 12 | * Controls the MemberPress integration. |
| 13 | */ |
| 14 | class MemberPressService extends IntegrationService implements IntegrationInterface, PurchaseSyncInterface { |
| 15 | /** |
| 16 | * Get the slug for the integration. |
| 17 | * |
| 18 | * @return string |
| 19 | */ |
| 20 | public function getName() { |
| 21 | return 'surecart/memberpress'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Get the model for the integration. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public function getModel() { |
| 30 | return 'product'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the slug for the integration. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public function getLogo() { |
| 39 | return esc_url_raw( trailingslashit( plugin_dir_url( SURECART_PLUGIN_FILE ) ) . 'images/integrations/memberpress.svg' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the slug for the integration. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function getLabel() { |
| 48 | return __( 'MemberPress Membership', 'surecart' ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the slug for the integration. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function getItemLabel() { |
| 57 | return __( 'Membership Access', 'surecart' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the slug for the integration. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | public function getItemHelp() { |
| 66 | return __( 'Enable access to a MemberPress membership.', 'surecart' ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Is this enabled? |
| 71 | * |
| 72 | * @return boolean |
| 73 | */ |
| 74 | public function enabled() { |
| 75 | return defined( 'MEPR_VERSION' ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get item listing for the integration. |
| 80 | * |
| 81 | * @param array $items The integration items. |
| 82 | * @param string $search The search term. |
| 83 | * |
| 84 | * @return array The items for the integration. |
| 85 | */ |
| 86 | public function getItems( $items = [], $search = '' ) { |
| 87 | if ( ! class_exists( 'MeprProduct' ) ) { |
| 88 | return []; |
| 89 | } |
| 90 | |
| 91 | $membership_query = new \WP_Query( |
| 92 | [ |
| 93 | 'post_type' => \MeprProduct::$cpt, |
| 94 | 's' => $search, |
| 95 | 'per_page' => 10, |
| 96 | ] |
| 97 | ); |
| 98 | |
| 99 | if ( ( isset( $membership_query->posts ) ) && ( ! empty( $membership_query->posts ) ) ) { |
| 100 | $items = array_map( |
| 101 | function( $post ) { |
| 102 | return (object) [ |
| 103 | 'id' => $post->ID, |
| 104 | 'label' => $post->post_title, |
| 105 | ]; |
| 106 | }, |
| 107 | $membership_query->posts |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | return $items; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the individual item. |
| 116 | * |
| 117 | * @param string $id Id for the record. |
| 118 | * |
| 119 | * @return object The item for the integration. |
| 120 | */ |
| 121 | public function getItem( $id ) { |
| 122 | $course = get_post( $id ); |
| 123 | if ( ! $course ) { |
| 124 | return []; |
| 125 | } |
| 126 | return (object) [ |
| 127 | 'id' => $id, |
| 128 | 'provider_label' => __( 'MemberPress Membership', 'surecart' ), |
| 129 | 'label' => $course->post_title, |
| 130 | ]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Enable Access to the course. |
| 135 | * |
| 136 | * @param \SureCart\Models\Integration $integration The integrations. |
| 137 | * @param \WP_User $wp_user The user. |
| 138 | * |
| 139 | * @return boolean|void Returns true if the user course access updation was successful otherwise false. |
| 140 | */ |
| 141 | public function onPurchaseCreated( $integration, $wp_user ) { |
| 142 | $this->updateAccess( $integration->integration_id, $wp_user, true ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Enable access when purchase is invoked |
| 147 | * |
| 148 | * @param \SureCart\Models\Integration $integration The integrations. |
| 149 | * @param \WP_User $wp_user The user. |
| 150 | * |
| 151 | * @return boolean|void Returns true if the user course access updation was successful otherwise false. |
| 152 | */ |
| 153 | public function onPurchaseInvoked( $integration, $wp_user ) { |
| 154 | $this->onPurchaseCreated( $integration, $wp_user ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Remove a user role. |
| 159 | * |
| 160 | * @param \SureCart\Models\Integration $integration The integrations. |
| 161 | * @param \WP_User $wp_user The user. |
| 162 | * |
| 163 | * @return boolean|void Returns true if the user course access updation was successful otherwise false. |
| 164 | */ |
| 165 | public function onPurchaseRevoked( $integration, $wp_user ) { |
| 166 | $this->updateAccess( $integration->integration_id, $wp_user, false ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Update access to a course. |
| 171 | * |
| 172 | * @param integer $membership_id The membership product id. |
| 173 | * @param \WP_User $wp_user The user. |
| 174 | * @param boolean $add True to add the user to the course, false to remove. |
| 175 | * |
| 176 | * @return boolean|void Returns true if the user course access updation was successful otherwise false. |
| 177 | */ |
| 178 | public function updateAccess( $membership_id, $wp_user, $add = true ) { |
| 179 | // we don't have Memberpress installed. |
| 180 | if ( ! class_exists( 'MeprTransaction' ) ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | // make sure we have a purchase id. |
| 185 | if ( empty( $this->getPurchaseId() ) ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // get order, invoice needed to sync the purchase. |
| 190 | $purchase = Purchase::with( [ 'order', 'invoice' ] )->find( $this->getPurchaseId() ); |
| 191 | $status = $add ? 'completed' : 'failed'; |
| 192 | $existing = false; |
| 193 | $transaction_num = $purchase->id; |
| 194 | $existing_transaction = \MeprTransaction::get_one_by_trans_num( $transaction_num ); |
| 195 | |
| 196 | // get purchase order or invoice. |
| 197 | if ( ! empty( $purchase->order->id ) ) { |
| 198 | $object = $purchase->order; |
| 199 | } |
| 200 | if ( ! empty( $purchase->invoice->id ) ) { |
| 201 | $object = $purchase->invoice; |
| 202 | } |
| 203 | |
| 204 | // It doesn't exist. |
| 205 | if ( ! isset( $existing_transaction->id ) || empty( $existing_transaction->id ) ) { |
| 206 | $transaction = new \MeprTransaction(); |
| 207 | $transaction->amount = $this->convertAmount( $object->amount_due, $object->currency ); |
| 208 | $transaction->total = $this->convertAmount( $object->total_amount, $object->currency ); |
| 209 | $transaction->tax_amount = $this->convertAmount( $object->tax_amount, $object->currency ); |
| 210 | $transaction->user_id = $wp_user->ID; |
| 211 | $transaction->product_id = $membership_id; |
| 212 | $transaction->trans_num = $transaction_num; |
| 213 | $transaction->txn_type = \MeprTransaction::$payment_str; |
| 214 | $transaction->gateway = 'manual'; |
| 215 | $transaction->created_at = gmdate( 'c' ); |
| 216 | $transaction->expires_at = \MeprUtils::mysql_lifetime(); |
| 217 | } else { |
| 218 | // It does exist. |
| 219 | $transaction = new \MeprTransaction( $existing_transaction->id ); |
| 220 | $existing = true; |
| 221 | } |
| 222 | |
| 223 | // Fire some hooks for Corporate Accounts. |
| 224 | if ( ! $existing ) { |
| 225 | $transaction->status = \MeprTransaction::$pending_str; |
| 226 | $transaction->store(); |
| 227 | do_action( 'mepr-signup', $transaction ); |
| 228 | } |
| 229 | |
| 230 | // Set the txn's status. |
| 231 | if ( 'failed' === $status ) { |
| 232 | $transaction->status = \MeprTransaction::$failed_str; |
| 233 | } else { |
| 234 | $transaction->status = \MeprTransaction::$complete_str; |
| 235 | } |
| 236 | |
| 237 | // Store the transaction. |
| 238 | return $transaction->store(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Maybe convert to non-zero decimal. |
| 243 | * |
| 244 | * @param integer $amount The amount as an integer. |
| 245 | * @param string $currency The currency. |
| 246 | * |
| 247 | * @return float|integer The new amount. |
| 248 | */ |
| 249 | public function convertAmount( $amount, $currency ) { |
| 250 | return Currency::isZeroDecimal( $currency ) ? $amount : round( $amount / 100, 2 ); |
| 251 | } |
| 252 | } |
| 253 |