PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.13.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.13.0
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Integrations / MemberPress / MemberPressService.php
surecart / app / src / Integrations / MemberPress Last commit date
MemberPressService.php 3 years ago MemberPressServiceProvider.php 3 years ago
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