Skip to content

Commit

Permalink
Add structure for subscription creation user side
Browse files Browse the repository at this point in the history
  • Loading branch information
Hancil Sequeira committed Feb 28, 2022
1 parent 550f54c commit 2d88732
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
92 changes: 82 additions & 10 deletions catalog/controller/extension/payment/razorpay.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ControllerExtensionPaymentRazorpay extends Controller
public function index()
{
$data['button_confirm'] = $this->language->get('button_confirm');

$this->load->model('checkout/order');

$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
Expand All @@ -29,17 +29,33 @@ public function index()
{
$api = $this->getApiIntance();

$order_data = $this->get_order_creation_data($this->session->data['order_id']);
if($this->cart->hasRecurringProducts()){
$order_data = $this->get_subscription_order_creation_data($this->session->data['order_id']);

if(empty($this->session->data["razorpay_order_id_" . $this->session->data['order_id']]) === true)
{
$razorpay_order = $api->order->create($order_data);
if(empty($this->session->data["razorpay_subscription_id_" . $this->session->data['order_id']]) === true)
{
$subscription_order = $api->subscription->create($order_data);

$this->session->data["razorpay_order_id_" . $this->session->data['order_id']] = $razorpay_order['id'];
$this->session->data["razorpay_subscription_order_id_" . $this->session->data['order_id']] = $subscription_order['id'];
$data['razorpay_subscription_order_id'] = "sub_IrrtytdV5ECxa0";// $this->session->data["razorpay_subscription_order_id_" . $this->session->data['order_id']];
$data['is_recurring'] = false;
$this->log->write("RZP subscriptionID (:" . $subscription_order['id'] . ") created for Opencart OrderID (:" . $this->session->data['order_id'] . ")");
}

$this->log->write("RZP orderID (:" . $razorpay_order['id'] . ") created for Opencart OrderID (:" . $this->session->data['order_id'] . ")");
}
} else {
$order_data = $this->get_order_creation_data($this->session->data['order_id']);
$order_data["amount"] = 5000;

if(empty($this->session->data["razorpay_order_id_" . $this->session->data['order_id']]) === true)
{
$razorpay_order = $api->order->create($order_data);

$this->session->data["razorpay_order_id_" . $this->session->data['order_id']] = $razorpay_order['id'];
$data['razorpay_order_id'] = $this->session->data["razorpay_order_id_" . $this->session->data['order_id']];

$this->log->write("RZP orderID (:" . $razorpay_order['id'] . ") created for Opencart OrderID (:" . $this->session->data['order_id'] . ")");
}
}
}
catch(\Razorpay\Api\Errors\Error $e)
{
Expand All @@ -60,7 +76,6 @@ public function index()
$data['name'] = $this->config->get('config_name');
$data['lang'] = $this->session->data['language'];
$data['return_url'] = $this->url->link('extension/payment/razorpay/callback', '', 'true');
$data['razorpay_order_id'] = $this->session->data["razorpay_order_id_" . $this->session->data['order_id']];
$data['version'] = $this->version;
$data['oc_version'] = VERSION;

Expand All @@ -70,7 +85,7 @@ public function index()
$data['api_url'] = $api->getBaseUrl();
$data['cancel_url'] = $this->url->link('checkout/checkout', '', 'true');

if (file_exists(DIR_TEMPLATE.$this->config->get('config_template').'/template/extension/payment/razorpay'))
if (file_exists(DIR_TEMPLATE.$this->config->get('config_template').'/template/extension/payment/razorpay'))
{
return $this->load->view($this->config->get('config_template').'/template/extension/payment/razorpay', $data);
}
Expand All @@ -94,6 +109,34 @@ private function get_order_creation_data($order_id)
return $data;
}

private function get_subscription_order_creation_data($order_id){
$order = $this->model_checkout_order->getOrder($order_id);
// echo "<pre>"; print_r($order);die;
$order_product_details = $this->model_checkout_order->getOrderProducts($order_id);
//
// echo"<pre>";print_r($this->cart->getRecurringProducts());die;
// foreach ($this->cart->getRecurringProducts() as $item) {
// echo"<pre>";print_r($item);
// }die;
// echo "<pre>"; print_r($order_product_details);die;
return [
"customer_id" => $this->getRazorpayCustomerData($order),
"plan_id" => "plan_ItTR3Jou5MBZT2",
"total_count" => 3,
"quantity" => 2,//$order_product_details['quantity'],
"customer_notify" => 0,
"notes" => [
"source" => "opencart-subscription",
"merchant_order_id" => $order_id,
],
"source" => "opencart-subscription",

];

// echo"<pre>";print_r($order);die;

}


public function callback()
{
Expand Down Expand Up @@ -350,4 +393,33 @@ protected function getApiIntance()
return new Api($this->config->get('payment_razorpay_key_id'), $this->config->get('payment_razorpay_key_secret'));
}

/**
* This line of code tells api that if a customer is already created,
* return the created customer instead of throwing an exception
* https://docs.razorpay.com/v1/page/customers-api
* @param $order
* @return void
*/
protected function getRazorpayCustomerData($order)
{
try {

$api = $this->getApiIntance();
$customerData = [
'email' => $order['email'],
'name' => $order['firstname']. " ". $order['lastname'],
'contact' => $order['telephone'],
'fail_existing' => 0
];
$customerResponse = $api->customer->create($customerData);

return $customerResponse->id;
} catch (\Exception $e) {
$this->log->write("Razopray exception: {$e->getMessage()}");
$this->session->data['error'] = $e->getMessage();
echo "<div class='alert alert-danger alert-dismissible'> Something went wrong</div>";
exit;
}
}

}
7 changes: 7 additions & 0 deletions catalog/view/javascript/razorpay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$(document).ready(function() {
$('#button-cart').on('click', function (e) {
alert("here");
})


});

0 comments on commit 2d88732

Please sign in to comment.