-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ShippingMethodService Introduced * ExpressShippingMethod -> ShippingMethod rename * Express Mapper
- Loading branch information
1 parent
eca2948
commit c4066a1
Showing
4 changed files
with
351 additions
and
106 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,173 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rvvup\Payments\Service\Express; | ||
|
||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\Quote\Address; | ||
use Rvvup\Payments\Model\Shipping\ShippingMethod; | ||
use Rvvup\Payments\Service\Shipping\ShippingMethodService; | ||
|
||
class ExpressPaymentRequestMapper | ||
{ | ||
|
||
/** @var ShippingMethodService */ | ||
private $shippingMethodService; | ||
|
||
/** | ||
* @param ShippingMethodService $shippingMethodService | ||
*/ | ||
public function __construct(ShippingMethodService $shippingMethodService) | ||
{ | ||
$this->shippingMethodService = $shippingMethodService; | ||
} | ||
|
||
/** | ||
* @param Quote $quote | ||
* @return array returns object that can be passed into the Rvvup Sdk for Express Payment | ||
*/ | ||
public function map(Quote $quote): array | ||
{ | ||
$total = $quote->getGrandTotal(); | ||
$result = [ | ||
'methodOptions' => [ | ||
'APPLE_PAY' => $this->getApplePayOptions($quote) | ||
], | ||
'total' => [ | ||
'amount' => is_numeric($total) ? number_format((float)$total, 2, '.', '') : $total, | ||
'currency' => $quote->getQuoteCurrencyCode() | ||
], | ||
'billing' => $this->mapAddress($quote->getBillingAddress()), | ||
'shipping' => $this->mapShippingAddress($quote), | ||
]; | ||
$result['shippingMethods'] = $this->getShippingMethods($quote, $result['shipping'] !== null); | ||
|
||
// If methods are empty, need to choose a new address in the express sheet | ||
if (empty($result['shippingMethods'])) { | ||
$result['shipping'] = null; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param Quote $quote | ||
* @param bool $hasShippingAddress | ||
* @return array|null | ||
*/ | ||
private function getShippingMethods(Quote $quote, bool $hasShippingAddress): ?array | ||
{ | ||
if ($quote->isVirtual()) { | ||
return null; | ||
} | ||
// If address is not present then shipping methods will appear after the address update | ||
if (!$hasShippingAddress) { | ||
return null; | ||
} | ||
$availableMethods = $this->shippingMethodService->getAvailableShippingMethods($quote); | ||
$shippingMethods = $this->mapShippingMethods($availableMethods); | ||
if (empty($shippingMethods)) { | ||
return null; | ||
} | ||
|
||
$selectedMethod = $quote->getShippingAddress()->getShippingMethod(); | ||
if (empty($selectedMethod)) { | ||
$shippingMethods[0]['selected'] = true; | ||
} else { | ||
$numShippingMethods = count($shippingMethods); | ||
for ($i = 0; $i < $numShippingMethods; $i++) { | ||
if ($shippingMethods[$i]['id'] === $selectedMethod) { | ||
$shippingMethods[$i]['selected'] = true; | ||
break; | ||
} | ||
} | ||
} | ||
return $shippingMethods; | ||
} | ||
|
||
/** | ||
* @param ShippingMethod[] $shippingMethods | ||
* @return array | ||
*/ | ||
public function mapShippingMethods(array $shippingMethods): array | ||
{ | ||
return array_reduce($shippingMethods, function ($carry, $method) { | ||
$carry[] = [ | ||
'id' => $method->getId(), | ||
'label' => $method->getLabel(), | ||
'amount' => ['amount' => $method->getAmount(), 'currency' => $method->getCurrency()], | ||
]; | ||
return $carry; | ||
}, []); | ||
} | ||
|
||
/** | ||
* @param Quote $quote | ||
* @return array|array[] | ||
*/ | ||
private function getApplePayOptions(Quote $quote): array | ||
{ | ||
$options = [ | ||
'paymentRequest' => [ | ||
'requiredBillingContactFields' => ['postalAddress', 'name', 'email', 'phone'], | ||
// Apple quirk - We need these "shipping" fields to fill the billing email and phone | ||
'requiredShippingContactFields' => ['email', 'phone'] | ||
], | ||
]; | ||
if (!$quote->isVirtual()) { | ||
$options['paymentRequest']['requiredShippingContactFields'] = ['postalAddress', 'name', 'email', 'phone']; | ||
$options['paymentRequest']['shippingType'] = 'shipping'; | ||
$options['paymentRequest']['shippingContactEditingMode'] = 'available'; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* @param Address $quoteAddress | ||
* @return array[] | ||
*/ | ||
private function mapAddress(Quote\Address $quoteAddress): ?array | ||
{ | ||
// We ignore country code because it's always pre-selected by magento. | ||
// We also ignore region, city, postcode because apple partially sets this, if you cancel the sheet after a | ||
// address change. We only pre-fill the apple sheet when the user has actively entered the other fields. | ||
if ((!empty($quoteAddress->getStreet()) && !empty($quoteAddress->getStreet()[0])) || | ||
!empty($quoteAddress->getFirstname()) || | ||
!empty($quoteAddress->getLastname()) || | ||
!empty($quoteAddress->getEmail()) || | ||
!empty($quoteAddress->getTelephone()) | ||
) { | ||
return [ | ||
'address' => [ | ||
'addressLines' => $quoteAddress->getStreet(), | ||
'city' => $quoteAddress->getCity(), | ||
'countryCode' => $quoteAddress->getCountryId(), | ||
'postcode' => $quoteAddress->getPostcode(), | ||
'state' => $quoteAddress->getRegion() | ||
], | ||
'contact' => [ | ||
'givenName' => $quoteAddress->getFirstname(), | ||
'surname' => $quoteAddress->getLastname(), | ||
'email' => $quoteAddress->getEmail(), | ||
'phoneNumber' => $quoteAddress->getTelephone() | ||
] | ||
]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param Quote $quote | ||
* @return void | ||
*/ | ||
private function mapShippingAddress(Quote $quote): ?array | ||
{ | ||
if ($quote->isVirtual()) { | ||
return null; | ||
} | ||
$quoteShippingAddress = $quote->getShippingAddress(); | ||
return $this->mapAddress($quoteShippingAddress); | ||
} | ||
} |
Oops, something went wrong.