Ok, I am trying to make an API call to create a hosted zone and update its DNS in AWS after the stripe webhook returns payment_succeeded. The API call is always successful, but Stripe fails with a 500 error and a retry. I've tried to use Jobs but still can't get it going.
This is from My StripeEventListener.php
<?php
namespace App\Listeners;
use GuzzleHttp\Client;
use App\Mail\WebsiteLive;
use GuzzleHttp\Middleware;
use App\Events\StripeEvent;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Handler\CurlHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Validator;
use Laravel\Cashier\Http\WebhookResponse;
use App\Mail\MailInvoiceOnSuccesfulPayment;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Cashier\Events\WebhookReceived;
use App\Jobs\UpdateAwsJob;
class StripeEventListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \App\Events\StripeEvent $event
* @return void
*/
public function handle(WebhookReceived $event) :void
{
if ($event->payload['type'] === 'invoice.payment_succeeded') {
$data = $event->payload['data']['object'];
$response = new Response('Webhook received.', 200);
$response->send();
UpdateAwsJob::dispatch($data)->onQueue('webhooks');
// Handle the incoming event... || $event->payload['type'] === 'customer.subscription.updated' || $event->payload['type'] === 'customer.subscription.created'
return;
// return response()->json(['message' => 'Webhook processing started']);
// Make the API call to your server to update AWS
// This could take some time...
// $client = new Client();
// $url = route('api.domain.register', ['stripe_id' => $data['customer']]);
// $client->request('GET', $url);
// echo $response->getBody();
// $response = WebhookResponse::create('Webhook received successfully.');
// $response->send();
// return WebhookResponse::create('Webhook received successfully.');
}
}
}
And this is my job
<?php
namespace App\Jobs;
use App\Models\User;
use GuzzleHttp\Client;
use GuzzleHttp\Middleware;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Handler\CurlHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
class UpdateAwsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
/**
* Create a new job instance.
*
* @param string $data
*/
public function __construct(Array $data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Make the API call to your server to update AWS
// This could take some time...
$amountPaid = number_format($this->data['amount_paid']/100, 2, '.', '');
$discount = $this->data['discount'] === null ? json_encode(['value' => null]) : json_encode(['id' => $this->data['discount']['coupon']['id'], 'percentage' => $this->data['discount']['coupon']['percent_off'], 'amount_off' => $this->data['discount']['coupon']['amount_off']]) ;
DB::table('payments')->insert([
'customer_id' => $this->data['customer'],
'collection_method' => $this->data['collection_method'],
'amount_paid' => $amountPaid,
'currency' => $this->data['currency'],
'created' => $this->data['created'],
'billing_reason' => $this->data['billing_reason'],
'amount_remaining' => $this->data['amount_remaining'],
'account_country' => $this->data['account_country'],
'discount' => $discount,
'created_at' => now(),
'updated_at' => now(),
]);
// $client = new Client();
// $url = route('api.domain.register', ['stripe_id' => $this->data['customer']]);
// $response = $client->request('GET', $url);
$this->registerDomain($this->data['customer']);
// Log::info($response);
// http_response_code(200);
// $response->getBody();
}
public function registerDomain($stripeId) {
$user = User::where('stripe_id', $stripeId)->first();
// $tenant = Tenant::where('user_id', $user->id)->first();
// $adminClientOrder = AdminClientOrder::where('tenant_id', $tenant->id)->first();
// $tenantPassTB = DB::table('tenant_password_tables')->where('tenant_id', $tenant->id)->first();
// if (!empty($tenant)) {
$domain = 'purityvendor';
$detail = [
'email' => $user->email,
'password' => 'hahkeil',
'domain' => $domain,
'name' => $user->firstname.' '.$user->lastname,
];
$domainDotCom = $domain.'.com';
$years = 1;
$key = env('NAMESILO_SANDBOX_API_KEY');
$api = env('NAMESILO_SANDBOX_API_URL');
// try {
// $URL = "{$api}/registerDomain?version=1&type=xml&key={$key}&domain={$domainDotCom}&years={$years}&private=1&auto_renew=1";
// $client = new \GuzzleHttp\Client();
// $response = $client->request('GET', $URL);
// $body = $response->getBody();
// $xml = simplexml_load_string($body);
// if (htmlentities((string)$xml->reply->code) == 300) { // && htmlentities((string)$xml->reply->detail) == 'success'
return $this->runAWSUtility($domainDotCom, $detail);
// return 200;
// }
// else {
// return response()->json(['message' => htmlentities((string)$xml->reply->detail), 'status' => htmlentities((string) $xml->reply->code)]);
// }
// } catch (\Throwable $th) {
// echo $th->getMessage();
// exit;
// }
// }
// else{
// return response()->json(['status' => 404, 'message' => 'Payment made against a physician that does not exist']);
// }
}
public function configureEmail($domainName, $key, $email, $forward1) {
$api = env('NAMESILO_API_URL');
try {
$URL = "{$api}/configureEmailForward?version=1&type=xml&key={$key}&domain={$domainName}&email={$email}&forward1={$forward1}";
$handler = new CurlHandler();
$client = new \GuzzleHttp\Client();
$tapMiddleware = Middleware::tap(function ($request) {});
$response = $client->request('GET', $URL, [
'handler' => $tapMiddleware($handler)
]);
$body = $response->getBody();
$xml = simplexml_load_string($body);
if (htmlentities((string)$xml->reply->detail) === 'success' && htmlentities((string)$xml->reply->code) === 300) {
// Request AWS Facility
// return
echo response()->json(["message" => htmlentities((string)$xml->reply->detail), "status" => htmlentities((string)$xml->reply->code)]);
}
else {
echo response()->json(['message' => htmlentities((string)$xml->reply->detail), 'status' => htmlentities((string) $xml->reply->code)]);
exit;
}
} catch (\Throwable $th) {
echo $th->getMessage();
exit;
}
}
public function runAWSUtility($domain, $detail){
try {
$handler = new CurlHandler();
$client = new \GuzzleHttp\Client();
$tapMiddleware = Middleware::tap(function ($request) {});
$data = ["DomainName" => $domain];
$response = $client->request('POST', env('AWS_UTILITY_URL'), [
'json' => $data,
'handler' => $tapMiddleware($handler)
]);
$body = $response->getBody();
$data = json_decode($body, true);
if ($data['status'] === 200) {
// Send mail and run the aws sendcommand api
// $this->sendMail($detail);
// return response()->json(['status' => 200], 200);
return $this->runAWSUtilityCommand($domain);
}
else {
echo 'Not available:'. $response->getStatusCode();
}
} catch (\Throwable $th) {
return $th->getMessage();
}
}
public function runAWSUtilityCommand($domain){
try {
$handler = new CurlHandler();
$client = new \GuzzleHttp\Client();
$tapMiddleware = Middleware::tap(function ($request) {});
$data = ["DomainName" => $domain];
$response = $client->request('POST', env('AWS_UTILITY_URL_SENDCOMMAND'), [
'json' => $data,
'handler' => $tapMiddleware($handler)
]);
$body = $response->getBody();
$data = json_decode($body, true);
if ($data['status'] === 200) {
return response()->json(['message' => 'Success', 'status' => $data['status'], 'commandID' => $data['commandID']]);
}
else {
echo 'Not available:'. $response->getStatusCode();
}
} catch (\Throwable $th) {
return $th->getMessage();
}
}
}
Please any help will be really appreciated. Thanks.