Custom Payment & Donation Forms
It is often a requirement of non-profits and membership organisations to accept donations on their website.
1) Build a Form
The form contains a <forms:paymentmethod> control and calls a function donate() on submit.
<forms:form id="donation_form" onsubmitredirect="/commerce/order/success/?sale_id=[? $sale_id ?]" contactmode="store" onsubmitserver="donate($this.getValuesForComponent())">
<forms:row label="Full Name">
<forms:editbox id="contact_first_name" width="100" datacolumn="contact_first_name" validations="mandatory" />
<forms:editbox id="contact_last_name" width="120" datacolumn="contact_last_name" validations="mandatory" />
</forms:row>
<forms:row label="E-Mail Address">
<forms:editbox id="contact_email" width="200" datacolumn="contact_email" validations="mandatory" />
</forms:row>
<forms:address validations="mandatory" />
<forms:row label="Contact Phone">
<forms:editbox id="contact_phone" validations="mandatory" />
</forms:row>
<forms:row description="Indicate the amount to donate" label="Donation Amount">
<forms:money id="donation_amount" width="100" datacolumn="donation_amount" validations="mandatory" align="left" />
</forms:row>
<br />
<forms:paymentmethod showpaymentformonzerovalue="true" />
</forms:form>
2) Server Code
In your page's Source > PHP tab, this is where the donate() function is declared.
You can add items to the cart here with a custom amount.
<?php
function donate($arrValues)
{
\Components\Commerce\Carts\Current::removeAllItems();
if(!isset($arrValues['donation_amount']) || !intval($arrValues['donation_amount']))
{
$pException = new \Framework\Exceptions\Validation();
$pException->setTitle('You must enter an amount and type of donation!');
throw $pException;
}
$strDonationTitle = 'Donation';
$arrProduct = ['product_id' => null, 'product_title' => $strDonationTitle, 'product_price' => $arrValues['donation_amount'], 'product_tax_percent' => 0];
\Components\Commerce\Carts\Current::addProduct($arrProduct);
}
?>