Announcement

Collapse
No announcement yet.

Trying to Create a Hook

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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($chCURLOPT_URL'https://api.stripe.com/v1/customers');

                
    // set method
                
    curl_setopt($chCURLOPT_CUSTOMREQUEST'POST');

                
    // return the transfer as a string
                
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

                
    // set headers
                
    curl_setopt($chCURLOPT_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($chCURLOPT_POST1);
                
    curl_setopt($chCURLOPT_POSTFIELDS$body);

                
    // send the request and save response to $response
                
    $response curl_exec($ch);

                
    $http_status curl_getinfo($chCURLINFO_HTTP_CODE) . PHP_EOL;

                
    $stripe_customer_id json_decode($responsetrue);
                
    $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!

  • #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

    Comment


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

      Comment


      • #4
        \Espo\Core\Hooks\Base has been deprecated for long. Not to be extended.

        Comment

        Working...
        X