Skip to content

Commit

Permalink
Adding some features to Stripe plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesegrits committed Jan 12, 2017
1 parent 3a647b9 commit 656d2c6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 34 deletions.
44 changes: 40 additions & 4 deletions plugins/fabrik_form/stripe/forms/fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,26 @@

<field default="0"
class="btn-group"
description="PLG_FORM_STRIPE_EVAL_COST_DESC"
label="PLG_FORM_STRIPE_EVAL_COST_LABEL"
description="PLG_FORM_STRIPE_COST_EVAL_DESC"
label="PLG_FORM_STRIPE_COST_EVAL_LABEL"
name="stripe_cost_eval"
repeat="true"
type="radio">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field default="0"
class="btn-group"
description="PLG_FORM_STRIPE_COST_EVAL_TO_ELEMENT_DESC"
label="PLG_FORM_STRIPE_COST_EVAL_TO_ELEMENT_LABEL"
name="stripe_cost_eval_to_element"
repeat="true"
type="radio">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field description="PLG_FORM_STRIPE_COST_ELEMENT_DESC"
label="PLG_FORM_STRIPE_COST_ELEMENT_LABEL"
name="stripe_cost_element"
Expand All @@ -124,15 +135,26 @@

<field default="0"
class="btn-group"
description="PLG_FORM_STRIPE_EVAL_ITEM_DESC"
label="PLG_FORM_STRIPE_EVAL_ITEM_LABEL"
description="PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_DESC"
label="PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_LABEL"
name="stripe_item_eval"
repeat="true"
type="radio">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field default="0"
class="btn-group"
description="PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_TO_ELEMENT_DESC"
label="PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_TO_ELEMENT_LABEL"
name="stripe_item_eval_to_element"
repeat="true"
type="radio">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field description="PLG_FORM_STRIPE_PURCHASE_ITEM_ELEMENT_DESC"
label="PLG_FORM_STRIPE_PURCHASE_ITEM_ELEMENT_LABEL"
name="stripe_item_element"
Expand Down Expand Up @@ -162,6 +184,20 @@
type="listfields"
valueformat="tableelement"/>

<field name="stripe_charge_bottom_text_new"
type="fabrikeditor"
mode="php"
description="PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW_DESC"
label="PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW_LABEL"
repeat="true"/>

<field name="stripe_charge_bottom_text_existing"
type="fabrikeditor"
mode="php"
description="PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING_DESC"
label="PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING_LABEL"
repeat="true"/>

</fieldset>

