0

I use the Drupal Commerce Stripe module to create subscription charges in a Drupal web, through the Stripe platform. Everything seems to work well, the clients are created, the charges are produced and appear in Stripe's Dashboard .... I also have a webhook that seems to work too. The problem I have is that I need to implement within the webhook itself the ability to complete certain fields with the response that Stripe sends to that webhook. These data must complete the profile of the user who made the payment. Any idea what this would be like?

I'm talking about Drupal 7

This is my webhook

<?php


require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_*************");


$input = @file_get_contents("php://input");
$event_json = json_decode($input);


$event_id = $event_json ->id;
$event_type = $event_json -> type;
$customer_email = $event_json -> receipt_email;

if ($event_type == "charge.succeeded") {
  $msg = "test webhook";
  mail($customer_email, "Test suscrip", $msg);

*/*Here I would like to be able to send certain data of the json object to certain fields of the user profile*/*

}


http_response_code(200);  





?>
  • Is this Commerce 2/Drupal 8? Also if you could ask the 2nd question (about subscriptions) in a separate question that would great. Q+A works best when there's one distinct problem per post – Clive Mar 22 '18 at 19:55
  • Ok, Clive, it's about Drupal 7, and I'm going to edit to put the subscriptions in another question. Apologies – Rafa Mar 22 '18 at 20:13
1

It sounds like you want to load the user object from the email address, set some field values, and save it again.

In code that would look something like this:

if ( ($account = user_load_by_mail($customer_email) ) {
  $account->field_foo[LANGUAGE_NONE][0]['value'] = $event_json->foo;
  $account->field_bar[LANGUAGE_NONE][0]['value'] = $event_json->bar;

  user_save($account);
}
  • Thanks again Clive, from what I understand is that you could modify the user directly in this way, you should not have any kind of login, tokens, cookies and these things for it? If so, you just cheered me up the day, I'm going to try and I'll comment on that here. I hope it works ... (beers for everyone if it's like that) – Rafa Mar 22 '18 at 20:37
  • Im assuming your “webhook” is implemented with the Drupal API, if it’s a custom plain PHP file you’ll also need to bootstrap Drupal to make that API available. But if you the trust the source of the data, then there’s no extra authentication to perform like logging the user in, no – Clive Mar 23 '18 at 7:59
  • My webhook is as you see it, I think it has no reference to the Drupal API, in my supreme ignorance, how could Drupal boot in that PHP file? Thank you – Rafa Mar 23 '18 at 8:33
0

I added this to the webhook, and now it would be like that, but it does not seem to write anything in the given field. Any ideas?

<?php

define('DRUPAL_ROOT', '/var/www/html/'); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

require_once('vendor/autoload.php'); \Stripe\Stripe::setApiKey("sk_test_***********");

$input = @file_get_contents("php://input"); $event_json = json_decode($input);

$event_id = $event_json ->id; $event_type = $event_json -> type; $customer_email = $event_json -> receipt_email;

if ($event_type == "charge.succeeded") { $msg = "test webhook"; mail($customer_email, "Test suscrip", $msg); if ( ($account = user_load_by_mail($customer_email) ) { $account->field_id_subscripcion[UND][0]['value'] = 'test';

user_save($account); }

}

http_response_code(200);

?>

  • This code gives me 500 error in the Stripe dashboard when I do the test ... – Rafa Mar 23 '18 at 10:00
0

Thank you very much for your response, I have managed to access the Stripe response, and thanks to its response I have also managed to update the user. I owe you one, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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