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

Installation file, Admin Menu module, Plan Module #70

Open
wants to merge 2 commits into
base: openCart_subscription
Choose a base branch
from
Open
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
Binary file added .DS_Store
Binary file not shown.
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");
})


});
48 changes: 48 additions & 0 deletions install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Razorpay Payment Extension</name>
<version>2.0</version>
<author>Razorpay Team</author>
<link>https://razorpay.com</link>
<code>razorpay_payment_extension</code>
<description>Razorpay Payment with Subscription</description>
<file path="admin/controller/common/column_left.php">
<operation>
<search><![CDATA[ if ($this->user->hasPermission('access', 'marketplace/extension')) { ]]></search>

<add position="before"><![CDATA[
/* Admin Side menu */
$adminmenus = array();

if ($this->user->hasPermission('access', 'marketplace/extension')) {
$adminmenus[] = array(
'name' => 'Plans',
'href' => $this->url->link('extension/payment/razorpay_subscription/plan_list', 'user_token=' . $this->session->data['user_token'], true),
'children' => array()
);

}
if ($this->user->hasPermission('access', 'marketplace/extension')) {
$adminmenus[] = array(
'name' => 'Subscriptions',
'href' => $this->url->link('extension/payment/razorpay_subscription', 'user_token=' . $this->session->data['user_token'], true),
'children' => array()
);

}

if ($adminmenus) {
$data['menus'][] = array(
'id' => 'menu-megafooter',
'icon' => 'fa fa-share-alt fa-fw',
'name' => 'Razorpay Subscription',
'href' => '',
'children' => $adminmenus
);
}

/* Admin menu */ ]]></add>
</operation>
</file>

</modification>
21 changes: 0 additions & 21 deletions system/library/razorpay-sdk/libs/Requests-1.8.0/.codecov.yml

This file was deleted.

Loading