<fieldset name="plg-form-stripe-customers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ PLG_FORM_STRIPE_CONDITION_LABEL="Condition"
PLG_FORM_STRIPE_CONDITION_DESC="Optional PHP code. If code returns false, Stripe transaction will not be run."
PLG_FORM_STRIPE_COST_DESC="Enter a value of the transaction. If evaling the code, return a single int or decimal value. Returning false will log an error, halt plugin processing, and show a warning message that the cost could not be determined."
PLG_FORM_STRIPE_COST_LABEL="Cost"
PLG_FORM_STRIPE_EVAL_COST_DESC="Eval the cost field as PHP code"
PLG_FORM_STRIPE_EVAL_COST_LABEL="Eval"
PLG_FORM_STRIPE_COST_ELEMENT_LABEL="Or select an element"
PLG_FORM_STRIPE_COST_ELEMENT_DESC="Select an element that will contain the transaction value"
PLG_FORM_STRIPE_COST_EVAL_DESC="Eval the cost field as PHP code"
PLG_FORM_STRIPE_COST_EVAL_LABEL="Eval"
PLG_FORM_STRIPE_COST_EVAL_TO_ELEMENT_DESC="If yes, and you select an element below, the value you eval above will be assigned to that element when the form is displayed. Useful for showing a read-only element with cost. If set to no and an element is selected, that element's value will be used for the cost."
PLG_FORM_STRIPE_COST_EVAL_TO_ELEMENT_LABEL="Eval to Element"
PLG_FORM_STRIPE_COST_ELEMENT_LABEL="Select an element"
PLG_FORM_STRIPE_COST_ELEMENT_DESC="Optionally select an element that will contain the transaction value (see Eval to Element above)"
PLG_FORM_STRIPE_CURRENCY_MULTIPLIER_DESC="Stripe charges have to be specified in the smallest currency unit, eg. cents for USD currency. So for USD, specify 100, if your cost above is in dollars."
PLG_FORM_STRIPE_CURRENCY_MUTLIPLIER_LABEL="Currency Muliplier"
PLG_FORM_STRIPE_COST_ELEMENT_ERROR="Could not determine cost of item selected!"
PLG_FORM_STRIPE_PURCHASE_ITEM_LABEL="Purchase item"
PLG_FORM_STRIPE_PURCHASE_ITEM_DESC="The name of the product being purchased"
PLG_FORM_STRIPE_PURCHASE_ITEM_ELEMENT_LABEL="Or select an element"
PLG_FORM_STRIPE_PURCHASE_ITEM_ELEMENT_DESC="Select an element that will contain the name of the item being purchased"
PLG_FORM_STRIPE_EVAL_ITEM_LABEL="Eval"
PLG_FORM_STRIPE_EVAL_ITEM_DESC="Eval the purchase item field as PHP code"
PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_LABEL="Eval"
PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_DESC="Eval the purchase item field as PHP code"
PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_TO_ELEMENT_DESC="If yes, and you select an element below, the value you eval above will be assigned to that element when the form is displayed. Useful for showing a read-only element with item description. If set to no and an element is selected, that element's value will be used for the item description."
PLG_FORM_STRIPE_PURCHASE_ITEM_EVAL_TO_ELEMENT_LABEL="Eval to Element"
PLG_FORM_STRIPE_TEST_SECRET_KEY_DESC="Your test Stripe secret key"
PLG_FORM_STRIPE_TEST_SECRET_KEY_LABEL="Test Secret Key"
PLG_FORM_STRIPE_TEST_PUBLISHABLE_KEY_DESC="Your tst Strip publishable key"
Expand All @@ -46,6 +50,12 @@ PLG_FORM_STRIPE_CHARGE_ID_ELEMENT_DESC="The element to store the Stripe charge I
PLG_FORM_STRIPE_CHARGE_ID_ELEMENT_LABEL="Charge ID"
PLG_FORM_STRIPE_CHARGE_EMAIL_ELEMENT_DESC="The element to store the email given by the user in the Stripe card collection process"
PLG_FORM_STRIPE_CHARGE_EMAIL_ELEMENT_LABEL="Charge Email"
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW_DESC="The text to insert at the bottom of the form for new customers (where a Stripe customer ID is not stored in a customer table). If left blank, will default to the language tag PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW. You can use placeholders in this text, including the special placeholders {stripe_item} and {stripe_amount}."
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW_LABEL="Form Text (new)"
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW="Purchase {stripe_item} for {stripe_amount}.<p />When you submit this form, you will be prompted to enter your payment details in a popup form for the Stripe secure payment processing system. Once entered, the form will submit, and you payment will be processed. This site does not store your CC details."
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING_DESC="The text to insert at the bottom of the form for existing customers (where a Stripe customer ID is stored in a customer table). If left blank, will default to the language tag PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING. You can use placeholders in this text,including the special placeholders {stripe_item}, {stripe_amount} and {stripe_last4}."
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING_LABEL="Form Text (existing)"
PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING="Purchase {stripe_item} for {stripe_amount} using your card ending in {stripe_last4}"
PLG_FORM_STRIPE_DIALOG_NAME_DESC="Name that appears at the top of the Strip payment collection dialog, usually your company name"
PLG_FORM_STRIPE_DIALOG_NAME_LABEL="Dialog Name"
PLG_FORM_STRIPE_PAY="Pay"
Expand Down
13 changes: 13 additions & 0 deletions plugins/fabrik_form/stripe/layouts/fabrik-form-stripe-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

$d = $displayData;

if (class_exists('NumberFormatter'))
{
$formatter = new NumberFormatter($d->langTag, NumberFormatter::CURRENCY);
$d->amount = $formatter->formatCurrency($d->amount, $d->currencyCode);
}

$d->bottomText = str_ireplace('{stripe_amount}', $d->amount, $d->bottomText);
$d->bottomText = str_ireplace('{stripe_item}', $d->item, $d->bottomText);

echo $d->bottomText;

/*
<script
src="https://checkout.stripe.com/checkout.js"
Expand All @@ -25,6 +36,8 @@ class="stripe-button"
?>
<button class="stripe-pay">Pay</button>
*/


?>


Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@

$d = $displayData;


if (class_exists('NumberFormatter'))
{
$formatter = new NumberFormatter($d->langTag, NumberFormatter::CURRENCY);
$d->amount = $formatter->formatCurrency($d->amount, $d->currencyCode);

echo FText::sprintf('PLG_FORM_EXISTING_CUSTOMER_PURCHASE', $d->amount, $d->card->last4);
}

$d->bottomText = str_ireplace('{stripe_amount}', $d->amount, $d->bottomText);
$d->bottomText = str_ireplace('{stripe_last4}', $d->card->last4, $d->bottomText);
$d->bottomText = str_ireplace('{stripe_item}', $d->item, $d->bottomText);

