Forum

Thread tagged as: Question, Shop

Getting values back from perch_shop_checkout

I am integrating Perch shop with another app for posting classified adverts. Members can pay to upgrade their ads.

On successful payment I redirect the user to a page which triggers a database change (to upgrade the advert) and then they end up at the ‘success’ page with the review of their purchase.

It works but currently I’m using perch_shop_order_successful in the model as the green light for updating the database so technically anyone could pay for one ad, then if they guessed the redirect url they could keep visiting it for any advert on the site and upgrade it without paying again (as perch_shop_order_successful is session based).

I think what I need to do in the model is check that the advert ID number is equal to a value I pass to perch_shop_checkout on the cart page and only approve the database update if that is true. However I’m not sure if it’s possible to get back any of those values from perch_shop_checkout?

I can use ‘metadata’ to pass values to Stripe but don’t know if I can get those value and use them on my site after payment is complete?

If not, is there another way to achieve my goal?

Many thanks.

Stephen Turvey

Stephen Turvey 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

Are you recording the successful order ID in your app?

While perch_shop_order_successful() returns true in the same session, you can check which order it is referencing with perch_shop_successful_order_id():

$order_id = perch_shop_successful_order_id();

So you can probably perform a number of checks with Perch Shop functions and within your app (perhaps based on order ID).

Hi Hussein,

I'm not really sure how I can get that to work. Here is the logic I currently have which updates the db and upgrades an ad:

public function upgradeAd($token) 
    {
        $ad = $this->showAdDetails();
        if ($token == accessToken($ad->id) && $ad->is_upgraded == -1 && perch_shop_order_successful()) {
            $ad->upgraded_order_id = perch_shop_successful_order_id(); // record perch order ID
            $ad->is_upgraded = 1; // update upgrade status
            $ad->upgraded_date = R::isoDateTime(); // record date of upgraded status
            R::store($ad);
            return true;
        }
        return false;
    }

So that function runs from the return_url on perch_shop_checkout. Using perch_shop_successful_order_id() the only thing I can think of would be running a check on all the db's upgraded_order_id entries to ensure the number is unique? There's probably a simpler way I'm not seeing!

Drew McLellan

Drew McLellan 2638 points
Perch Support

the only thing I can think of would be running a check on all the db's upgraded_order_id entries to ensure the number is unique

That would be the correct way to do it. Make sure you only ratify each order once.

Thanks both, I have it working now :)