0

I want to trigger a 3rd party api when admin clicking on the 'Send Contract'button in Sales Order View.

enter image description here

This is the code for my button plugin:

<?php
  namespace MarsMage\Eversign\Plugin\Sales\Block\Adminhtml\Order;

  use Magento\Sales\Block\Adminhtml\Order\View as OrderView;

  class View
{
public function beforeSetLayout(OrderView $subject)
{
    $subject->addButton(
        'send_contract_button',
        [
            'label' => __('Send Contract'),
            'class' => __('send-contract'),
            'id' => 'send-contract-button',
            'onclick' => 'setLocation(\'' . $subject->getUrl('module/controller/action') . '\')'
        ],
    );

    $subject->addButton(
        'send_hire_purchase_button',
        [
            'label' => __('Send Hire Purchase'),
            'class' => __('send-hire-purchase'),
            'id' => 'send-hire-purchase-button',
            'onclick' => 'setLocation(\'' . $subject->getUrl('eversign/sendhirepurchase') . '\')'
        ],
    );
}

 }

This is the code for my controller:

<?php
  namespace MarsMage\Eversign\Controller\Adminhtml;

  use Magento\Framework\App\Action\Context;

  class SendHirePurchase extends \Magento\Framework\App\Action\Action
   {
      /** @var  \Magento\Framework\View\Result\Page */
      protected $resultPageFactory;

     /*** @param \Magento\Framework\App\Action\Context $context      */
      public function __construct(
         Context $context, 
         \Magento\Framework\View\Result\PageFactory $resultPageFactory,
          \MarsMage\Eversign\Helper\Data $data
           )
       {
         $this->resultPageFactory = $resultPageFactory;
            $this->helper = $data;
         parent::__construct($context);
          }


       public function execute()
       {
         $this->sendHirePurchase();
        }

       public function sendHirePurchase()
       {
         try {
    
        $postData = [
            
    'sandbox' => 0,
    'template_id' => 'b4edc68efca14b44a6aec31c6a7ad9dd',
    'title' => 'Hire Purchase Agreement',
    'message'=> 'This is my message.',
    'custom_requester_name' => '',
    'custom_requester_email' => '',
    'redirect'=> 'https://myredirect.com/completed',
    'redirect_decline' => 'https://myredirect.com/declined',
    'client' => '',
    'expires'=> 1494276966,
    'embedded_signing_enabled' => 0,
    'signers' => [
        
    [
        'role'=> 'Client',
        'name'=> 'Chris',
        'email' => '[email protected]',
        
        'message' => 'This is my custom message to Paul.',
        'deliver_email' => '',
        'language' => 'en'
    ], 
    
   
],
    'recipients'=> [
    
    [
        'role' => 'Motorkap',
        'name' => 'Motorkap',
        'email' => '[email protected]',
        'language' => 'en'
    ],
],
    'fields' => [
        [
        'identifier' => 'full_name',
        'value'=> 'Chris Lee'
        ], 
        
        [
        'identifier' => 'cash_price',
        'value'=> 12000
        ],
]];
        
        $apiKey = "9b735cc49cf593754c8cc54cec2cc71eReset";
        $business_id= "280557";
       $url="https://api.eversign.com/api/document?access_key=" . $apiKey . "&business_id=" . $business_id;

        $this->curl->post($url, $postData);
        // Response Text
        $response = $this->curl->getBody();
        // Response Header
        $responseHeader = $this->curl->getHeaders();
        // Response cookies
        $responseCookies = $this->curl->getCookies();
        return $response;
    }
catch (Exception $e) {
    $return = ["status"=>false, "message"=> $e->getMessage()];
}
  }
    }

When I put the url in button onclick, it always return me invalid form key. What is the exact solution to trigger api when button onclick?

1 Answer 1

0

The code looks correct to me, nothing strange.

Maybe you forgot to declare your routes.xml file or maybe misconfig the route file?

3
  • I have declared routes.xml but still it return invalid key when button triggered.
    – Chris Lee
    Mar 18, 2021 at 1:29
  • You can easily check your routes setting by go directly to that route, see if it's handling correctly.
    – Jimmy
    Mar 18, 2021 at 1:34
  • or you can change the action to Magento's route, any route, if it's working then your route has problem, if it is NOT working, then your button setting has problem
    – Jimmy
    Mar 18, 2021 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.