Skip to content

Commit

Permalink
Standardize plugin for Shopware 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason authored and Mason committed Mar 26, 2021
2 parents ae941e8 + e8d68c7 commit 134fc35
Show file tree
Hide file tree
Showing 43 changed files with 1,434 additions and 789 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/engine/Library/paymentwall"]
path = src/engine/Library/paymentwall
url = git@github.com:paymentwall/paymentwall-php.git
[submodule "src/Paymentwall/Components/Libs/Paymentwall"]
path = src/Paymentwall/Components/Libs/Paymentwall
url = https://github.com/paymentwall/paymentwall-php.git
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Paymentwall module for Shopware.


###Versions
* Tested on Shopware 5.3.5
* PHP 5.3 or later
* Tested on Shopware 5.3.5, 5.6.9
* PHP 7.2 or later

#Installation
To install Paymentwall Shopware module, please follow the [instructions](https://docs.paymentwall.com/modules/shopware).
Expand Down
1 change: 1 addition & 0 deletions src/Paymentwall/Components/Libs/Paymentwall
Submodule Paymentwall added at 040cbd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Paymentwall\Components\Services;

use Paymentwall_GenerericApiObject;
use Shopware\Models\Order\Order;

class DeliveryConfirmationService
{
const PRODUCT_PHYSICAL = 'physical';
const PRODUCT_DIGITAL = 'digital';
const STATUS_DELIVERED = 'delivered';
const STATUS_ORDER_PLACED = 'order_placed';
const STATUS_ORDER_SHIPPED = 'order_shipped';

/**
* @var Order $order
* @param $customer
* @param $status
* @param null $trackingData
* @return array
*/
public function prepareDeliveryData($order, $status, $trackingData = null)
{
$shipping = $order->getShipping();
$data = [
'payment_id' => $order->getTransactionId(),
'merchant_reference_id' => $order->getId(),
'type' => ($order->getEsd()) ? self::PRODUCT_DIGITAL : self::PRODUCT_PHYSICAL,
'status' => $status,
'estimated_delivery_datetime' => date('Y/m/d H:i:s'),
'estimated_update_datetime' => date('Y/m/d H:i:s'),
'refundable' => 'yes',
'details' => 'Order status has been updated on ' . date('Y/m/d H:i:s'),
'product_description' => '',
'shipping_address[country]' => $shipping->getCountry()->getIso(),
'shipping_address[city]' => $shipping->getCountry(),
'shipping_address[zip]' => $shipping->getZipCode(),
'shipping_address[state]' => !empty($shipping->getState()) ? $shipping->getState() : 'N/A',
'shipping_address[street]' => $shipping->getStreet(),
'shipping_address[phone]' => !empty($shipping->getPhone()) ? $shipping->getPhone() : 'N/A',
'shipping_address[firstname]' => $shipping->getFirstName(),
'shipping_address[lastname]' => $shipping->getLastName(),
'shipping_address[email]' => $order->getCustomer()->getEmail(),
'reason' => 'none',
'attachments' => null,
'is_test' => PaymentwallSettings::getTestMode(),
];

if (!empty($trackingData)) {
return array_merge($data, $trackingData);
}
return $data;
}

public function sendDeliveryData($dataPrepared)
{
if (empty($dataPrepared)) {
return;
}

$delivery = new Paymentwall_GenerericApiObject('delivery');
$delivery->post($dataPrepared);
}
}
103 changes: 103 additions & 0 deletions src/Paymentwall/Components/Services/OrderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Paymentwall\Components\Services;

use Doctrine\DBAL\Connection;
use Shopware\Models\Order\Order;
use Shopware\Models\Order\Status;

class OrderService
{
protected $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

public function setOrderStatus($orderId, $orderStatusId)
{
return $this->connection->createQueryBuilder()
->update('s_order')
->set('status', ':orderStatusId')
->where('id = :orderId')
->setParameters([
':orderId' => $orderId,
':orderStatusId' => $orderStatusId,
])
->execute();
}

public function getOrderStatus($orderId)
{
return $this->connection->createQueryBuilder()
->select('status')
->from('s_order')
->where('id = :orderId')
->setParameters([
':orderId' => $orderId
])
->execute()
->fetchColumn();
}

public function isOrderHistoryEmpty($orderId)
{
return !$this->connection->createQueryBuilder()
->select('id')
->from('s_order_history')
->where('orderId = :orderId')
->setParameters([
':orderId' => $orderId,
])
->execute()
->fetchColumn();
}

public function updateTransactionId($transactionId, $orderId)
{
return $this->connection->createQueryBuilder()
->update('s_order')
->set('transactionID', ':transactionId')
->where('id = :orderId')
->setParameters([
':orderId' => $orderId,
':transactionId' => $transactionId,
])
->execute();
}

public function getPaymentIdByOrderId($orderId)
{
return $this->connection->createQueryBuilder()
->select('paymentID')
->from('s_order')
->where('id = :orderId')
->setParameters([
':orderId' => $orderId,
])
->execute()
->fetchColumn();
}

public function loadOrderRepositoryById($orderId)
{
$repository = Shopware()->Models()->getRepository(Order::class);
return $repository->find($orderId);
}

public function checkOrderWasPaid($orderId)
{
return $this->connection->createQueryBuilder()
->select('id')
->from('s_order_history')
->where('orderId = :orderId')
->andWhere('payment_status_id = :payment_status_id')
->setParameters([
':orderId' => $orderId,
':payment_status_id' => Status::PAYMENT_STATE_COMPLETELY_PAID
])
->execute()
->fetchColumn();
}
}
51 changes: 51 additions & 0 deletions src/Paymentwall/Components/Services/PaymentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Paymentwall\Components\Services;

use Paymentwall_Product;
use Paymentwall_Widget;
use Shopware\Models\Order\Order;

class PaymentService
{
public function prepareWidget($user, $totalAmount, $currencyCode, $orderId)
{
$session = Shopware()->Container()->get('session');
$selectedPaymentMethod = $session->offsetGet('paymentwall-localpayment');

return new Paymentwall_Widget(
!empty($user['userID']) ? $user['userID'] : $user['email'],
PaymentwallSettings::getWidgetCode(),
[
new Paymentwall_Product(
$orderId,
$totalAmount,
$currencyCode,
Paymentwall_Product::TYPE_FIXED
)
],
[
'integration_module' => 'shopware',
'ps' => !empty($selectedPaymentMethod['id']) ? $selectedPaymentMethod['id'] : 'all',
'test_mode' => PaymentwallSettings::getTestMode(),
'success_url' => $this->getSuccessUrl()
]
);
}

protected function getSuccessUrl()
{
$router = Shopware()->Front()->Router();
return $router->assemble(['controller' => 'Paymentwall', 'action' => 'success']);
}

public function getOrderIdByPaymentUniqueId($paymentUniqueId)
{
$repository = Shopware()->Models()->getRepository(Order::class);
$order = $repository->findOneBy([
'temporaryId' => $paymentUniqueId,
'transactionId' => $paymentUniqueId
]);
return $order->getId();
}
}
67 changes: 67 additions & 0 deletions src/Paymentwall/Components/Services/PaymentSystemService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Paymentwall\Components\Services;

use Paymentwall_Config;
use Paymentwall_Signature_Widget;

class PaymentSystemService
{
public function getLocalPaymentMethods()
{
$localPaymentMethods = [];

if (empty($localPaymentMethods)) {
$userCountry = UtilService::getCountryByIp(UtilService::getRealClientIP());
$response = $this->getPaymentMethodFromApi($userCountry);
$localPaymentMethods = $this->prepareLocalPayment($response);
}
return $localPaymentMethods;
}

protected function getPaymentMethodFromApi($userCountry)
{
if (empty($userCountry)) {
return null;
}

$params = array(
'key' => PaymentwallSettings::getProjectKey(),
'country_code' => $userCountry,
'sign_version' => 3,
'currencyCode' => Shopware()->Container()->get('currency')->getShortName(),
);

$params['sign'] = (new Paymentwall_Signature_Widget())->calculate(
$params,
$params['sign_version']
);

$url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_error($curl)) {
return null;
}

return json_decode($response, true);
}

protected function prepareLocalPayment($payments)
{
$methods = [];
if (!empty($payments)) {
foreach ($payments as $payment) {
if (!empty($payment['id']) && !empty($payment['name'])) {
$methods[] = [
'id' => $payment['id'],
'name' => $payment['name'],
'img_url' => !empty($payment['img_url']) ? $payment['img_url'] : ''
];
}
}
}
return $methods;
}
}
47 changes: 47 additions & 0 deletions src/Paymentwall/Components/Services/PaymentwallSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Paymentwall\Components\Services;

class PaymentwallSettings
{
protected static function getConfig()
{
return Shopware()->Container()->get('shopware.plugin.cached_config_reader')->getByPluginName('paymentwall');
}

public static function getTestMode()
{
$config = self::getConfig();
return $config['pwTestMode'];
}

public static function getWidgetCode()
{
$config = self::getConfig();
return $config['pwWidget'];
}

public static function getProjectKey()
{
$config = self::getConfig();
return $config['pwProjectKey'];
}

public static function getSecretKey()
{
$config = self::getConfig();
return $config['pwWipwSecretKey'];
}

public static function getRefundState()
{
$config = self::getConfig();
return $config['pwRefundState'];
}

public static function getRedirectPayment()
{
$config = self::getConfig();
return $config['pwRedirectPayment'];
}
}
Loading

0 comments on commit 134fc35

Please sign in to comment.