Upon trying to use the Purchase system all my packages received the following error upon trying to follow the navigation to the Paypal portal:
Turns out the issue was due to float precision on the "amount" post parameter:
Found the problematic line in the adcredit_packages template:
Fixed it by replacing it with:
Code:
<input type="hidden" name="amount" value="{xen:helper currency, $package.price, 0}" />
Hopefully this fix can be implemented (or improved on) for the official build. Would save me the hassle of having to modify it each upgrade.
---
Here's another revision I had to make for my use case.
class Audentio_Credit_Core_DataWriter_Transaction
Old:
Code:
protected function _preSave()
{
if ($this->get('credits') < 1)
{
$this->error(new XenForo_Phrase('adcredit_invalid_amount'), 'amount');
}
$currencyModel = $this->getModelFromCache('Audentio_Credit_Core_Model_Currency');
$currency = $currencyModel->getCurrencyById($this->get('currency_id'));
$sender = $this->getOption('sender');
if ($sender)
{
if ($this->get('credits') > $sender[$currency['currency_id']] && !$currency['allow_negative'])
{
$this->error(new XenForo_Phrase('adcredit_not_enough_X_you_need_Y',array('creditName' => $currency['name'], 'amount'=>Audentio_Credit_Core_Template_Helper_Credit::formatCredit($this->get('credits'), $this->get('currency_id')))), 'amount');
}
}
}
New:
Code:
protected function _preSave()
{
$currencyModel = $this->getModelFromCache('Audentio_Credit_Core_Model_Currency');
$currency = $currencyModel->getCurrencyById($this->get('currency_id'));
$min = 1 / pow(10, $currency['decimal_points']) - 1 / pow(10, $currency['decimal_points'] + 1);
if ($this->get('credits') <= $min)
{
$this->error(new XenForo_Phrase('adcredit_invalid_amount'), 'amount');
}
$sender = $this->getOption('sender');
if ($sender && $this->isInsert() && $this->get('credits') > $sender[$currency['currency_id']] && !$currency['allow_negative'])
{
$this->error(new XenForo_Phrase('adcredit_not_enough_X_you_need_Y',array('creditName' => $currency['name'], 'amount'=>Audentio_Credit_Core_Template_Helper_Credit::formatCredit($this->get('credits'), $this->get('currency_id')))), 'amount');
}
}
The first notable change are that it now supports transfers of the smallest precision allowed by the currency (as configured). Apologies for the second hacky part of calculating the min, but it was necessary because I believe there is some precision loss going through the AJAX layer and I'm sure you can come up with a more elegant fix. The second is the addition of "$this->isInsert()" condition to the insufficient funds error, because for my use, and presumably anyone building on top of this, there may be a need to edit transactions after it is created and that error state makes no sense then.
Lastly, please let me know if you would like me to continue posting my mods/thoughts/etc., PM you instead, or just leave you alone. Each dev has their own style and I'll respect it.
---
Edit:
More tweaks I made that I thought I should share. I really wanted to a display like this (yes I'm using the AD theme Intrepid; it's awesome
)
To achieve this I modified the beginning of template adcredit_navigation_visitor_tabs_end as such:
Code:
<li class="navTab credits Popup PopupControl PopupClosed {xen:if $adcredit_selectedTab, 'selected'}">
<a href="{xen:link adcredits}" rel="Menu" class="navLink NoPopupGadget">
{xen:if '{$xenOptions.adcreidt_visitorTabDisplayMethod} == 2 OR {$xenOptions.adcreidt_visitorTabDisplayMethod} == 1', '<i class="{$xenOptions.adcreidt_visitorTabIconClass}"></i> '}{xen:if '{$xenOptions.adcreidt_visitorTabDisplayMethod} == 0 OR {$xenOptions.adcreidt_visitorTabDisplayMethod} == 1', '{xen:phrase adcredit_wallet}'}
<strong class="itemCount alert" id="PrimaryCurrency_Counter">
<span class="Total">{xen:helper credit, '{$visitor.adcredit}', 'adcredit'}</span>
<span class="arrow"></span>
</strong>
</a>
Obviously this is horrible since I hard-coded the currency_id, but I'm sure it can be improved on to provide such a display option out-of-box.
Edit x2:
Couple more notes.
1) I would like to have a space between the end of my currency and the suffix. I managed to do this by manually modifying the currency table, but trying to do some through admincp trims the leading space.
2) I want only one currency withdrawable and the other not. I tried to turn off "Can Withdraw" through the Currency page in admincp:
However, the currency still shows up in the Withdraw template and refreshing the admincp page shows that the option has been checked again.
I'm assuming this is undesired behavior. I also didn't notice a can_withdraw flag in the MySQL table, so I'm not sure how the setting is retained.
Edit x3:
Fixed* #2 above by replacing the following line in template adcredit_withdraw
Code:
<xen:if is="{$currency.withdraw_conversion_rate}">
with
Code:
<xen:if is="{$currency.withdraw_conversion_rate} > 0">
* While this fixes the front-end I fear that the back-end will still allow a withdrawal to be made on a currency that has withdrawal disabled (or conversion_rate == 0).
Edit x4:
Gifting is also susceptible to the same double precision bug documented at the beginning. The fix involves modifying template adcredit_package_purchase_do in a similar fashion. Also the lower case "submit" button using the submit phrase looks a little improper. I replaced it with the go phrase, which is properly capitalized.
Code:
<form action="{$payPalUrl}" method="post" class="xenForm formOverlay">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="amount" value="{xen:helper currency, $package.price, 0}" />
<dl class="ctrlUnit submitUnit">
<dt></dt>
<dd style="text-align: center;">
{xen:phrase adcredit_click_here_if_redirect_takes_longer_than_X_seconds, 'seconds=<span id="numSeconds">5</span>'}<br>
<input type="submit" value="{xen:phrase go}" class="button primary">
</dd>
</dl>