Trying to Create a Hook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickt
    Junior Member
    • Aug 2019
    • 6

    Trying to Create a Hook

    I'm trying to create a hook that occurs after saving a new Account. I have a custom field under the Account entity for a Stripe customer ID. I'm trying to use the hook feature to make a cURL request to Stripe to create a new customer ID and save the returned value in the field I have created when saving a new account.

    I looked at the hook examples and created this file, but it doesn't seem to be working (I did clear the cache as well):

    /custom/Espo/Custom/Hooks/Account/Stripe.php:

    PHP Code:
    <?php
    
    namespace Espo\Custom\Hooks\Account;
    
    use Espo\ORM\Entity;
    
    class StripeNew extends \Espo\Core\Hooks\Base {
    
        public function afterSave(Entity $entity, array $options = array()) {
    
            if ( $entity->isNew() && $entity->get('stripeCustomerID') == null ) {
    
                // get cURL resource
                $ch = curl_init();
    
                // set url
                curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/customers');
    
                // set method
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    
                // return the transfer as a string
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
                // set headers
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                  'Authorization: Basic xxxxx',
                  'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
                ]);
    
                // form body
                $body = [
                  'description' => $entity->get('name'),
                ];
                $body = http_build_query($body);
    
                // set body
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    
                // send the request and save response to $response
                $response = curl_exec($ch);
    
                $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
    
                $stripe_customer_id = json_decode($response, true);
                $stripe_customer_id = $stripe_customer_id['id'];
    
                // close curl resource to free up system resources
                curl_close($ch);
    
                $entity->set('stripeCustomerID', $stripe_customer_id);
    
                $this->getEntityManager()->saveEntity($entity, ['skipAll' => true]);
    
    
            }
    
        }
    }

    Any idea why this is not working? It seems like the cURL request doesn't even get made (since no new ID is created in my Stripe dashboard). I have tested the cURL request independently outside of Espo and it works fine.

    Also, I see these warnings in the log file:

    [2019-08-22 10:25:43] Espo.WARNING: E_WARNING: array_diff(): Argument #1 is not an array {"code":2,"message":"array_diff(): Argument #1 is not an array","file":"/var/www/application/Espo/Core/HookManager.php","line":166,"context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":{"Account":["Stripe.php"]},"hookFiles":["Stripe.php"],"scopeName":"Account","hookScopeDirPath":"cust om/Espo/Custom/Hooks/Account","normalizedScopeName":"Account","hookFile ":"Stripe.php","hookFilePath":"custom/Espo/Custom/Hooks/Account/Stripe.php","className":"\\Espo\\Custom\\Hooks\\Ac count\\Stripe","classMethods":null}} []

    [2019-08-22 10:25:43] Espo.WARNING: E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied for foreach()","file":"/var/www/application/Espo/Core/HookManager.php","line":168,"context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":{"Account":["Stripe.php"]},"hookFiles":["Stripe.php"],"scopeName":"Account","hookScopeDirPath":"cust om/Espo/Custom/Hooks/Account","normalizedScopeName":"Account","hookFile ":"Stripe.php","hookFilePath":"custom/Espo/Custom/Hooks/Account/Stripe.php","className":"\\Espo\\Custom\\Hooks\\Ac count\\Stripe","classMethods":null,"hookMethods":n ull}} []

    Thanks!
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,
    it seems : the fileName is Stripe.php and the class name is StripeNew

    and possible loop :
    $entity->set('stripeCustomerID', $stripe_customer_id);
    $this->getEntityManager()->saveEntity($entity, ['skipAll' => true]);


    Regards
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • nickt
      Junior Member
      • Aug 2019
      • 6

      #3
      Thank you! I updated the class name to match the file and it works as expected now.

      Comment

      • yuri
        Member
        • Mar 2014
        • 8440

        #4
        \Espo\Core\Hooks\Base has been deprecated for long. Not to be extended.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        Working...