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

PA-1065: identificador de bandeira #508

Merged
merged 17 commits into from
Oct 28, 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
1 change: 1 addition & 0 deletions src/Concrete/WoocommercePlatformOrderDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ private function extractBasePaymentData()
$newPaymentData->identifier = $identifier;
$newPaymentData->installments = intval($this->formData["installments"]);
$newPaymentData->recurrenceCycle = $this->formData["recurrence_cycle"] ?? null;
$newPaymentData->paymentOrigin = $this->formData["payment_origin"] ?? null;
$newPaymentData->saveOnSuccess = isset($this->formData["save_credit_card"]);
$amount = $this->formData["card_order_value"] ?? $this->getGrandTotal();
$amount = number_format($amount, 2, '.', '');
Expand Down
31 changes: 30 additions & 1 deletion src/Model/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Woocommerce\Pagarme\Model\Payment\Data\Card;
use Woocommerce\Pagarme\Model\Payment\Data\Multicustomers;
use Woocommerce\Pagarme\Model\Payment\Data\PaymentRequestInterface;
use Woocommerce\Pagarme\Service\CardService;

class Checkout
{
Expand Down Expand Up @@ -125,6 +126,7 @@ 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();
$this->formatFieldsWhenIsSubscription($fields, $wc_order);
$attempts = intval($wc_order->get_meta('_pagarme_attempts') ?? 0) + 1;
$wc_order->update_meta_data("_pagarme_attempts", $attempts);
$response = $this->orders->create_order(
Expand All @@ -144,8 +146,8 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
$order->update_meta("attempts", $attempts);
$this->addAuthenticationOnMetaData($order, $fields);
if ($response) {
do_action("on_pagarme_response", $response);
WC()->cart->empty_cart();
do_action("on_pagarme_response", $wc_order->get_id(), $response);
$order->update_meta('transaction_id', $response->getPagarmeId()->getValue());
$order->update_meta('pagarme_id', $response->getPagarmeId()->getValue());
$order->update_meta('pagarme_status', $response->getStatus()->getStatus());
Expand All @@ -160,6 +162,30 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
return false;
}
}
private function formatFieldsWhenIsSubscription(&$fields, $wc_order)
{
if(Subscription::hasSubscriptionProductInCart() == false){
return;
}
if ($fields['payment_method'] === 'credit_card') {
$fields['card_id'] = $this->getCardId($fields, $wc_order);
// If same, return payment_origin in $fields
$subscription = new Subscription();
$subscription->isSameCardInSubscription($fields, $wc_order);
unset($fields['pagarmetoken1']);
}
}

private function getCardId($fields, $wc_order)
{
if($fields['card_id']){
return $fields['card_id'];
}
$customer = new Customer($wc_order->get_customer_id());
$cardService = new CardService();
$pagarmeCard = $cardService->create($fields['pagarmetoken1'], $customer->getPagarmeCustomerId());
return $pagarmeCard['cardId'];
}

private function convertCheckoutObject(PaymentRequestInterface $paymentRequest)
{
Expand Down Expand Up @@ -214,6 +240,9 @@ private function convertCheckoutObject(PaymentRequestInterface $paymentRequest)

private function extractGooglePayToken(&$fields, $paymentRequest)
{
if(!$paymentRequest->getDataByKey('googlepay')){
return;
}
$fields['googlepay']['token'] = $paymentRequest->getDataByKey('googlepay');
}

Expand Down
30 changes: 26 additions & 4 deletions src/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
exit(0);
}

use Woocommerce\Pagarme\Helper\Utils;
use Pagarme\Core\Payment\Repositories\SavedCardRepository as CoreSavedCardRepository;
use Pagarme\Core\Payment\Repositories\CustomerRepository as CoreCustomerRepository;
use Woocommerce\Pagarme\Service\CustomerService;
Expand All @@ -33,11 +32,11 @@ class Customer
* @param CoreCustomerRepository $customerRepository
*/
/** phpcs:disable */
public function __construct($ID, $cardRepository, $customerRepository)
public function __construct($ID, $cardRepository = null, $customerRepository = null)
{
$this->ID = (int) $ID;
$this->cardRepository = $cardRepository;
$this->customerRepository = $customerRepository;
$this->cardRepository = $cardRepository ?? new CoreSavedCardRepository();
$this->customerRepository = $customerRepository ?? new CoreCustomerRepository();
}

public function __get($prop_name)
Expand Down Expand Up @@ -125,6 +124,11 @@ public function getPagarmeCustomerId()
return $customer->getPagarmeId()->getValue();
}

/**
* @param string $code
* @param string $pagarmeId
* @return void
*/
public function savePagarmeCustomerId($code, $pagarmeId)
{
$customerService = new CustomerService();
Expand All @@ -133,4 +137,22 @@ public function savePagarmeCustomerId($code, $pagarmeId)
$customer->setPagarmeId($pagarmeId);
$customerService->saveOnPlatform($customer);
}

/**
* @param \WC_Order $wcOrder
* @param boolean|null $createIfNotExists create customer on Pagar.me if not exists
* @return string|\Exception
*/
public function getPagarmeCustomerIdByOrder($wcOrder, $createIfNotExists = true)
{
$customer = $this->customerRepository->findByCode($wcOrder->get_customer_id());
if ($customer) {
return $customer->getPagarmeId()->getValue();
}
if (!$createIfNotExists) {
throw new \Exception('Customer not found');
}
$customer = new CustomerService();
return $customer->createCustomerByOrder($wcOrder);
}
}
Loading
Loading