Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

PayEase Payment Gateway For Magento 2

PayEase is the payment service provider in China. After converting our client site to Magento 2, i tried to find Payease payment extension. But unfortunately Payease was not there. So we are unable to process payment without Payease payment method. That is why i have developed my custom PayEase extension for Magento 2. I hope this will help someone else.

payease-payment-method-magento2


Download

Magento2 – Write Custom Mysql Query In External File

Here we learn how to write custom mysql add, update or delete query in external file

Suppose we have table ’employee’ with fields emp_id, emp_name, emp_code and emp_salary.

use Magento\Framework\App\Bootstrap;
require $_SERVER["DOCUMENT_ROOT"] . '/app/bootstrap.php';
 
$params = $_SERVER;
 
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('employee'); //table name with prefix

//Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql); // gives associated array, table fields as key in array.
 
//Delete Data from table
$sql = "Delete FROM " . $tableName." Where emp_id = 10";
$connection->query($sql);
 
//Insert Data into table
$sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
$connection->query($sql);
 
//Update Data into table
$sql = "Update " . $tableName . "Set emp_salary = 20000 where emp_id = 12";
$connection->query($sql);

Magento 2 Stock control issue in Cancelled/Failed order

I experience a problem with stock control in Magento 2. After placing an order, the inventory is reduced. However, when i cancel the payment, the qty is increased by 2. It looks like restoring the cart results in increasing the stocks, as well as canceling the order. So i think there is something wrong in magento's restore cart function.

You can remove the event that increases the stock on restoring a quote.
This will require an edit in a magento core file.

In: vendor/magento/module-catalog-inventory/etc/events.xml

remove:

<event name="restore_quote">
  <observer name="inventory" instance="Magento\CatalogInventory\Observer\RevertQuoteInventoryObserver"/>
</event>

Magento 2: Override/Rewrite Wishlist controller

In seeking how to override a controller in Magento 2, you find lots of different ways to proceed. Some work, some do not. I will show you how to override a controller very easily. I am going to override wishlist controller. Lets see step by step how to do.

Magento 2: Override/Rewrite Wishlist controller


Step1: Create directory structure

app/code/HK/Localwishlist/Controller/Wishlist/Add.php
app/code/HK/Localwishlist/etc/di.xml
app/code/HK/Localwishlist/etc/module.xml
app/code/HK/Localwishlist/registration.php

Step2: Add this code in your Add.php file

<?php
namespace HK\Localwishlist\Controller\Wishlist;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\AbstractIndex
{
    /**
     * @var \Magento\Wishlist\Controller\WishlistProviderInterface
     */
    protected $wishlistProvider;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @param Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
        ProductRepositoryInterface $productRepository
    ) {
        $this->_customerSession = $customerSession;
        $this->wishlistProvider = $wishlistProvider;
        parent::__construct($context);
        $this->productRepository = $productRepository;
    }

    public function execute(){
            
        echo "Test.....";
        
    }
}


Step3: Add this code in your di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="Magento\Wishlist\Controller\Index\Add" type="HK\Localwishlist\Controller\Wishlist\Add" />
</config>

Step4: Add this code in your module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="HK_Localwishlist" setup_version="1.0.0"/>
</config>

Step5: Add this code in your registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'HK_Localwishlist',
    __DIR__
);

Now use this command,
php bin/magento setup:upgrade

Download

Bitcoin Coinbase API for PHP - Example

Bitcoin is an innovative payment network and a new kind of money. Bitcoin is the world's most widely used alternative currency with a total market cap of approximately $10 billion. The bitcoin network is made up of thousands of computers run by individuals all over the world.

Bitcoin Coinbase API for PHP - Example


Here i am going to present a small example to send money with Coinbase API. You can create a new payment button with Coinbase api. Let see step by step how it will work.

Step 1: Create a payment.php file


<?php
require __DIR__ . '/vendor/autoload.php';

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;
use Coinbase\Wallet\Resource\Order;
    
$apiKey = 'API KEY';
$apiSecret = 'API SECRET';

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

$amount = 0.1;
$orderId = "YOUR ORDER ID";

$params = array(
    'name'          => 'Site order ID: '.$orderId,
    'amount'        => new Money($amount, 'USD'),
    'metadata'      => array('order_id' => $orderId),
    'auto_redirect' => true
);

$checkout = new Checkout($params);
$client->createCheckout($checkout);
$code = $checkout->getEmbedCode();

$redirect_url = "https://www.coinbase.com/checkouts/$code";
?>

Step 2: Create a notification.php file


<?php 
require __DIR__ . '/vendor/autoload.php';

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;
use Coinbase\Wallet\Resource\Order;

$apiKey = 'API KEY';
$apiSecret = 'API SECRET';
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

$raw_body = file_get_contents('php://input');
$signature = $_SERVER['HTTP_CB_SIGNATURE'];
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean
if($authenticity){
 $order = json_decode($raw_body, true);
    mail("YOUR EMAIL ID","Coinbase Patment Notifications",print_r($order, true));
}
?>

Step 3: Create a success.php file


<?php
$order = $_GET['order'];
var_dump($order);
?>

Note: You need to set these all files url in your coinbase merchant account


 

Copyright @ 2017 HKBlog.