1

I am facing a problem. I have made payment through paypal. After payment when it redirect to my website, i have a code which update the value in database. But when i refresh the same page, it automatically update its value. I only want to update value only on making a payment not on refreshing. I am using the code

$db  = JFactory::getDBO();
$result = null;     
$user    = JFactory::getUser();

if ($user->guest) {
    return false;
}

$query = 'SELECT credit' .
         'FROM #__vodes_credits' .
         'WHERE userid = ' . (int) $user->id;
$db->setQuery($query);
$result = $db->loadResult();

$result_final = $result+20;

$query = 'update #__vodes_credits SET credit='.$result_final.
         'WHERE userid = ' . (int) $user->id;
//echo $query;
$db->setQuery($query);

$result = $db->loadResult();
if ($db->getErrorNum()) {
    JError::raiseWarning( 500, $db->stderr());
}

return $result;

$app = JFactory::getApplication();
$app->redirect("localhost/Joomla/");

Please help me to sought it out.

2
  • 1
    A solution could by using a "transactions" table. Each time a user wants buy credits, you must insert a row in transaction table with an ID invoice and default state. This ID is sent to paypal through "invoice" hidden field in your form. When Paypal redirects the user to your website, this ID is in the URL so you can fetch the transaction and verify their state before update your credits table Commented Oct 29, 2014 at 13:41
  • 2
    Dayron you probably want to enter that as an answer, not a comment. It provides a possible solution.
    – Toni Marie
    Commented Nov 4, 2014 at 15:47

1 Answer 1

1

Your code is fine until the last three lines. I am assuming this code is in the controller method whose name corresponds to the task parameter in the url paypal is invoking.

return $result;

$app = JFactory::getApplication();
$app->redirect("localhost/Joomla/");

You should not be returning the result. You're skipping the redirection part which is going to achieve your goal.

Once you parse the result of a sensitive form, it is good practice to redirect so any refresh / reload won't trigger the same actions again. But you're returning before the redirect is set.

Additionally, I don't think redirecting to "localhost/Joomla" really makes sense, usually you would redirect implicitly with setRedirect() to a view of your component where you show the payment details, and passing in a message to display to the user, just to let them know you processed the payment correctly.

To sum it up, your code should end with:

$app->setRedirect(JRoute::_("index.php?option=com_yourcomponent&view=paymentsuccess"),JText::_("HAPPY_PAYMENT_MESSAGE"));

with no returns;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.