public function cancelSubscription($userId, $validateUsersCount = true)
 {
     $result = ['error' => 1, 'message' => "Error canceling subscription. Please contact support about this issue"];
     $ipn = new SubscriptionsIPN();
     $ipn->configureDatabase($userId);
     /* clear subscription plan restrictions */
     unset($_SESSION['subscriptionPlanOptions']);
     /**/
     $gsSubscriptions = Subscription::getActiveSubscriptions();
     if (count($gsSubscriptions) != 1) {
         PayPalIPN::log(['message' => "Error. Can\\'t cancel subscription. User has no active subscriptions", 'level' => PayPalIPN::LOG_LEVEL_ERROR, 'user_id' => $userId]);
     }
     $subscription = $gsSubscriptions[0];
     $subscriptionPlan = new SubscriptionPlan();
     $subscriptionPlan->load($subscription['plan_id']);
     if (!$this->validateUsersCount() && $validateUsersCount) {
         $result = ['error' => 2, 'message' => 'Error, wrong users count'];
     }
     /* free subscription */
     if ($subscriptionPlan->service_provider == 'getscorecard' && $subscriptionPlan->service_name == 'free') {
         $recordId = $subscription['subId'];
         $subscription = new Subscription();
         $subscription->load($recordId);
         $subscription->status = Subscription::STATUS_CANCELED;
         $subscription->save();
         $result = ['error' => 0, 'message' => 'success. Subscription canceling.'];
     } else {
         if (!$subscription['subscription_id']) {
             PayPalIPN::log(['message' => "Error. Can\\'t cancel subscription. Wrong subscription id", 'level' => PayPalIPN::LOG_LEVEL_ERROR, 'user_id' => $userId]);
         }
         $lastTransaction = $ipn->getLastActiveTransactionBySubscription($subscription['subId']);
         $transactionId = $lastTransaction['txn_id'];
         $subscriptionId = $subscription['subscription_id'];
         $refundAmount = $ipn->getTransactionRefundAmount($lastTransaction);
         $cancelSubscriptionResult = $ipn->changeSubscriptionStatus($subscriptionId, 'Cancel');
         $refundTransactionResult = $ipn->refundTransaction($lastTransaction, $refundAmount);
         if ($cancelSubscriptionResult['ACK'] == "Success" && $refundTransactionResult['ACK'] == "Success") {
             $subscription = new Subscription();
             $subscription->loadBySubscriptionId($subscriptionId);
             $subscription->status = Subscription::STATUS_CANCELING;
             $subscription->save();
             $result = ['error' => 0, 'message' => 'success. Subscription canceling.'];
             PayPalIPN::log(['txn_id' => $transactionId, 'subscr_id' => $subscriptionId, 'message' => "Subscription canceling", 'level' => PayPalIPN::LOG_LEVEL_INFO, 'data' => '', 'user_id' => $userId]);
         } else {
             PayPalIPN::log(['txn_id' => $transactionId, 'subscr_id' => $subscriptionId, 'message' => "Error. Can\\'t cancel subscription. Rejected by PayPal", 'level' => PayPalIPN::LOG_LEVEL_ERROR, 'data' => json_encode(['cancelSubscriptionResult' => $cancelSubscriptionResult, 'refundTransactionResult' => $refundTransactionResult]), 'user_id' => $userId]);
         }
     }
     return $result;
 }
Exemple #2
0
                $payment_type = (int) Configuration::get('PS_OS_PAYPAL');
                $message = $this->l('Pending payment confirmation.') . '<br />';
            } else {
                $payment_type = (int) Configuration::get('PS_OS_ERROR');
                $message = $this->l('Cart changed, please retry.') . '<br />';
            }
        }
        return array('message' => $message, 'payment_type' => (int) $payment_type);
    }
    public function getResult()
    {
        if ((int) Configuration::get('PAYPAL_SANDBOX') == 1) {
            $action_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate';
        } else {
            $action_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate';
        }
        $request = '';
        foreach ($_POST as $key => $value) {
            $value = urlencode(Tools::stripslashes($value));
            $request .= "&{$key}={$value}";
        }
        $handle = fopen(dirname(__FILE__) . '/log.txt', 'w+');
        fwrite($handle, $action_url . $request);
        return Tools::file_get_contents($action_url . $request);
    }
}
if (Tools::getIsset('custom')) {
    $ipn = new PayPalIPN();
    $custom = Tools::jsonDecode(Tools::getValue('custom'), true);
    $ipn->confirmOrder($custom);
}