Skip to content

Commit

Permalink
Added PayPalOrder VO and Comparator service
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Jan 19, 2024
1 parent c25ab66 commit 2022c2c
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 16 deletions.
75 changes: 75 additions & 0 deletions src/PayPal/Order/Comparator/PayPalOrderComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\Comparator;

use PrestaShop\Module\PrestashopCheckout\PayPal\Order\ValueObject\PayPalOrder;

class PayPalOrderComparator
{
/**
* @param PayPalOrder $oldOrder
* @param PayPalOrder $newOrder
*
* @return array
*/
public function getFieldsToUpdate(PayPalOrder $oldOrder, PayPalOrder $newOrder)
{
$fieldsToUpdate = [];
$oldOrderArray = $oldOrder->toArray();
$newOrderArray = $newOrder->toArray();

$fieldsToUpdate = array_filter($newOrderArray, function ($value, $key) use ($oldOrderArray) {
return $this->compareValues($value, $oldOrderArray[$key]);
}, ARRAY_FILTER_USE_BOTH);

// if ($oldOrder->getIntent() !== $newOrder->getIntent()) {
// $fieldsToUpdate['intent'] = $newOrder->getIntent();
// }
//
//
// if ($this->compareArraysDeep($oldOrderArray['payer'], $newOrderArray['payer'])) {
// $fieldsToUpdate['payer'] = $newOrderArray['payer'];
// }
//
// if ($this->compareArraysDeep($oldOrderArray['purchase_units'], $newOrderArray['purchase_units'])) {
// $fieldsToUpdate['purchase_units'] = $newOrderArray['purchase_units'];
// }

return $fieldsToUpdate;
}

private function compareValues($value1, $value2)
{
$result = false;
if (is_array($value1) && is_array($value2)) {
$result |= $this->compareArraysDeep($value1, $value2);
} else {
$result |= $value1 !== $value2;
}
return $result;
}

/**
* @param array $array1
* @param array $array2
* @return false|int|string
*/
private function compareArraysDeep($array1, $array2)
{
$result = false;

if (count($array1) !== count($array2)) {
return true;
}

foreach ($array1 as $key => $oldValue) {
$newValue = $array2[$key];
if (is_array($oldValue)) {
$result |= $this->compareArraysDeep($oldValue, $newValue);
} else {
$result |= $oldValue !== $newValue;
}
}
return $result;
}
}
21 changes: 13 additions & 8 deletions src/PayPal/Order/ValueObject/PayPalOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class PayPalOrder
*/
private $intent;
/**
* @var array|null
* @var PayPalOrderPaymentSource|null
*/
private $paymentSource;
/**
* @var array|null
* @var PayPalOrderPurchaseUnit[]
*/
private $purchaseUnits;
/**
Expand All @@ -39,7 +39,7 @@ class PayPalOrder
*/
private $links;

public function __construct(PayPalOrderId $id = null, $status = null, $intent = null, $paymentSource = [], $purchaseUnits = [], $payer = [], $createTime = null, $links = [] )
public function __construct(PayPalOrderId $id = null, $status = null, $intent = null, PayPalOrderPaymentSource $paymentSource = null, $purchaseUnits = [], $payer = [], $createTime = null, $links = [] )
{
$this->id = $id;
$this->status = $status;
Expand Down Expand Up @@ -100,15 +100,15 @@ public function setIntent($intent)
}

/**
* @return array
* @return PayPalOrderPaymentSource
*/
public function getPaymentSource()
{
return $this->paymentSource;
}

/**
* @param array $paymentSource
* @param PayPalOrderPaymentSource $paymentSource
*/
public function setPaymentSource($paymentSource)
{
Expand All @@ -124,7 +124,7 @@ public function getPurchaseUnits()
}

/**
* @param array $purchaseUnits
* @param PayPalOrderPurchaseUnit[] $purchaseUnits
*/
public function setPurchaseUnits($purchaseUnits)
{
Expand Down Expand Up @@ -179,14 +179,19 @@ public function setLinks($links)
$this->links = $links;
}

/**
* @return array
*/
public function toArray()
{
return [
'id' => $this->id ? $this->id->getValue() : null,
'status' => $this->status,
'intent' => $this->intent,
'payment_source' => $this->paymentSource,
'purchase_units' => $this->purchaseUnits,
'payment_source' => $this->paymentSource === null ? null : $this->paymentSource->toArray(),
'purchase_units' => array_map(function(PayPalOrderPurchaseUnit $purchaseUnit) {
return $purchaseUnit->toArray();
}, $this->purchaseUnits),
'payer' => $this->payer,
'create_time' => $this->createTime,
'links' => $this->links
Expand Down
233 changes: 233 additions & 0 deletions src/PayPal/Order/ValueObject/PayPalOrderPayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\ValueObject;

class PayPalOrderPayer
{
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $address1;
/**
* @var string
*/
private $address2;
/**
* @var string
*/
private $adminArea1;
/**
* @var string
*/
private $adminArea2;
/**
* @var string
*/
private $countryCode;
/**
* @var string
*/
private $postalCode;

/**
* @param string $firstName
* @param string $lastName
* @param string $email
* @param string $address1
* @param string $address2
* @param string $adminArea1
* @param string $adminArea2
* @param string $countryCode
* @param string $postalCode
*/
public function __construct($firstName, $lastName, $email, $address1, $address2, $adminArea1, $adminArea2, $countryCode, $postalCode)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->address1 = $address1;
$this->address2 = $address2;
$this->adminArea1 = $adminArea1;
$this->adminArea2 = $adminArea2;
$this->countryCode = $countryCode;
$this->postalCode = $postalCode;
}

/**
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}

/**
* @param string $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}

/**
* @return string
*/
public function getLastName()
{
return $this->lastName;
}

/**
* @param string $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}

/**
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}

/**
* @return string
*/
public function getAddress1()
{
return $this->address1;
}

/**
* @param string $address1
*/
public function setAddress1($address1)
{
$this->address1 = $address1;
}

/**
* @return string
*/
public function getAddress2()
{
return $this->address2;
}

/**
* @param string $address2
*/
public function setAddress2($address2)
{
$this->address2 = $address2;
}

/**
* @return string
*/
public function getAdminArea1()
{
return $this->adminArea1;
}

/**
* @param string $adminArea1
*/
public function setAdminArea1($adminArea1)
{
$this->adminArea1 = $adminArea1;
}

/**
* @return string
*/
public function getAdminArea2()
{
return $this->adminArea2;
}

/**
* @param string $adminArea2
*/
public function setAdminArea2($adminArea2)
{
$this->adminArea2 = $adminArea2;
}

/**
* @return string
*/
public function getCountryCode()
{
return $this->countryCode;
}

/**
* @param string $countryCode
*/
public function setCountryCode($countryCode)
{
$this->countryCode = $countryCode;
}

/**
* @return string
*/
public function getPostalCode()
{
return $this->postalCode;
}

/**
* @param string $postalCode
*/
public function setPostalCode($postalCode)
{
$this->postalCode = $postalCode;
}

/**
* @return array
*/
public function toArray()
{
return [
'name' => [
'given_name' => $this->firstName,
'surname' => $this->lastName,
],
'email_address' => $this->email,
'address' => [
'address_line_1' => $this->address1,
'address_line_2' => $this->address2,
'admin_area_1' => $this->adminArea1,
'admin_area_2' => $this->adminArea2,
'country_code' => $this->countryCode,
'postal_code' => $this->postalCode,
],
];
}
}
Loading

0 comments on commit 2022c2c

Please sign in to comment.