echo $d->bottomText;

?>
<!--
<div class="fabrikStripeButtonContainer">
<button class="fabrikStripeChange">
<span>Update CC</span>
</button>
</div>
-->
71 changes: 49 additions & 22 deletions plugins/fabrik_form/stripe/stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ public function getBottomContent()
$opts->allowRememberMe = false;
$opts->zipCode = $params->get('stripe_zipcode_check', '1') === '1';

$currencyCode = $params->get('stripe_currencycode', 'USD');
$currencyCode = $w->parseMessageForPlaceHolder($currencyCode, $this->data);
$opts->currencyCode = $currencyCode;

$amount = $params->get('stripe_cost');
$amount = $w->parseMessageForPlaceHolder($amount, $this->data);

Expand All @@ -389,16 +393,35 @@ public function getBottomContent()
}
}

if (trim($amount) == '')
if ($params->get('stripe_cost_eval_to_element', '0') === '1')
{
// Priority to raw data.
$amountKey = FabrikString::safeColNameToArrayKey($params->get('stripe_cost_element'));
$amount = FArrayHelper::getValue($this->data, $amountKey);
$amount = FArrayHelper::getValue($this->data, $amountKey . '_raw', $amount);

if (is_array($amount))
if (!empty($amountKey))
{
$amount = array_shift($amount);
if (class_exists('NumberFormatter'))
{
$formatter = new NumberFormatter(JFactory::getLanguage()->getTag(), NumberFormatter::CURRENCY);
$formModel->data[$amountKey] = $formatter->formatCurrency($amount, $currencyCode);
}
else
{
$formModel->data[$amountKey] = $amount;
}
}
}
else
{
if (trim($amount) == '')
{
// Priority to raw data.
$amountKey = FabrikString::safeColNameToArrayKey($params->get('stripe_cost_element'));
$amount = FArrayHelper::getValue($this->data, $amountKey);
$amount = FArrayHelper::getValue($this->data, $amountKey . '_raw', $amount);

if (is_array($amount))
{
$amount = array_shift($amount);
}
}
}

Expand All @@ -415,30 +438,29 @@ public function getBottomContent()
$item = @eval($item);
}

$itemRaw = $item;

if (trim($item) == '')
if ($params->get('stripe_item_eval_to_element', '0') === '1')
{
$itemRaw = FArrayHelper::getValue($this->data, FabrikString::safeColNameToArrayKey($params->get('stripe_item_element') . '_raw'));
$item = $this->data[FabrikString::safeColNameToArrayKey($params->get('stripe_item_element'))];

if (is_array($item))
$itemKey = FabrikString::safeColNameToArrayKey($params->get('stripe_item_element'));
if (!empty($itemKey))
{
$item = array_shift($item);
$formModel->data[$itemKey] = $item;
}

if (is_array($itemRaw))
}
else
{
if (trim($item) == '')
{
$itemRaw = array_shift($itemRaw);
$item = $this->data[FabrikString::safeColNameToArrayKey($params->get('stripe_item_element'))];

if (is_array($item))
{
$item = array_shift($item);
}
}
}

$opts->item = $item;

$currencyCode = $params->get('stripe_currencycode', 'USD');
$currencyCode = $w->parseMessageForPlaceHolder($currencyCode, $this->data);
$opts->currencyCode = $currencyCode;

$opts->billingAddress = $params->get('stripe_collect_billing_address', '0') === '1';

/*
Expand Down Expand Up @@ -529,6 +551,8 @@ public function getBottomContent()
$layoutData->amount = $amount;
$layoutData->currencyCode = $currencyCode;
$layoutData->langTag = JFactory::getLanguage()->getTag();
$layoutData->item = $item;
$layoutData->bottomText = FText::_($params->get('stripe_charge_bottom_text_existing', 'PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_EXISTING'));
$this->html = $layout->render($layoutData);
}
else
Expand All @@ -539,6 +563,9 @@ public function getBottomContent()
$layoutData->amount = $amount;
$layoutData->currencyCode = $currencyCode;
$layoutData->langTag = JFactory::getLanguage()->getTag();
$layoutData->bottomText = FText::_($params->get('stripe_charge_bottom_text_new', 'PLG_FORM_STRIPE_CHARGE_BOTTOM_TEXT_NEW'));
$layoutData->bottomText = $w->parseMessageForPlaceHolder($layoutData->bottomText, $this->data);
$layoutData->item = $item;
$this->html = $layout->render($layoutData);
FabrikHelperHTML::script('https://checkout.stripe.com/checkout.js');
}
Expand Down

0 comments on commit 656d2c6

Please sign in to comment.