-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dda33d2
commit 0eff40b
Showing
21 changed files
with
1,999 additions
and
1,991 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* (c) shopware AG <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace SwagPaymentPayPalUnified\Components\TransactionReport; | ||
|
||
class ReportResult | ||
{ | ||
const PRECISION = 2; | ||
|
||
/** | ||
* @var array<string, array<int, array{'id': int, 'invoice_amount': float}>> | ||
*/ | ||
private $ordersToReport; | ||
|
||
/** | ||
* @var array<string, array<int, int>> | ||
*/ | ||
private $orderIds = []; | ||
|
||
/** | ||
* @var array<string, float> | ||
*/ | ||
private $turnover = []; | ||
|
||
/** | ||
* @var array<int, string> | ||
*/ | ||
private $currencies = []; | ||
|
||
/** | ||
* @param array<string, array<int, array{'id': int, 'invoice_amount': float}>> $ordersToReport | ||
*/ | ||
public function __construct(array $ordersToReport) | ||
{ | ||
$this->ordersToReport = $ordersToReport; | ||
$this->init(); | ||
} | ||
|
||
/** | ||
* @return array<int, string> | ||
*/ | ||
public function getCurrencies() | ||
{ | ||
return $this->currencies; | ||
} | ||
|
||
/** | ||
* @param string $currency | ||
* | ||
* @return float | ||
*/ | ||
public function getTurnover($currency) | ||
{ | ||
if (!\array_key_exists($currency, $this->turnover)) { | ||
return 0.0; | ||
} | ||
|
||
return round($this->turnover[$currency], self::PRECISION, \PHP_ROUND_HALF_UP); | ||
} | ||
|
||
/** | ||
* @param string $currency | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getOrderIds($currency) | ||
{ | ||
if (!\array_key_exists($currency, $this->orderIds)) { | ||
return []; | ||
} | ||
|
||
return $this->orderIds[$currency]; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
private function init() | ||
{ | ||
foreach ($this->ordersToReport as $currency => $orders) { | ||
$this->currencies[] = $currency; | ||
$this->orderIds[$currency] = []; | ||
$this->turnover[$currency] = 0.0; | ||
foreach ($orders as $order) { | ||
$this->turnover[$currency] += (float) $order['invoice_amount']; | ||
$this->orderIds[$currency][] = $order['id']; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/** | ||
* (c) shopware AG <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace SwagPaymentPayPalUnified\Components\TransactionReport; | ||
|
||
use DateTime; | ||
use Doctrine\DBAL\Connection; | ||
use GuzzleHttp\Client; | ||
use PDO; | ||
use Shopware\Models\Order\Status; | ||
|
||
final class TransactionReport | ||
{ | ||
const TRANSACTION_REPORT_TABLE = 'swag_payment_paypal_unified_transaction_report'; | ||
const API_IDENTIFIER = '9b6f559b-5ca1-4969-b23d-e0aa2c01d562'; | ||
const POST_URL = 'https://api.shopware.com'; | ||
const POST_URL_ENDPOINT = '/shopwarepartners/reports/technology'; | ||
|
||
/** | ||
* @var Connection | ||
*/ | ||
private $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @param int $orderId | ||
* | ||
* @return void | ||
*/ | ||
public function reportOrder($orderId) | ||
{ | ||
$this->connection->createQueryBuilder() | ||
->insert(self::TRANSACTION_REPORT_TABLE) | ||
->setValue('order_id', ':orderId') | ||
->setParameter('orderId', $orderId) | ||
->execute(); | ||
} | ||
|
||
/** | ||
* @param string $shopwareVersion | ||
* | ||
* @return void | ||
*/ | ||
public function report($shopwareVersion, Client $client) | ||
{ | ||
$reportResult = $this->getReportResult($this->getReportedOrderIds()); | ||
$currencies = $reportResult->getCurrencies(); | ||
|
||
foreach ($currencies as $currency) { | ||
$requestBody = [ | ||
'identifier' => self::API_IDENTIFIER, | ||
'reportDate' => (new DateTime())->format('Y-m-d\\TH:i:sP'), | ||
'shopwareVersion' => $shopwareVersion, | ||
'currency' => $currency, | ||
'reportDataKeys' => ['turnover' => $reportResult->getTurnover($currency)], | ||
]; | ||
|
||
$client->post(self::POST_URL_ENDPOINT, ['json' => $requestBody]); | ||
|
||
$this->deleteReportedOrders($reportResult->getOrderIds($currency)); | ||
} | ||
} | ||
|
||
/** | ||
* @return array<int, int> | ||
*/ | ||
private function getReportedOrderIds() | ||
{ | ||
return $this->connection->createQueryBuilder() | ||
->select(['order_id']) | ||
->from(self::TRANSACTION_REPORT_TABLE) | ||
->execute() | ||
->fetchAll(PDO::FETCH_COLUMN); | ||
} | ||
|
||
/** | ||
* @param array<int, int> $reportedOrderIds | ||
* | ||
* @return void | ||
*/ | ||
private function deleteReportedOrders(array $reportedOrderIds) | ||
{ | ||
$this->connection->createQueryBuilder() | ||
->delete(self::TRANSACTION_REPORT_TABLE) | ||
->where('order_id IN (:reportedOrderIds)') | ||
->setParameter('reportedOrderIds', $reportedOrderIds, Connection::PARAM_INT_ARRAY) | ||
->execute(); | ||
} | ||
|
||
/** | ||
* @param array<int, int> $reportedIds | ||
* | ||
* @return ReportResult | ||
*/ | ||
private function getReportResult(array $reportedIds) | ||
{ | ||
$queryBuilder = $this->connection->createQueryBuilder(); | ||
|
||
$result = $queryBuilder->select(['currency', 'id', 'invoice_amount']) | ||
->from('s_order') | ||
->where('id IN (:ordersToReport)') | ||
->andWhere('cleared = :paymentStatus') | ||
->andWhere('paymentID IN (:paymentIds)') | ||
->setParameter('paymentStatus', Status::PAYMENT_STATE_COMPLETELY_PAID) | ||
->setParameter('ordersToReport', $reportedIds, Connection::PARAM_INT_ARRAY) | ||
->setParameter('paymentIds', $this->getPaymentIds(), Connection::PARAM_INT_ARRAY) | ||
->execute() | ||
->fetchAll(PDO::FETCH_GROUP); | ||
|
||
return new ReportResult($result); | ||
} | ||
|
||
/** | ||
* @return array<int, int> | ||
*/ | ||
private function getPaymentIds() | ||
{ | ||
return $this->connection->createQueryBuilder() | ||
->select(['id']) | ||
->from('s_core_paymentmeans') | ||
->where('name LIKE :paymentName') | ||
->setParameter('paymentName', 'SwagPaymentPayPalUnified%') | ||
->execute() | ||
->fetchAll(PDO::FETCH_COLUMN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<cronjobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware5/shopware/5.3/engine/Shopware/Components/Plugin/schema/cronjob.xsd"> | ||
|
||
<cronjob> | ||
<name>Execute PayPal transaktion report</name> | ||
<action>SwagPaymentPayPalUnifiedTransaktionReport</action> | ||
<active>true</active> | ||
<interval>86400</interval> | ||
<disableOnError>false</disableOnError> | ||
</cronjob> | ||
</cronjobs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.