Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manual order and emails #443

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ protected function saveAdminOptionsInCoreConfig($values)
public function pagarme_email_payment_info($order, $sent_to_admin)
{
if ($sent_to_admin
|| $this->id !== $order->get_meta('payment_method')
|| $this->id !== $order->get_payment_method()
|| !in_array($order->get_status(), $this->sendEmailStatus)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function create_order(WC_Order $wc_order, $payment_method, $form_fields)
$paymentMethodDecorator->setPaymentMethod($orderDecorator);

$orderDecorator->setPaymentMethod($paymentMethodDecorator->getPaymentMethod());

$orderDecorator->setAttempts($wc_order->get_meta('_pagarme_attempts'));
$orderService = new OrderService();
$response = $orderService->createOrderAtPagarme($orderDecorator);

Expand Down
6 changes: 4 additions & 2 deletions src/Helper/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ public static function is_request_ajax()
*/
public static function isCheckoutRequest()
{
return strpos(strtolower(self::server('REQUEST_URI')), 'checkout') !== false;
if(function_exists('is_checkout')) {
return is_checkout();
}
return false;
}

/**
* Get value by array index
*
Expand Down
4 changes: 4 additions & 0 deletions src/Model/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
if ($type === CheckoutTypes::TRANSPARENT_VALUE) {
$fields = $this->convertCheckoutObject($_POST[PaymentRequestInterface::PAGARME_PAYMENT_REQUEST_KEY]);
$fields['recurrence_cycle'] = Subscription::getRecurrenceCycle();
$attempts = intval($wc_order->get_meta('_pagarme_attempts') ?? 0) + 1;
$wc_order->update_meta_data("_pagarme_attempts", $attempts);
$response = $this->orders->create_order(
$wc_order,
$fields['payment_method'],
Expand All @@ -137,6 +139,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
);
$order->getWcOrder()->set_total($this->getTotalValue($wc_order, $totalWithInstallments));
$order->update_meta('payment_method', $fields['payment_method']);
$order->update_meta("attempts", $attempts);
$this->addAuthenticationOnMetaData($order, $fields);
WC()->cart->empty_cart();
if ($response) {
Expand All @@ -151,6 +154,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
}
$order->update_meta('pagarme_status', 'failed');
$order->update_by_pagarme_status('failed');
$order->getWcOrder()->save();
return false;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ class Order extends Meta
// == END WC ORDER ==

public $with_prefix = array(
'payment_method' => 1,
'response_data' => 1,
'pagarme_status' => 1,
'pagarme_id' => 1,
'payment_method' => 1,
'response_data' => 1,
'pagarme_status' => 1,
'pagarme_id' => 1,
'attempts' => 1
);

/** phpcs:disable */
Expand Down
6 changes: 5 additions & 1 deletion tests/Model/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function testProcessWithTdsAuthenticatedCreditCardPaymentMethodShouldSetA
->andReturn(1);
$wcOrderMock->shouldReceive('set_total')
->andReturnSelf();
$wcOrderMock->shouldReceive('get_meta')
->andReturn("");
$wcOrderMock->shouldReceive('update_meta_data')
->andReturnSelf();

$ordersMock->shouldReceive('create_order')
->withArgs(function ($wcOrder, $paymentMethod, $fields) use ($wcOrderMock) {
Expand Down Expand Up @@ -144,7 +148,7 @@ public function testProcessWithTdsAuthenticatedCreditCardPaymentMethodShouldSetA
->once();

$checkout = new Checkout($gatewayMock, $configMock, $ordersMock, $wooOrderRepositoryMock);

$this->assertTrue($checkout->process($wcOrderMock));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ abstract class AbstractPlatformOrderDecorator implements PlatformOrderInterface
protected $platformOrder;
private $logService;
private $paymentMethod;
private $attempts = 1;

public function __construct()
{
$this->logService = new OrderLogService();
}

public function getAttempts()
{
return $this->attempts;
}

public function setAttempts($attempts)
{
$this->attempts = $attempts;
}
public function addHistoryComment($message, $notifyCustomer = false)
{
$message = 'PGM - ' . $message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function createOrderAtPagarme(PlatformOrderInterface $platformOrder)
);

$i18n = new LocalizationService();

$paymentOrder->setAttempts($platformOrder->getAttempts());
//Send through the APIService to pagarme
$apiService = new APIService();
$response = $apiService->createOrder($paymentOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ final class Order extends AbstractEntity implements ConvertibleToSDKRequestsInte
private $payments;
/** @var boolean */
private $closed;

/** @var int */
private $attempts = 1;
/** @var array */
private $splitData;

/** @var boolean */
Expand All @@ -46,6 +48,17 @@ public function __construct()
$this->closed = true;
}


public function getAttempts()
{
return $this->attempts;
}

public function setAttempts($attempts)
{
$this->attempts = $attempts;
}

/**
* @return string
*/
Expand Down Expand Up @@ -138,7 +151,7 @@ public function addPayment(AbstractPayment $payment)
*/
public function generateIdempotencyKey()
{
return sha1($this->getCustomer()->getDocument() . $this->getCode());
return sha1($this->getCustomer()->getDocument() . $this->getCode() . '-attempt-' . $this->getAttempts());
}

/**
Expand All @@ -154,8 +167,7 @@ public function isPaymentSumCorrect()
}

$sum = 0;
foreach ($this->payments as $payment)
{
foreach ($this->payments as $payment) {
$sum += $payment->getAmount();
}

Expand Down Expand Up @@ -207,7 +219,7 @@ private function validatePaymentInvariants(AbstractPayment $payment)
private function discoverPaymentMethod(AbstractPayment $payment)
{
$paymentClass = get_class($payment);
$paymentClass = explode ('\\', $paymentClass ?? '');
$paymentClass = explode('\\', $paymentClass ?? '');
$paymentClass = end($paymentClass);
return $paymentClass;
}
Expand All @@ -217,7 +229,7 @@ private function validateSavedCreditCardPayment(SavedCreditCardPayment $payment)
if ($this->customer === null) {
throw new \Exception(
'To use a saved credit card payment in an order ' .
'you must add a customer to it.',
'you must add a customer to it.',
400
);
}
Expand Down Expand Up @@ -335,7 +347,7 @@ public function setSplitData($splitData)
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
*/
#[\ReturnTypeWillChange]

public function jsonSerialize()
Expand Down Expand Up @@ -375,7 +387,7 @@ public function convertToSDKRequest()
$orderRequest->payments[] = $payment->convertToSDKRequest();
}

if (!empty($this->getSplitData())){
if (!empty($this->getSplitData())) {
$orderRequest = $this->fixRoundedValuesInCharges($orderRequest);
}

Expand All @@ -392,34 +404,35 @@ public function convertToSDKRequest()
return $orderRequest;
}

private function fixRoundedValuesInCharges(&$orderRequest){
private function fixRoundedValuesInCharges(&$orderRequest)
{

if(count($orderRequest->payments) < 2){
if (count($orderRequest->payments) < 2) {
return $orderRequest;
}

$firstChargeAmount = $orderRequest->payments[0]->amount;
$firstChargePercentageOfTotal = $firstChargeAmount / $this->getAmount();

if ($firstChargePercentageOfTotal !== 0.5){
if ($firstChargePercentageOfTotal !== 0.5) {
return $orderRequest;
}

$orderSplitData = $this->getSplitData();

$wrongValuesPerRecipient = $this->getRecipientWrongValuesMap($orderRequest, $orderSplitData);

if (!$wrongValuesPerRecipient){
if (!$wrongValuesPerRecipient) {
return $orderRequest;
}

$orderRequest = $this->fixRoundedValues($wrongValuesPerRecipient, $orderRequest);

return $orderRequest;

}

private function getRecipientWrongValuesMap($orderRequest, $splitData){
private function getRecipientWrongValuesMap($orderRequest, $splitData)
{
$map = [];

$marketplaceId = $splitData->getMainRecipientOptionConfig();
Expand All @@ -445,15 +458,16 @@ private function getRecipientWrongValuesMap($orderRequest, $splitData){
}

foreach ($map as $recipientId => $wrongValue) {
if ($wrongValue !== 0){
if ($wrongValue !== 0) {
return $map;
}
}

return false;
}

private function fixRoundedValues($wrongValuesMap, &$orderRequest){
private function fixRoundedValues($wrongValuesMap, &$orderRequest)
{

foreach ($wrongValuesMap as $recipientId => $wrongValue) {
$payments = $orderRequest->payments;
Expand All @@ -466,12 +480,12 @@ private function fixRoundedValues($wrongValuesMap, &$orderRequest){
foreach ($paymentRequest->split as $key => &$splitRequest) {
$splitedAmount += $splitRequest->amount;

if($splitRequest->recipientId === $recipientId){
if ($splitRequest->recipientId === $recipientId) {
$recipientSplitData = $splitRequest;
}
}

if ($splitedAmount === $paymentRequestAmount){
if ($splitedAmount === $paymentRequestAmount) {
continue;
}

Expand All @@ -481,7 +495,7 @@ private function fixRoundedValues($wrongValuesMap, &$orderRequest){

$mustRemoveFromOtherCharges = $wrongValue + $amountRemovableFromCharge;

if (!$mustRemoveFromOtherCharges){
if (!$mustRemoveFromOtherCharges) {
break;
}
}
Expand Down
Loading