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

GraphQL addition #240

Merged
merged 7 commits into from
Jun 11, 2021
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
127 changes: 114 additions & 13 deletions Model/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public function authorize(InfoInterface $payment, $amount)

$isWebhookCall = false;

//validate RzpOrderamount with quote/order amount before signature
$orderAmount = (int) (number_format($order->getGrandTotal() * 100, 0, ".", ""));

if((empty($request) === true) and (isset($_POST['razorpay_signature']) === true))
{
//set request data based on redirect flow
Expand All @@ -225,21 +228,119 @@ public function authorize(InfoInterface $payment, $amount)
}
else
{
$payment_id = $request['paymentMethod']['additional_data']['rzp_payment_id'];
//check for GraphQL
if(empty($request['query']) === false)
{

//update orderLink
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$orderLinkCollection = $_objectManager->get('Razorpay\Magento\Model\OrderLink')
->getCollection()
->addFilter('quote_id', $order->getQuoteId())
->getFirstItem();

$orderLink = $orderLinkCollection->getData();

if (empty($orderLink['entity_id']) === false)
{
$payment_id = $orderLink['rzp_payment_id'];

$rzp_order_id = $orderLink['rzp_order_id'];

$rzp_signature = $orderLink['rzp_signature'];

$rzp_order_amount_actual = (int) $orderLink['rzp_order_amount'];

if((empty($payment_id) === true) and
(emprty($rzp_order_id) === true) and
(emprty($rzp_signature) === true))
{
throw new LocalizedException(__("Razorpay Payment details missing."));
}

if ($orderAmount !== $rzp_order_amount_actual)
{
$rzpOrderAmount = $order->getOrderCurrency()->formatTxt(number_format($rzp_order_amount_actual / 100, 2, ".", ""));

throw new LocalizedException(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
}

//validate payment signature first
$this->validateSignature([
'razorpay_payment_id' => $payment_id,
'razorpay_order_id' => $rzp_order_id,
'razorpay_signature' => $rzp_signature
]);

try
{
//fetch the payment from API and validate the amount
$payment_data = $this->rzp->payment->fetch($payment_id);
}
catch(\Razorpay\Api\Errors\Error $e)
{
$this->_logger->critical($e);
throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage()));
}

if($payment_data->order_id === $rzp_order_id)
{
try
{
//fetch order from API
$rzp_order_data = $this->rzp->order->fetch($rzp_order_id);
}
catch(\Razorpay\Api\Errors\Error $e)
{
$this->_logger->critical($e);
throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage()));
}

//verify order receipt
if($rzp_order_data->receipt !== $order->getQuoteId())
{
throw new LocalizedException(__("Not a valid Razorpay Payment"));
}

//verify currency
if($payment_data->currency !== $order->getOrderCurrencyCode())
{
throw new LocalizedException(__("Order Currency:(%1) not matched with payment currency:(%2)", $order->getOrderCurrencyCode(), $payment_data->currency));
}
}
else
{
throw new LocalizedException(__("Not a valid Razorpay Payments."));
}

}
else
{
throw new LocalizedException(__("Razorpay Payment details missing."));
}
}
else
{
// Order processing through front-end

$rzp_order_id = $this->order->getOrderId();
$payment_id = $request['paymentMethod']['additional_data']['rzp_payment_id'];

//validate RzpOrderamount with quote/order amount before signature
$orderAmount = (int) (number_format($order->getGrandTotal() * 100, 0, ".", ""));
$rzp_order_id = $this->order->getOrderId();

if ($orderAmount !== $this->order->getRazorpayOrderAmount())
{
$rzpOrderAmount = $order->getOrderCurrency()->formatTxt(number_format($this->order->getRazorpayOrderAmount() / 100, 2, ".", ""));
if ($orderAmount !== $this->order->getRazorpayOrderAmount())
ramth05 marked this conversation as resolved.
Show resolved Hide resolved
{
$rzpOrderAmount = $order->getOrderCurrency()->formatTxt(number_format($this->order->getRazorpayOrderAmount() / 100, 2, ".", ""));

throw new LocalizedException(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
}
throw new LocalizedException(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
}

$this->validateSignature($request);
$this->validateSignature([
'razorpay_payment_id' => $payment_id,
'razorpay_order_id' => $rzp_order_id,
'razorpay_signature' => $request['paymentMethod']['additional_data']['rzp_signature']
]);
}
}

$payment->setStatus(self::STATUS_APPROVED)
Expand Down Expand Up @@ -332,9 +433,9 @@ protected function updatePaymentNote($paymentId, $order, $rzpOrderId, $isWebhook
protected function validateSignature($request)
{
$attributes = array(
'razorpay_payment_id' => $request['paymentMethod']['additional_data']['rzp_payment_id'],
'razorpay_order_id' => $this->order->getOrderId(),
'razorpay_signature' => $request['paymentMethod']['additional_data']['rzp_signature'],
'razorpay_payment_id' => $request['razorpay_payment_id'],
'razorpay_order_id' => $request['razorpay_order_id'],
'razorpay_signature' => $request['razorpay_signature'],
);

$this->rzp->utility->verifyPaymentSignature($attributes);
Expand Down
137 changes: 137 additions & 0 deletions Model/Resolver/PlaceRazorpayOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
declare (strict_types = 1);

namespace Razorpay\Magento\Model\Resolver;


use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\CartManagementInterface;
use Razorpay\Magento\Model\PaymentMethod;

class PlaceRazorpayOrder implements ResolverInterface
{

protected $scopeConfig;

protected $cartManagement;

protected $_objectManager;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
GetCartForUser $getCartForUser,
\Magento\Quote\Api\CartManagementInterface $cartManagement,
PaymentMethod $paymentMethod
) {
$this->scopeConfig = $scopeConfig;
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->rzp = $paymentMethod->rzp;
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
}

/**
* @param GetCartForUser $getCartForUser
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (empty($args['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

try
{
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

$storeId = (int) $context->getExtensionAttributes()->getStore()->getId();

$maskedCartId = $args['cart_id'];

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
$receipt_id = $cart->getId();
$amount = (int) (number_format($cart->getGrandTotal() * 100, 0, ".", ""));
$payment_action = $this->scopeConfig->getValue('payment/razorpay/payment_action', $storeScope);

$payment_capture = 1;

if ($payment_action === 'authorize')
{
$payment_capture = 0;
}

$order = $this->rzp->order->create([
'amount' => $amount,
'receipt' => $receipt_id,
'currency' => $cart->getQuoteCurrencyCode(),
'payment_capture' => $payment_capture,
'app_offer' => (($cart->getBaseSubtotal() - $cart->getBaseSubtotalWithDiscount()) > 0) ? 1 : 0,
]);

if (null !== $order && !empty($order->id))
{

$responseContent = [
'success' => true,
'rzp_order_id' => $order->id,
'order_quote_id' => $receipt_id,
'amount' => number_format((float) $cart->getGrandTotal(), 2, ".", ""),
'currency' => $cart->getQuoteCurrencyCode(),
'message' => 'Razorpay Order created successfully'
];


//save to razorpay orderLink
$orderLinkCollection = $this->_objectManager
->get('Razorpay\Magento\Model\OrderLink')
->getCollection()
->addFilter('quote_id', $receipt_id)
->getFirstItem();

$orderLinkData = $orderLinkCollection->getData();

if (empty($orderLinkData['entity_id']) === false)
{
$orderLinkCollection->setRzpOrderId($order->id)
->setRzpOrderAmount($amount)
->save();
}
else
{
$orderLink = $this->_objectManager->create('Razorpay\Magento\Model\OrderLink');
$orderLink->setQuoteId($receipt_id)
->setRzpOrderId($order->id)
->setRzpOrderAmount($amount)
->save();
}

return $responseContent;

}else
{
return [
'success' => false,
'message' => "Razorpay Order not generated. Something went wrong",
];
}
}
catch (\Razorpay\Api\Errors\Error $e)
{
return [
'success' => false,
'message' => $e->getMessage(),
];
}
catch (\Exception $e)
{
return [
'success' => false,
'message' => $e->getMessage(),
];
}
}
}
Loading