insert data from one api to another api

0

I managed to install an API so that customers can subscribe, but now I have to pass the data of the subscription form once validated to another page, which is actually a page made also with another API. How could that be done?

I think the function should be implemented in the file that the API calls checkout.php:

$subscription = $result->subscription();
    // var_dump($subscription); 
    $queryParameters = "subscription_id=" . urlencode($subscription->id);
    $jsonResp["forward"] = "thankyou?" . $queryParameters;

    //echo json_encode($jsonResp, true);
    echo "<div class='respuesta'><h1>Thanks for subscribing</h1></div>";
    //  header('Status: 301 Moved Permanently', false, 301);
    header("Location: https://aem-author-proda4.bmw.com/content/bmw/marketAT/bmw_at/de_AT/campaign/bmw-prime/thank-you.html?wcmmode=disabled" );
    exit();


} catch(ChargeBee_InvalidRequestException $e) {
    handleInvalidRequestErrors($e, "plan_id");
} catch(Exception $e) {
    handleGeneralErrors($e);
}
?>
    
asked by diego pichaco 09.08.2018 в 06:13
source

1 answer

0

It all depends on how you declared the endpoint of your APIs, which you should include in your question. Even if your question is incomplete, someone can come up with a similar question and I'll leave a response that in a general way can serve what you want, although I doubt it applies directly to your particular case.

In general, , as I said, if the endpoint where you receive the subscription request is declared as:

$app->post('/suscribir', function($request, $response) {

});

(Which would be expected if you are using a framework or router that complies with PSR-7 )

You can get the parameters sent to your endpoint using:

$app->post('/suscribir', function($request, $response) {
    $parametros = $request->getParsedBody();
});

And since you're apparently using ChargeBee , send those parameters by:

$app->post('/suscribir', function($request, $response) {

  $parametros = $request->getParsedBody();

  $body = $response->getBody(); 

  try {

    ChargeBee_Environment::configure("{site}","{site_api_key}");
    $result = ChargeBee_Subscription::create($parametros);
    $subscription = $result->subscription();
    $status = 200;
    $msg = 'Subscripción exitosa: '.$subscription->id;

  } catch(ChargeBee_InvalidRequestException $e) {
    $status = 400;
    $msg = 'Hubo un problema: '.$e->getMessage();

  } catch(Exception $e) {
    $status = 500;
    $msg = 'Hubo otro problema: '.$e->getMessage();

  } finally {
    $body->write($msg);
    return $response
        ->withStatus($status)
        ->withHeader('Content-type', 'text/html')
        ->withBody($body);

  }
});

If the request to your endpoint was made through ajax and you expect a response in json format, the finally block should be instead:

 return $response
        ->withStatus($status)
        ->withHeader('Content-type', 'text/html')
        ->withJson(['resultado'=>$msg]);

Looking at the code that you put, it seems to me that what you need is to parse the first payload (that you receive in your endpoint) and then handle the answer.

    
answered by 09.08.2018 в 14:57