AdditionalOptions.class.php
3 months ago
CampaignInfo.class.php
3 months ago
Campaigns.class.php
3 months ago
ClickTracking.class.php
3 months ago
ContactForm7.class.php
3 months ago
Debugging.class.php
2 months ago
EDD.class.php
2 months ago
General.class.php
2 months ago
Integrations.class.php
3 months ago
LifterLMS.class.php
3 months ago
Marketpress.class.php
2 months ago
MemberPress.class.php
3 months ago
Membership2.class.php
3 months ago
PMPro.class.php
3 months ago
PPBuyNowButton.class.php
3 months ago
RestrictContentPro.class.php
3 months ago
S2Member.class.php
3 months ago
Signup.class.php
3 months ago
SimplePayPro.class.php
3 months ago
StripePayments.class.php
2 months ago
SureCart.class.php
3 months ago
WPEasyCart.class.php
3 months ago
WPPayForms.class.php
3 months ago
WishListMember.class.php
3 months ago
WooComm.class.php
2 months ago
Membership2.class.php
215 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class postaffiliatepro_Form_Settings_Membership2 extends postaffiliatepro_Form_Base { |
| 5 | const MEMBERSHIP2_COMMISSION_ENABLED = 'membership2-commission-enabled'; |
| 6 | const MEMBERSHIP2_CONFIG_PAGE = 'membership2-config-page'; |
| 7 | const MEMBERSHIP2_TRACK_REFUNDS = 'membership2-track-refunds'; |
| 8 | |
| 9 | public function __construct() { |
| 10 | parent::__construct(self::MEMBERSHIP2_CONFIG_PAGE, 'options.php'); |
| 11 | } |
| 12 | |
| 13 | protected function getTemplateFile() { |
| 14 | return WP_PLUGIN_DIR . '/postaffiliatepro/Template/Membership2Config.xtpl'; |
| 15 | } |
| 16 | |
| 17 | protected function initForm() { |
| 18 | $this->addCheckbox(self::MEMBERSHIP2_TRACK_REFUNDS); |
| 19 | |
| 20 | $this->addSubmit(); |
| 21 | } |
| 22 | |
| 23 | public function initSettings() { |
| 24 | register_setting(postaffiliatepro::INTEGRATIONS_SETTINGS_PAGE_NAME, self::MEMBERSHIP2_COMMISSION_ENABLED); |
| 25 | register_setting(self::MEMBERSHIP2_CONFIG_PAGE, self::MEMBERSHIP2_TRACK_REFUNDS); |
| 26 | } |
| 27 | |
| 28 | public function addPrimaryConfigMenu() { |
| 29 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) === 'true') { |
| 30 | add_submenu_page( |
| 31 | 'integrations-config-page-handle', |
| 32 | 'Membership 2 Pro', |
| 33 | 'Membership 2 Pro', |
| 34 | 'manage_options', |
| 35 | 'membership2integration-settings-page', |
| 36 | array($this, 'printConfigPage') |
| 37 | ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function printConfigPage() { |
| 42 | $this->render(); |
| 43 | } |
| 44 | |
| 45 | public function Membership2TrackSale($invoice, $subscription) { |
| 46 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) !== 'true') { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | $papCookie = ''; |
| 51 | $accountID = ''; |
| 52 | $visitorID = ''; |
| 53 | if (isset($_REQUEST['pap_custom']) && (sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])) != '')) { |
| 54 | $papCookie = sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])); |
| 55 | } |
| 56 | $visitorID = substr($papCookie,-32); |
| 57 | $accountID = substr($papCookie,0,8); |
| 58 | |
| 59 | if ($subscription->is_trial_eligible()) { |
| 60 | $total = $invoice->trial_price; |
| 61 | } else { |
| 62 | $total = $invoice->total; |
| 63 | } |
| 64 | |
| 65 | $query = 'AccountId='.$accountID. '&visitorId='.$visitorID.'&TotalCost='.$total.'&ProductID='.$subscription->get_membership()->id.'&OrderID='.$subscription->id; |
| 66 | |
| 67 | if (isset($_POST['payer_email'])) { |
| 68 | $user = get_user_by('email', sanitize_email(wp_unslash($_POST['payer_email']))); |
| 69 | $query .= '&Data1='.$user->ID; |
| 70 | } |
| 71 | |
| 72 | if (isset($_REQUEST['pap_IP']) && ($ip = filter_var(wp_unslash($_REQUEST['pap_IP']), FILTER_VALIDATE_IP))) { |
| 73 | $query .= '&ip='.$ip; |
| 74 | } |
| 75 | |
| 76 | self::sendRequest(postaffiliatepro::parseSaleScriptPath(), $query); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | public function Membership2StripeTransaction($invoice, $gateway) { |
| 81 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) !== 'true') { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | $subscription = $invoice->get_subscription(); |
| 86 | $member = $subscription->get_member(); |
| 87 | $customer = $gateway->_api->find_customer($member); |
| 88 | |
| 89 | $accountID = ''; |
| 90 | $visitorID = ''; |
| 91 | $pap_custom = ''; |
| 92 | if (!empty($customer)) { |
| 93 | $pap_custom = $customer->description; |
| 94 | } |
| 95 | |
| 96 | if (isset($pap_custom) && ($pap_custom != '')) { |
| 97 | $visitorID = substr($pap_custom,-32); |
| 98 | $accountID = substr($pap_custom,0,8); |
| 99 | } |
| 100 | |
| 101 | if ($subscription->is_trial_eligible()) { |
| 102 | $total = $invoice->trial_price; |
| 103 | } else { |
| 104 | $total = $invoice->total; |
| 105 | } |
| 106 | |
| 107 | $query = 'AccountId='.$accountID.'&visitorId='.$visitorID.'&TotalCost='.$total.'&ProductID='.$subscription->get_membership()->id.'&OrderID='.$subscription->id; |
| 108 | |
| 109 | $user = get_user_by('email', $member->email); |
| 110 | $query .= '&Data1='.$user->ID; |
| 111 | |
| 112 | self::sendRequest(postaffiliatepro::parseSaleScriptPath(), $query); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | public function Membership2TrackRefund($invoice, $subscription) { |
| 117 | if (get_option(self::MEMBERSHIP2_TRACK_REFUNDS) !== 'true') { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | $session = $this->getApiSession(); |
| 122 | if ($session === null || $session === '0') { |
| 123 | $this->_log('We have no session to PAP installation! Transaction status change failed.'); |
| 124 | return false; |
| 125 | } |
| 126 | $ids = $this->getTransactionIDsByOrderID($subscription->id, $session, 'A,P', 1); |
| 127 | if (empty($ids)) { |
| 128 | $this->_log('Nothing to change, the commission does not exist in PAP'); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | $request = new Gpf_Rpc_FormRequest('Pap_Merchants_Transaction_TransactionsForm', 'makeRefundChargeback', $session); |
| 133 | $request->addParam('ids', new Gpf_Rpc_Array($ids)); |
| 134 | $request->addParam('status', 'R'); |
| 135 | $request->addParam('merchant_note', 'Refunded automatically from WooCommerce'); |
| 136 | try { |
| 137 | $request->sendNow(); |
| 138 | } catch (Exception $e) { |
| 139 | $this->_log('A problem occurred while transaction status change with API: ' . $e->getMessage()); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | public function Membership2PayPalButton($html, $subscription, $gateway) { |
| 147 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) !== 'true') { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | $ip = 'pap_IP='.postaffiliatepro::getRemoteIp().'&'; |
| 152 | $snippet = '<!-- Post Affiliate Pro integration snippet -->'; |
| 153 | |
| 154 | $contentToAsync = 'PostAffTracker.setAccountId(\''.postaffiliatepro_Base::getAccountName().'\'); |
| 155 | PostAffTracker.writeCookieToCustomField(\'notify_url\', \'\', \''.$ip.'pap_custom\');'; |
| 156 | |
| 157 | if (get_option(postaffiliatepro::ASYNC_ENABLED) != 'true') { |
| 158 | $result = postaffiliatepro::getPAPTrackJSDynamicCode().'<script type="text/javascript">'; |
| 159 | $result .= $contentToAsync . "</script>\n"; |
| 160 | } else { |
| 161 | $result = postaffiliatepro::getPAPTrackJSAsyncCode($contentToAsync); |
| 162 | } |
| 163 | |
| 164 | $snippet .= $result.'<!-- /Post Affiliate Pro integration snippet -->'; |
| 165 | |
| 166 | return str_replace('</form>', '</form>'.$snippet, $html); |
| 167 | } |
| 168 | |
| 169 | public function Membership2StripeButton($html, $subscription, $gateway) { |
| 170 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) !== 'true') { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | $snippet = postaffiliatepro::addHiddenFieldToPaymentForm(true); |
| 175 | $html = str_replace('</form>', $snippet.'</form>', $html); |
| 176 | return $html; |
| 177 | } |
| 178 | |
| 179 | public function Membership2StripeCustomer($customer, $member, $gateway) { |
| 180 | if (get_option(self::MEMBERSHIP2_COMMISSION_ENABLED) !== 'true') { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | $desc = ''; |
| 185 | if (isset($_REQUEST['pap_custom'])) { |
| 186 | $desc = sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])); |
| 187 | } |
| 188 | if (empty($customer)) { |
| 189 | $customer = Stripe_Customer::create( |
| 190 | array( |
| 191 | 'card' => $token, |
| 192 | 'email' => $member->email, |
| 193 | 'description' => $desc |
| 194 | ) |
| 195 | ); |
| 196 | $member->set_gateway_profile($gateway::ID, 'customer_id', $customer->id); |
| 197 | $member->save(); |
| 198 | } |
| 199 | return $customer; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | $papSubmenuPriority = 23; |
| 204 | $papIntegrationMembership2 = new postaffiliatepro_Form_Settings_Membership2(); |
| 205 | add_action('admin_init', array($papIntegrationMembership2, 'initSettings'), 99); |
| 206 | add_action('admin_menu', array($papIntegrationMembership2, 'addPrimaryConfigMenu'), $papSubmenuPriority); |
| 207 | |
| 208 | add_action('ms_gateway_paypalstandard_payment_processed_', array($papIntegrationMembership2, 'Membership2TrackSale'), 99, 2); |
| 209 | add_action('ms_gateway_paypalstandard_payment_processed_paid', array($papIntegrationMembership2, 'Membership2TrackSale'), 99, 2); |
| 210 | add_action('ms_gateway_paypalstandard_payment_processed_denied', array($papIntegrationMembership2, 'Membership2TrackRefund'), 99, 2); |
| 211 | add_filter('ms_controller_gateway_purchase_button_paypalstandard', array($papIntegrationMembership2, 'Membership2PayPalButton'), 99, 3); |
| 212 | add_filter('ms_controller_gateway_purchase_button_stripeplan', array($papIntegrationMembership2, 'Membership2StripeButton'), 99, 3); |
| 213 | add_filter('ms_gateway_stripe_find_customer', array($papIntegrationMembership2, 'Membership2StripeCustomer'), 1, 3); |
| 214 | add_filter('ms_gateway_stripe_process_purchase', array($papIntegrationMembership2, 'Membership2StripeTransaction'), 1, 2); |
| 215 |