Skip to content

Commit

Permalink
Merge branch 'develop-v5' into v5
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	composer.json
  • Loading branch information
janhenckens committed Jul 29, 2024
2 parents 266581b + d018f67 commit 2546f75
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 12 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/MolliePayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function(RegisterUrlRulesEvent $event) {
$event->rules['mollie-payments'] = ['template' => 'mollie-payments/_payment/_index.twig'];
$event->rules['mollie-payments/payments/<uid:{uid}>'] = '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/<uid:{uid}>'] = 'mollie-payments/subscription/edit';
$event->rules['mollie-payments/forms'] = 'mollie-payments/forms/index';
$event->rules['mollie-payments/forms/add'] = 'mollie-payments/forms/edit';
Expand Down Expand Up @@ -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'),
Expand Down
68 changes: 65 additions & 3 deletions src/controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
}
}
10 changes: 10 additions & 0 deletions src/services/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 13 additions & 5 deletions src/services/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/templates/_payment/_transactions.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<table class="data fullwidth collapsible">
<thead>
<tr>
<th scope="col">{{ 'Status'|t('mollie-payments') }}</th>
<th scope="col">{{ 'chrchStatus'|t('mollie-payments') }}</th>
<th scope="col">{{ 'ID'|t('mollie-payments') }}</th>
<th scope="col">{{ 'Amount'|t('mollie-payments') }}</th>
<th scope="col">{{ 'Date'|t('mollie-payments') }}</th>
Expand Down
35 changes: 35 additions & 0 deletions src/templates/_subscribers/_index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


<div id="mollie-vue-admin-table"></div>

{% 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 '<a href="https://my.mollie.com/dashboard/customers/' + value +'" class="" target="_blank">Link</a>';
}
}
];


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 %}

0 comments on commit 2546f75

Please sign in to comment.