Skip to content

Commit

Permalink
Merge branch 'main' of github.com:craftcms/stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Apr 25, 2024
2 parents 4f2dc5b + 6f2d50f commit 3304266
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 102 deletions.
10 changes: 5 additions & 5 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ public function actionIndex(?Settings $settings = null): Response
public function actionSaveSettings(): ?Response
{
$settings = Craft::$app->getRequest()->getParam('settings');
$routingSettings = Craft::$app->getRequest()->getParam('routingSettings');
$plugin = Plugin::getInstance();

/** @var Settings $pluginSettings */
$pluginSettings = $plugin->getSettings();

if (isset($settings['routing'])) {
if (isset($routingSettings['routing'])) {
$originalUriFormat = $pluginSettings->productUriFormat;

// Remove from editable table namespace
$settings['productUriFormat'] = $settings['routing']['productUriFormat'];
$settings['productUriFormat'] = $routingSettings['routing']['productUriFormat'];
// Could be blank if in headless mode
if (isset($settings['routing']['productTemplate'])) {
$settings['productTemplate'] = $settings['routing']['productTemplate'];
if (isset($routingSettings['routing']['productTemplate'])) {
$settings['productTemplate'] = $routingSettings['routing']['productTemplate'];
}
unset($settings['routing']);
}

$settingsSuccess = true;
Expand Down
22 changes: 11 additions & 11 deletions src/elements/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use craft\base\NestedElementTrait;
use craft\db\Query;
use craft\db\Table as CraftTable;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\User;
use craft\helpers\Db;
use craft\helpers\Html;
Expand Down Expand Up @@ -72,7 +71,7 @@ class Price extends Element implements NestedElementInterface
/**
* @var string|null
*/
public ?string $priceType = null;
public ?string $type = null;

/**
* @var string|null
Expand All @@ -89,6 +88,11 @@ class Price extends Element implements NestedElementInterface
*/
public ?string $stripeProductId = null;

/**
* @var float|null
*/
public ?float $unitAmount = null;

/**
* @var array|null
*/
Expand Down Expand Up @@ -344,17 +348,13 @@ protected static function defineSortOptions(): array
unset($sortOptions['pricePerUnit']);
unset($sortOptions['interval']);
unset($sortOptions['currency']);
unset($sortOptions['type']);

$sortOptions['title'] = self::displayName();

$sortOptions['stripeId'] = [
'label' => Craft::t('stripe', 'Stripe ID'),
'orderBy' => 'stripe_pricedata.stripeId',
'defaultDir' => SORT_DESC,
];
$sortOptions['priceType'] = [
'label' => Craft::t('stripe', 'Price Type'),
'orderBy' => 'stripe_pricedata.priceType',
'orderBy' => 'stripeId',
'defaultDir' => SORT_DESC,
];

Expand All @@ -367,7 +367,7 @@ protected static function defineTableAttributes(): array
'primaryCurrency' => Craft::t('stripe', 'Primary Currency'),
'stripeId' => Craft::t('stripe', 'Stripe ID'),
'stripeEdit' => Craft::t('stripe', 'Stripe Edit'),
'priceType' => Craft::t('stripe', 'Price Type'),
'type' => Craft::t('stripe', 'Type'),
'unitPrice' => Craft::t('stripe', 'Unit Price'),
'pricePerUnit' => Craft::t('stripe', 'Price per Unit'),
'interval' => Craft::t('stripe', 'Interval'),
Expand All @@ -387,7 +387,7 @@ protected static function defineDefaultTableAttributes(string $source): array
return [
'stripeId',
'stripeStatus',
'priceType',
'type',
'unitPrice',
];
}
Expand Down Expand Up @@ -531,7 +531,7 @@ protected function attributeHtml(string $attribute): string
return match ($attribute) {
'stripeEdit' => Html::a('', $this->getStripeEditUrl(), ['target' => '_blank', 'data' => ['icon' => 'external']]),
'stripeStatus' => $this->getStripeStatusHtml(),
'priceType' => $this->priceType ?? '',
'type' => $this->type ?? '',
'pricePerUnit' => $this->pricePerUnit(),
'unitPrice' => $this->unitPrice(),
'currency' => strtoupper($this->primaryCurrency),
Expand Down
3 changes: 0 additions & 3 deletions src/elements/conditions/prices/CurrencyConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

namespace craft\stripe\elements\conditions\prices;

use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\conditions\BaseTextConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\StringHelper;
use craft\stripe\elements\db\PriceQuery;
use craft\stripe\elements\Price;
use craft\stripe\enums\PriceType;

/**
* Class CurrencyConditionRule
Expand Down
4 changes: 2 additions & 2 deletions src/elements/conditions/prices/PriceTypeConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getExclusiveQueryParams(): array
public function matchElement(ElementInterface $element): bool
{
/** @var Price $element */
return $this->matchValue($element->priceType);
return $this->matchValue($element->type);
}

/**
Expand All @@ -70,6 +70,6 @@ public function matchElement(ElementInterface $element): bool
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var PriceQuery $query */
$query->priceType($this->paramValue());
$query->type($this->paramValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

namespace craft\stripe\elements\conditions\products;

use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\StringHelper;
use craft\stripe\elements\conditions\prices\PriceTypeConditionRule;
use craft\stripe\elements\db\ProductQuery;
use craft\stripe\elements\Price;
Expand Down Expand Up @@ -40,7 +37,7 @@ public function modifyQuery(ElementQueryInterface $query): void
{
$priceQuery = Price::find();
$priceQuery->select(['stripe_prices.primaryOwnerId as id']);
$priceQuery->priceType($this->paramValue());
$priceQuery->type($this->paramValue());

/** @var ProductQuery $query */
$query->andWhere(['elements.id' => $priceQuery]);
Expand All @@ -54,7 +51,7 @@ public function matchElement(ElementInterface $element): bool
/** @var Product $element */
foreach ($element->getPrices() as $price) {

Check failure on line 52 in src/elements/conditions/products/ProductPriceTypeConditionRule.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Variable $element in PHPDoc tag @var does not match any variable in the foreach loop: $price
/** @var Price $price */
if ($this->matchValue($price->priceType)) {
if ($this->matchValue($price->type)) {
// Skip out early if we have a match
return true;
}
Expand Down
53 changes: 44 additions & 9 deletions src/elements/db/PriceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PriceQuery extends ElementQuery
/**
* @var mixed|null Type of the price one-time or recurring
*/
public mixed $priceType = null;
public mixed $type = null;

/**
* @var mixed|null Price's main currency
Expand All @@ -55,6 +55,11 @@ class PriceQuery extends ElementQuery
*/
public mixed $currency = null;

/**
* @var mixed|null Unit amount
*/
public mixed $unitAmount = null;

/**
* @var mixed|null Stripe id of the product the price is associated with
*/
Expand Down Expand Up @@ -137,23 +142,23 @@ public function stripeId(mixed $value): self
* ```twig
* {# Fetch recurring prices #}
* {% set {elements-var} = {twig-method}
* .priceType('recurring')
* .type('recurring')
* .all() %}
* ```
*
* ```php
* // Fetch recurring prices
* ${elements-var} = {element-class}::find()
* ->priceType(PriceType::Recurring)
* ->type(PriceType::Recurring)
* ->all();
* ```
*/
public function priceType(mixed $value): self
public function type(mixed $value): self
{
if ($value instanceof PriceType) {
$this->priceType = $value->value;
$this->type = $value->value;
} else {
$this->priceType = $value;
$this->type = $value;
}

return $this;
Expand Down Expand Up @@ -209,6 +214,31 @@ public function currency(mixed $value): self
return $this;
}

/**
* Narrows the query results based on the unit amount of the price.
*
* ---
*
* ```twig
* {# Fetch prices where the unit amount is > 10 #}
* {% set {elements-var} = {twig-method}
* .unitAmount('> 10')
* .all() %}
* ```
*
* ```php
* // Fetch prices where the unit amount is > 10
* ${elements-var} = {element-class}::find()
* ->unitAmount('> 10')
* ->all();
* ```
*/
public function unitAmount(mixed $value): self
{
$this->unitAmount = $value;
return $this;
}

/**
* Narrows the query results based on the product associated with the price.
*
Expand Down Expand Up @@ -504,9 +534,10 @@ protected function beforePrepare(): bool
'stripe_prices.stripeId',
'stripe_prices.primaryOwnerId',
'stripe_pricedata.stripeStatus',
'stripe_pricedata.priceType',
'stripe_pricedata.type',
'stripe_pricedata.primaryCurrency',
'stripe_pricedata.currencies',
'stripe_pricedata.unitAmount',
'stripe_pricedata.productId as stripeProductId',
'stripe_pricedata.data',
]);
Expand Down Expand Up @@ -565,8 +596,8 @@ protected function beforePrepare(): bool
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.stripeStatus', $this->stripeStatus));
}

if (isset($this->priceType)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.priceType', $this->priceType));
if (isset($this->type)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.type', $this->type));
}

if (isset($this->primaryCurrency)) {
Expand All @@ -578,6 +609,10 @@ protected function beforePrepare(): bool
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.currencies', $currency));
}

if (isset($this->unitAmount)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.unitAmount', $this->unitAmount));
}

if (isset($this->stripeProductId)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.productId', $this->stripeProductId));
}
Expand Down
26 changes: 3 additions & 23 deletions src/events/CheckoutSessionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace craft\stripe\events;

use craft\stripe\models\Customer;
use Stripe\Checkout\Session;
use yii\base\Event;

/**
Expand All @@ -18,27 +18,7 @@
class CheckoutSessionEvent extends Event
{
/**
* @var string|Customer The Customer model or email address of the customer
* @var Session The Stripe checkout session object
*/
public Customer|string $customer;

/**
* @var array array of prices and quantities for the checkout
*/
public array $lineItems;

/**
* @var string|null Absolute URL to redirect the user to after checkout
*/
public ?string $successUrl = null;

/**
* @var string|null Absolute URL to redirect the user to if they choose to cancel the checkout; e.g. click the back button
*/
public ?string $cancelUrl = null;

/**
* @var array|null Additional params to use to instantiate the checkout session with
*/
public ?array $params = null;
public Session $session;
}
40 changes: 31 additions & 9 deletions src/helpers/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ class Price
];

/**
* Returns price amount as number & currency.
* Returns unit amount as a number
*
* examples:
* £10.50
* $13.35
* ¥1,000
* £6.00 (when the price is: £6.00 per group of 10)
* £10.00 (when the price is: Starts at £10.00 per unit + £0.00)
* Customer chooses
* 10.50
* 13.35
* 1,000
* 6.00 (when the price is: £6.00 per group of 10)
* 10.00 (when the price is: Starts at £10.00 per unit + £0.00)
* 0.00 (when the price is: Customer chooses)
*
* @param mixed $stripePrice
* @return string|null
* @return float|null
* @throws InvalidConfigException
*/
public static function asUnitAmount(mixed $stripePrice): ?string
public static function asUnitAmountNumber(mixed $stripePrice): ?float
{
$unitAmount = $stripePrice['unit_amount'];

Expand All @@ -82,6 +82,28 @@ public static function asUnitAmount(mixed $stripePrice): ?string
$unitAmount = $unitAmount / 100;
}

return $unitAmount;
}

/**
* Returns unit amount as number & currency.
*
* examples:
* £10.50
* $13.35
* ¥1,000
* £6.00 (when the price is: £6.00 per group of 10)
* £10.00 (when the price is: Starts at £10.00 per unit + £0.00)
* Customer chooses
*
* @param mixed $stripePrice
* @return string|null
* @throws InvalidConfigException
*/
public static function asUnitAmount(mixed $stripePrice): ?string
{
$unitAmount = self::asUnitAmountNumber($stripePrice);

return $unitAmount ? Craft::$app->getFormatter()->asCurrency($unitAmount, $stripePrice['currency']) : null;
}

Expand Down
Loading

0 comments on commit 3304266

Please sign in to comment.