0

I'm kinda new to this part of programming, and I find it really confusing.

I have a script that makes some contact with a third party api. It basically adds a customer to their data.

What I heard is that they do something on the background, that checks if the newly added customer paid.

When their system notifies that, they're basically making an api call to a custom route of mine, and send a json object to that route.

I googled a bit, and came to this:

add_action('rest_api_init', function(){
    register_rest_route('sportivity-callback', '/endpoint', [
        'method' => 'GET',
        'callback' => 'rest_api_init',
    ]);
});

function rest_api_init($request){
    $json = $request->get_json_params();
    return $json;
}

Am I explaining it right? Here I use the rest api init hook. I register a custom route, for the third party. I add a method to that route. that returns the json, that they've sent.

3
  • 1
    Yes, this is a fairly common pattern e.g. payment providers, some of the oAuth2 flows. I'm a bit lost though: what are you asking / what do you need help with? What to do with the JSON they send you? I'd guess just update the customer / order / whatever you sent to them in your database.
    – Rup
    Aug 5 at 10:10
  • 1
    I'd be surprised if their callback was a GET though. I'd expect it to be a POST and them to post the JSON content.
    – Rup
    Aug 5 at 10:12
  • @Rup I basically need to inserrt a membership whenever they retrieve a customer ID. But I'm not sure how to test this. I went to the route and it just gave me a 404. Also I changed it to a POST.
    – user224670
    Aug 5 at 10:13

1 Answer 1

2

I think I managed to fix it

add_action('rest_api_init', function(){
    register_rest_route('endpoint', '/sportivity', [
        'method' => 'POST',
        'callback' => 'sportivity_endpoint',
    ]);
});

function sportivity_endpoint($request){
    $json = $request->get_json_params();
    $json['test'] = 'Jan';
    return $json;
}

When I go to url/wp-json/endpoint/sportivity it returns the json now.

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.