Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
Hey there,
I use Couch Cart on my website and I have integrated a custom payment gateway. Everything is fine, but I need to send information on a page to let the payment provider know that the payment was successful.
The problem is that when I put the php codes on the page into the <cms: php> tags, it gives an error. I share the page below. How can I use php codes on this page. Whatever I tried I was not successful :(

Code: Select all
<?php require_once ('couch/cms.php'); ?>

<cms:php>
   
   $post = $_POST;

   $merchant_key    = 'biX6xqTn2312MNC1BBwt';
   $merchant_salt   = 'hJTPi5pe2fd90231889WDZ1xRa';
   
   $hash = base64_encode( hash_hmac('sha256', $post['merchant_oid'].$merchant_salt.$post['status'].$post['total_amount'], $merchant_key, true) );

   if( $hash != $post['hash'] )
      die('PAYTR notification failed: bad hash');
   
   if( $post['status'] == 'success' ) {

   } else {

   }

   echo "OK";
   exit;

</cms:php>

<?php COUCH::invoke(); ?>
Hi,

I'll presume the code you posted is not in full (because, in its current form, there is no need to use Couch's <cms:php> block to execute your code - you may directly use raw PHP i.e. <?php .. />).

Anyway, in cases where it is necessary for Couch tags and raw PHP to interact, I have found the following very useful for debugging -
viewtopic.php?f=2&t=9882#p22669

Please use that technique to place your code in a PHP function (you may place that function in your kfunctions.php file).
Use Couch tags in your template to only call that PHP function. This way you should be able to troubleshoot with ease using your PHP debugger.

Hope this helps.
Hi KK,
Thanks for your reply. You are right the code is not full. I share the full code below.

Also as you said when I use raw php it work without problem.

Code: Select all
<?php require_once ('couch/cms.php'); ?>

<cms:php>
   
   $post = $_POST;

   $merchant_key    = 'biX6xqTn2312MNC1BBwt';
   $merchant_salt   = 'hJTPi5pe2fd90231889WDZ1xRa';
   
   $hash = base64_encode( hash_hmac('sha256', $post['merchant_oid'].$merchant_salt.$post['status'].$post['total_amount'], $merchant_key, true) );

   if( $hash != $post['hash'] )
      die('PAYTR notification failed: bad hash');
   
   if( $post['status'] == 'success' ) {
         
     <cms:set the_customer = "<cms:gpc 'g_ad_soyad' />" scope="global"/>
    <cms:set the_email = "<cms:gpc 'g_email' />" scope="global"/>
    <cms:set the_telefon = "<cms:gpc 'g_telefon' />" scope="global"/>
    <cms:set the_address = "<cms:gpc 'g_adres' />" scope="global"/>
    <cms:set the_price = "<cms:pp_total />" scope="global"/>
    <cms:set siparis_id="<cms:gpc 'siparis_no' method='get'/>" />
     
    <cms:show the_customer/><br>
    <cms:show the_email/><br>
    <cms:show the_telefon /><br>
    <cms:show the_address/><br>
    <cms:show the_price/><br>
    <cms:show siparis_id/><br>
   
    <cms:capture into='my_data' is_json='1'>
        <cms:show_repeatable 'purchase_items' as_json='1' />
    </cms:capture >
           
    <cms:capture into='my_data.' is_json='1'>
        {
            "purchase_item_id" : <cms:escape_json><cms:show title /></cms:escape_json>,
            "purchase_item_quantity" : <cms:escape_json><cms:show quantity /></cms:escape_json>
        }
    </cms:capture >

    <cms:db_persist
    _masterpage="siparisler.php"
    _mode='create'
    _invalidate_cache='0'
    auto_title='0'
    k_page_title=siparis_id
   
    purchase_name=g_ad_soyad
    purchase_email=g_email
    purchase_tel=g_telefon
    purchase_address=g_adres
    purchase_items=my_data
    purchase_price=the_price
    purchase_status="unconfirmed"
    user_access=g_kullanici_adi
                                />

    <cms:send_mail from="a@mail.com" to="<cms:show g_email />" bcc="a@mail.com" subject='Siparişiniz Tamamlandı' html='1'>
        <h3>Teşekkürler.</h3>
        <h4>Sipariş Detayları:</h4>
        <p>Ad Soyad: <cms:show g_ad_soyad /></p>
        <p>Email: <cms:show g_email /></p>
        <p>Adres: <cms:show g_adres /></p>
        <p>Satın Alınan Eğitimler:</p>
        <cms:pp_cart_items>
            <p href="<cms:show link />"><cms:show title /></p><br>
        </cms:pp_cart_items>
        <p>Toplam: <cms:pp_total /> TL</p>
    </cms:send_mail>

   } else {

   }

   echo "OK";
   exit;

</cms:php>

<?php COUCH::invoke(); ?>
Like I mentioned in the linked post, it'd be easier to place the PHP in a separate function.
Please do the following -
place this function in your kfunctions.php -
Code: Select all
function my_verify(){
    $post = $_POST;

    $merchant_key    = 'biX6xqTn2312MNC1BBwt';
    $merchant_salt   = 'hJTPi5pe2fd90231889WDZ1xRa';

    $hash = base64_encode( hash_hmac('sha256', $post['merchant_oid'].$merchant_salt.$post['status'].$post['total_amount'], $merchant_key, true) );

    $result = 0;
    if( $hash == $post['hash'] && $post['status'] == 'success'){
        $result = 1;
    }

    return $result;
}

As you can see, this PHP function will return 1 or 0 depending on the test it does.

Our Couch code in the template can now be simplified to something like this -
Code: Select all
<cms:set my_success="<cms:php>echo my_verify();</cms:php>" />

<cms:if my_success >
    <cms:set the_customer = "<cms:gpc 'g_ad_soyad' />" scope="global"/>
    <cms:set the_email = "<cms:gpc 'g_email' />" scope="global"/>
    ..
    ..
    OK
<cms:else />
    VERIFICATION FAILED!
</cms:if>

Now, as you can see, the <cms:php></cms:php> part of our code has shrunk to just one line where we invoke the function created above.

Hope this helps.
You know you are great KK :) It works like a charm.

I said it before, but I want to say it again. Since Paypal is banned in my country, I cannot get a license. If there is a method I can pay by credit card (etc. Stripe), I want to buy a license for my every project. Because I owe a lot to you and couch cms.

Thanks :)
You are welcome :)

I am glad you find Couch useful for your work.
I appreciate your sentiment but, for now, PayPal is the only option for us so will have to make do with your well wishes instead :)

Thanks.
KK I know you can not response all of my question but I have one more problem.
We use <cms:pp_cart_items></cms:pp_cart_items> to show cart items. Is there anyway to limit and offset items in the cart?

I tried <cms:pp_cart_items limit='1' offset='1'> but It didn't worked :)
7 posts Page 1 of 1
cron