Skip to content

Commit

Permalink
Merge pull request #71 from checkout/bugfix/incorrct_refund_env
Browse files Browse the repository at this point in the history
Fixed wrong env being picked for refunds
  • Loading branch information
anand-balasubramaniam-cko authored Nov 4, 2022
2 parents 64b68c9 + 1dfe6b6 commit 8a3c065
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
38 changes: 19 additions & 19 deletions checkoutcom.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Checkout\CheckoutApi;
use Checkout\Models\Payments\Capture;
use Checkout\Models\Payments\Refund;
use CheckoutCom\PrestaShop\Models\Payments\Method;

class CheckoutCom extends PaymentModule
{
Expand All @@ -47,7 +48,7 @@ public function __construct()
{
$this->name = 'checkoutcom';
$this->tab = 'payments_gateways';
$this->version = '2.3.2';
$this->version = '2.3.3';
$this->author = 'Checkout.com';
$this->need_instance = 1;

Expand Down Expand Up @@ -444,6 +445,9 @@ public function hookActionOrderSlipAdd($params)
$order_payment = OrderPayment::getByOrderId($order_id);
$payment_id = $order_payment[0]->transaction_id;
$amount = 0;
$currency = new CurrencyCore($order->id_currency);
$currency_code = $currency->iso_code;

if (! $this->active || ($order->module != $this->name)) {
return;
}
Expand All @@ -456,7 +460,7 @@ public function hookActionOrderSlipAdd($params)
$this->context->controller->errors[] = $this->l('An error has occured. No cko payment id found');
return false;
}
$checkout = new CheckoutApi( \Configuration::get('CHECKOUTCOM_SECRET_KEY') );
// $checkout = new CheckoutApi( \Configuration::get('CHECKOUTCOM_SECRET_KEY') );
if(strlen($payment_id)>0)
{
$returnedQuantity = 0;
Expand Down Expand Up @@ -536,9 +540,11 @@ public function hookActionOrderSlipAdd($params)
}

//Refund the amount using sdk.
$refund = $this->_refund($payment_id, $amount);
$refund = $this->_refund($payment_id, $amount, $currency_code);
if(!$refund){
$this->context->controller->errors[] = $this->l('An error has occured while processing your refund on checkout.com.');
$this->errors[] = $this->l('An error has occured while processing your refund on checkout.com.');

$this->logger->error('Refund failure : true');
// No refund, so get back refunded products quantities, and available products stock quantities.
$this->_rollbackOrder($order);
Expand Down Expand Up @@ -617,22 +623,16 @@ private function _updateOrderState($order){
* @param $amount
* @return bool
*/
private function _refund($payment_id, $amount){
$checkout = new CheckoutApi( \Configuration::get('CHECKOUTCOM_SECRET_KEY') );
try{
$amount = (int) round( $amount * 100,0);
$request = new Refund($payment_id,$amount);
$refund = $checkout->payments()->refund($request);
return $refund;
}
catch (Checkout\Library\Exceptions\CheckoutHttpException $ex) {

$this->logger->error('Refund error : '.$ex->getBody());
$this->context->controller->errors[] = $this->l('An error has occured while processing your refund on checkout.com.');
$this->errors[] = $this->l('An error has occured while processing your refund on checkout.com.');
return false;
}
return false;
private function _refund($payment_id, $amount, $currency_code){

$param = array(
'payment_id' => $payment_id,
'currency_code' => $currency_code,
'amount' => $amount
);

$refund = Method::makeRefund($param);
return $refund;
}


Expand Down
4 changes: 3 additions & 1 deletion src/Models/Payments/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public static function addThreeDs(Payment $payment)
*/
public static function makeRefund(array $params)
{

$cko_payment_id = $params['payment_id'];

// Check if cko_payment_id is empty
Expand All @@ -288,12 +289,13 @@ public static function makeRefund(array $params)
if(isset($params['amount'])){
$ckoPayment->amount = static::fixAmount($params['amount'], $params['currency_code']);
}

$response = CheckoutApiHandler::api()->payments()->refund($ckoPayment);

if (!$response->isSuccessful()) {
//@todo return error message
} else {

return $response;
}
} catch (CheckoutHttpException $ex) {
Expand Down

0 comments on commit 8a3c065

Please sign in to comment.