Skip to content

Commit

Permalink
Merge pull request #291 from enupal/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
andrelopez authored Jun 2, 2022
2 parents 3f0011c + e99432b commit 3d06ce4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Stripe Payments Changelog

## 5.0.2 - 2022.06.02
### Added
- Added the `craft.enupalStripe.app` service layer to twig variable ([#289])
- Added the `Stripe Prices` field type ([#288])

[#288]: https://github.com/enupal/stripe/issues/288
[#289]: https://github.com/enupal/stripe/issues/289

## 5.0.1 - 2022.05.28
### Fixed
- Fixed issue when creating a Stripe Products field type
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "enupal/stripe",
"description": "Allows customers # for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS 3.x",
"type": "craft-plugin",
"version": "5.0.1",
"version": "5.0.2",
"keywords": [
"craft",
"cms",
Expand Down
7 changes: 6 additions & 1 deletion src/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use enupal\stripe\events\AfterPopulatePaymentFormEvent;
use enupal\stripe\events\OrderCompleteEvent;
use enupal\stripe\events\WebhookEvent;
use enupal\stripe\fields\StripePrices;
use enupal\stripe\fields\StripeProducts;
use enupal\stripe\services\App;
use enupal\stripe\services\Carts;
Expand Down Expand Up @@ -116,7 +117,11 @@ function(Event $event) {

Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = StripePaymentFormsField::class;
$event->types[] = StripeProducts::class;
if (self::getInstance()->is(self::EDITION_PRO)) {
$event->types[] = StripeProducts::class;
$event->types[] = StripePrices::class;
}

});

Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {
Expand Down
23 changes: 21 additions & 2 deletions src/elements/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getCpEditUrl(): ?string
*/
public function __toString(): string
{
$name = $this->getUnitAmount() ?? $this->stripeId;
$name = $this->getLabel() ?? $this->stripeId;

return (string)$name;
}
Expand Down Expand Up @@ -171,13 +171,15 @@ protected static function defineSortOptions(): array
protected static function defineTableAttributes(): array
{
$attributes['stripeId'] = ['label' => StripePlugin::t('Stripe Id')];
$attributes['unitAmount'] = ['label' => StripePlugin::t('Unit Amount')];
$attributes['type'] = ['label' => StripePlugin::t('Type')];

return $attributes;
}

protected static function defineDefaultTableAttributes(string $source): array
{
$attributes = ['stripeId', 'unitAmount'];
$attributes = ['stripeId', 'unitAmount', 'type'];

return $attributes;
}
Expand All @@ -194,6 +196,10 @@ protected function tableAttributeHtml(string $attribute): string
{
return $this->getUnitAmount();
}
case 'type':
{
return $this->getType();
}
}

return parent::tableAttributeHtml($attribute);
Expand Down Expand Up @@ -243,6 +249,19 @@ public function getUnitAmount()
return Craft::$app->getFormatter()->asCurrency($unitAmount, $this->getStripeObject()->currency);
}

public function getType()
{
return $this->getStripeObject()->type ?? null;
}

/**
* @return string|null
*/
private function getLabel()
{
return $this->getStripeObject()->nickname ?? null;
}

public function getStripeObject()
{
if (is_string($this->stripeObject)) {
Expand Down
49 changes: 49 additions & 0 deletions src/fields/StripePrices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Stripe Payments plugin for Craft CMS 3.x
*
* @link https://enupal.com/
* @copyright Copyright (c) 2018 Enupal LLC
*/

namespace enupal\stripe\fields;

use craft\fields\BaseRelationField;
use enupal\stripe\elements\Price;
use enupal\stripe\Stripe as StripePlugin;

/**
* Class StripePrices
*
*/
class StripePrices extends BaseRelationField
{
/**
* @inheritdoc
*/
public bool $allowMultipleSources = false;

/**
* @inheritdoc
*/
public static function displayName(): string
{
return StripePlugin::t('Stripe Prices');
}

/**
* @inheritdoc
*/
public static function elementType(): string
{
return Price::class;
}

/**
* @inheritdoc
*/
public static function defaultSelectionLabel(): string
{
return StripePlugin::t('Add a Stripe Price');
}
}
23 changes: 23 additions & 0 deletions src/variables/StripeVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
namespace enupal\stripe\variables;

use enupal\stripe\elements\db\PaymentFormsQuery;
use enupal\stripe\elements\db\PriceQuery;
use enupal\stripe\elements\db\ProductQuery;
use enupal\stripe\elements\Order;
use enupal\stripe\elements\db\OrdersQuery;
use enupal\stripe\elements\PaymentForm;
use enupal\stripe\elements\Price;
use enupal\stripe\elements\Product;
use enupal\stripe\enums\FrequencyType;
use enupal\stripe\services\App;
use enupal\stripe\services\PaymentForms;
use enupal\stripe\Stripe;
use craft\helpers\Template as TemplateHelper;
Expand All @@ -39,6 +42,11 @@ class StripeVariable extends Behavior
*/
public $paymentForms;

public function app(): App
{
return Stripe::$app;
}

/**
* Returns a new OrderQuery instance.
*
Expand Down Expand Up @@ -69,6 +77,21 @@ public function products($criteria = null): ProductQuery
return $query;
}

/**
* Returns a new PriceQuery instance.
*
* @param mixed $criteria
* @return PriceQuery
*/
public function prices($criteria = null): PriceQuery
{
$query = Price::find();
if ($criteria) {
Craft::configure($query, $criteria);
}
return $query;
}

public function tax()
{
return Stripe::$app->taxes;
Expand Down

0 comments on commit 3d06ce4

Please # to comment.