diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ab51d9..2fbc458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## 5.1.0-beta.2 - 2024-07-08 +## 5.1.0-beta.3 - 2024-07-29 +### Added +- Added an overview for subscribers + +### Fixed +- Fixed an error when canceling a subscription from the CP + +## 5.1.0-beta.2 - 2024-07-07 ### Fixed -- Fix for existing transactions that don't have the ``elementTye`` property yet +- Fix for payments that exist before upgrading to 5.1 ## 5.1.0-beta.1 - 2024-07-04 diff --git a/composer.json b/composer.json index 5e5f441..dcb4238 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "minimum-stability": "alpha", "description": "Easily accept payments with Mollie Payments", "type": "craft-plugin", - "version": "5.1.0-beta.2", + "version": "5.1.0-beta.3", "keywords": [ "craft", "cms", diff --git a/src/MolliePayments.php b/src/MolliePayments.php index 4dedda4..64c444b 100644 --- a/src/MolliePayments.php +++ b/src/MolliePayments.php @@ -129,6 +129,7 @@ function(RegisterUrlRulesEvent $event) { $event->rules['mollie-payments'] = ['template' => 'mollie-payments/_payment/_index.twig']; $event->rules['mollie-payments/payments/'] = 'mollie-payments/payment/edit'; $event->rules['mollie-payments/subscriptions'] = ['template' => 'mollie-payments/_subscription/_index.twig']; + $event->rules['mollie-payments/subscribers'] = 'mollie-payments/subscription/subscriber-overview'; $event->rules['mollie-payments/subscriptions/'] = 'mollie-payments/subscription/edit'; $event->rules['mollie-payments/forms'] = 'mollie-payments/forms/index'; $event->rules['mollie-payments/forms/add'] = 'mollie-payments/forms/edit'; @@ -220,6 +221,11 @@ public function getCpNavItem(): array 'url' => 'mollie-payments/subscriptions', ]; + $subNavs['subscribers'] = [ + 'label' => Craft::t('mollie-payments', 'Subscribers'), + 'url' => 'mollie-payments/subscribers', + ]; + if (Craft::$app->getUser()->getIsAdmin() && Craft::$app->getConfig()->getGeneral()->allowAdminChanges) { $subNavs['forms'] = [ 'label' => Craft::t('mollie-payments', 'Forms'), diff --git a/src/controllers/SubscriptionController.php b/src/controllers/SubscriptionController.php index 17e58d5..4e25021 100644 --- a/src/controllers/SubscriptionController.php +++ b/src/controllers/SubscriptionController.php @@ -6,6 +6,7 @@ use craft\base\Element; use craft\helpers\ConfigHelper; use craft\helpers\UrlHelper; +use craft\web\assets\admintable\AdminTableAsset; use craft\web\Controller; use Mollie\Api\Types\PaymentStatus; use studioespresso\molliepayments\elements\Subscription; @@ -233,10 +234,15 @@ public function actionGetLinkForCustomer() public function actionCancel() { - $this->requirePostRequest(); + if (!$this->request->isCpRequest) { + $this->requirePostRequest(); + $subscription = $this->request->getRequiredBodyParam('subscription'); + $subscriber = $this->request->getRequiredBodyParam('subscriber'); + } else { + $subscription = $this->request->getRequiredQueryParam('subscription'); + $subscriber = $this->request->getRequiredQueryParam('subscriber'); + } - $subscription = $this->request->getRequiredBodyParam('subscription'); - $subscriber = $this->request->getRequiredBodyParam('subscriber'); $subscription = Subscription::findOne(['id' => $subscription]); $subscriber = SubscriberRecord::findOne(['uid' => $subscriber]); @@ -262,4 +268,60 @@ public function actionCancel() return $this->redirectToPostedUrl(); } + + public function actionSubscriberOverview() + { + Craft::$app->getView()->registerAssetBundle(AdminTableAsset::class); + + return $this->asCpScreen() + ->title(Craft::t('mollie-payments', 'Subscriber overview')) + ->contentTemplate('mollie-payments/_subscribers/_index'); + } + + public function actionGetSubscribers() + { + $page = $this->request->getQueryParam('page', 1); + $baseUrl = 'mollie-payments/subscription/get-subscribers'; + $data = MolliePayments::getInstance()->subscriber->getAllSubscribers(); + $subscribers = collect($data)->map(function($subscriber) { + return [ + 'title' => $subscriber->email, + 'id' => $subscriber->customerId, + ]; + }); + + $rows = $subscribers->all(); + + + $total = count($rows); + $limit = $total < 20 ? $total : 20; + $from = ($page - 1) * $limit + 1; + $lastPage = (int)ceil($total / $limit); + $to = $page === $lastPage ? $total : ($page * $limit); + $nextPageUrl = $baseUrl . sprintf('?page=%d', ($page + 1)); + $prevPageUrl = $baseUrl . sprintf('?page=%d', ($page - 1)); + $rows = array_slice($rows, $from - 1, $limit); + + + return $this->asJson([ + 'pagination' => [ + 'total' => (int)$total, + 'per_page' => (int)$limit, + 'current_page' => (int)$page, + 'last_page' => (int)$lastPage, + 'next_page_url' => $nextPageUrl, + 'prev_page_url' => $prevPageUrl, + 'from' => (int)$from, + 'to' => (int)$to, + ], + 'data' => $rows, + ]); + } + + public function actionDeleteSubscriber() + { + MolliePayments::getInstance()->mollie->deleteCustomer($this->request->getRequiredBodyParam('id')); + MolliePayments::getInstance()->subscriber->deleteById($this->request->getRequiredBodyParam('id')); + return $this->asJson(['success' => true]); + } } diff --git a/src/services/Mollie.php b/src/services/Mollie.php index 8295844..f5aa347 100644 --- a/src/services/Mollie.php +++ b/src/services/Mollie.php @@ -7,6 +7,7 @@ use craft\helpers\App; use craft\helpers\ConfigHelper; use craft\helpers\UrlHelper; +use Mollie\Api\Exceptions\ApiException; use Mollie\Api\MollieApiClient; use Mollie\Api\Resources\Customer; use studioespresso\molliepayments\elements\Payment; @@ -190,6 +191,15 @@ public function createCustomer($email): Customer return $customer; } + public function deleteCustomer($id): void + { + try { + $this->mollie->customers->delete($id); + } catch(ApiException $e) { + Craft::error($e->getMessage(), __METHOD__); + } + } + public function getStatus($orderId) { return $this->mollie->payments->get($orderId); diff --git a/src/services/Subscriber.php b/src/services/Subscriber.php index 1234050..4709d76 100644 --- a/src/services/Subscriber.php +++ b/src/services/Subscriber.php @@ -49,11 +49,6 @@ public function getOrCreateSubscriberByEmail($email): SubscriberModel $model->metadata = $customer->metadata ?? ''; $model->links = $customer->_links; - - /*if (Craft::$app->getUser()->getIdentity()) { - $model->userId = Craft::$app->getUser()->getIdentity()->id; - $model->name = Craft::$app->getUser()->getIdentity()->fullName ?? ''; - }*/ $this->save($model); return $model; } @@ -73,4 +68,17 @@ public function save(SubscriberModel $model) $record->links = $model->links; return $record->save(); } + + public function deleteById($id): void + { + $record = SubscriberRecord::findOne(['customerId' => $id]); + if ($record) { + $record->delete(); + } + } + + public function getAllSubscribers() + { + return SubscriberRecord::find()->all(); + } } diff --git a/src/templates/_payment/_transactions.twig b/src/templates/_payment/_transactions.twig index 071f8fc..f79f7ea 100644 --- a/src/templates/_payment/_transactions.twig +++ b/src/templates/_payment/_transactions.twig @@ -25,7 +25,7 @@ - + diff --git a/src/templates/_subscribers/_index.twig b/src/templates/_subscribers/_index.twig new file mode 100644 index 0000000..af2f1cc --- /dev/null +++ b/src/templates/_subscribers/_index.twig @@ -0,0 +1,35 @@ + + +
+ +{% js %} + var columns = [ + { + name: '__slot:title', + title: Craft.t('app', 'Email') + }, + { + name: 'id', + title: Craft.t('mollie-payments', 'ID') + }, + { + name: 'id', + title: Craft.t('mollie-payments', 'View on Mollie'), + callback: function(value) { + return 'Link'; + } + } + ]; + + + new Craft.VueAdminTable({ + container: '#mollie-vue-admin-table', + columns: columns, + tableDataEndpoint: 'mollie-payments/subscription/get-subscribers' , + deleteAction: 'mollie-payments/subscription/delete-subscriber', + deleteConfirmationMessage: "{{ 'Are you sure you want to delete subscriber “{name}”?'|t('mollie-payments') }}", + deleteSuccessMessage: "{{ 'Subscriber deleted.'|t('mollie-payments') }}", + emptyMessage: "{{ 'No subscribers exist yet.'|t('mollie-payments') }}", + checkboxes: 1, + }); +{% endjs %}
{{ 'Status'|t('mollie-payments') }}{{ 'chrchStatus'|t('mollie-payments') }} {{ 'ID'|t('mollie-payments') }} {{ 'Amount'|t('mollie-payments') }} {{ 'Date'|t('mollie-payments') }}