Please help us to make AbanteCart Ideal Open Source Ecommerce Solution for everyone.

Support AbanteCart eCommerce

Author Topic: Extend order (add a generated text)  (Read 4847 times)

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Extend order (add a generated text)
« on: September 26, 2019, 09:04:56 AM »
Hello

I want to call a php script (that generates a secret code) when the payment was accepted (--> order status Completed) so that this generated code is also saved to an order.
User Story: user purchases a digital game, downloads exe, but needs also a unique code that is generated when payment was successful. This code should be accessible by the user himself.

I assume I would need to add a new DB column to order_products or order_history, but then I haven't yet found the grips in terms of php.
I need to know where to invoke that script and what other parts I need to modify. Any help greatly appreciated ♥

Offline Basara

  • Administrator
  • Hero Member
  • *****
  • Posts: 5774
  • Karma: +274/-2
    • View Profile
Re: Extend order (add a generated text)
« Reply #1 on: September 27, 2019, 02:37:12 AM »
You can do this in your payment method (extension) code.

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #2 on: September 27, 2019, 09:15:08 AM »
your payment method extension code sounds like I programmed that  :-\
I just installed AbanteCart a few days ago and never learned php. I do know Java/C# but this framework is too big for me to properly understand yet, even after reading the create-extension-example.
Your comment doesn't really help me any further  :'(. What about extending the database? Is there no work beyond AbanteCart php to do?
Atm I only use Stripe, but if I also install the PayPal extension at a later point, do I need to extend that extension as well?

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #3 on: September 27, 2019, 11:03:21 AM »
What Basara means is that you need to edit your payment extension code:
If you are using paypal or authorize.net extension then you need to edit code in that folder
1. extensions\authorizenet\storefront\controller\responses\extension\authorizenet.php
2. extensions\authorizenet\storefront\model\extension\authorizenet.php
3. extensions\authorizenet\storefront\view\default\template\responses\authorizenet.tpl

Since you have basic knowledge of Java you can use it here and edit these files. You can just look at code its very neat and clear you will understand how you can pass your code to table orders after adding a new column to it.

You can see functions like public function recordOrder($order_info) or something like that based on your extension and then just add your newly added field into Insert Query.

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #4 on: September 27, 2019, 01:03:30 PM »
Maybe I can work better if I understand a few things more clearly:
1) when there's code like
public function update($order_id, $order_status_id, $comment = '', $notify = false) {
        $this->extensions->hk_update($this, $order_id, $order_status_id, $comment, $notify);
    }
    public function _update($order_id, $order_status_id, $comment = '', $notify = false){  }
(for some reason, I am not allowed to use the code-tag here  :o)
does this mean that now _update() is called? But why does the extension need to define this? I would rather think that only the second method is needed in the extension and the core stack will somehow call this new _update method
2) why do I need to alter the payment extension? Isn't my usecase something that has to be done when the invoice is created?
3) what is the difference between storefront/controller/responses and /storefront/controller/pages?
4) another example of where I don't understand php: the abstract class Model has a member $registry. If in eclipse I follow a method call of this member, it leads me to registry.php final class Registry. How can I or eclipse know the type of this member?? Uhm, is it that /** in php are not just comments??? If I delete the "comment" above the __construct method, eclipse doesn't know which type it is anymore.

If it would be easier, I think if I can just generate the unique code (only once per successful payment) and present it to the user somehow, I would also do this. In the end, I just need to give the code to the user somehow (mail would also be fine).
« Last Edit: September 27, 2019, 01:34:49 PM by Evergrowing »

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #5 on: September 27, 2019, 03:14:47 PM »
Can you please explain us in detail what are you trying to do rather than figuring out all these functions. Maybe there is one easy solution that I can think of from what I understood  \storefront\view\default\template\pages\checkout\success.tpl you can write your code here so that customer can get it after payment is confirmed. If you want to send it in email then you need to edit this \storefront\view\default\template\mail\order_confirm.tpl 

Every project has different structure and maybe you are trying to re-invent the wheel. Let us know what exactly you are trying to achieve and we can help you.

From what I understood is:
1.You need to pass a code to customer after payment is confirmed?
2.Store it in database so you can refer to it?


Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #6 on: September 27, 2019, 03:30:04 PM »
Well actually the storing in DB is optional. As long as he receives it via mail, that is also OK.
I thought if I put it into a .tpl file, then he can refresh the page and generate more codes as he likes (not that bad because hardly anyone would figure that out).
I just closed my VM and am about to shut down my computer. Tmr I will look into success.tpl
Thanks for your help so far :)

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #7 on: September 27, 2019, 04:54:43 PM »
okay
You can set it as cookie so that refresh issue is fixed https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username
and add your code in success.tpl


Like This :


<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(cname,cvalue,exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires=" + d.toGMTString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i < ca.length; i++) {
    var c = ca;
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function checkCookie() {
  var code=getCookie("codeCookie");
  if (code != "") {
    //alert("already set " + code);
  document.getElementById("random").innerHTML = code;
  } else {
  var code = Math.floor((Math.random() * 100000) + 1);
  document.getElementById("random").innerHTML = code;
     if (code != "" && code != null) {
       setCookie("codeCookie", code, 30);
     }
  }
}
</script>
</head>
<body onload="checkCookie()">
<p id="random"></p>
</body>
</html>

« Last Edit: September 27, 2019, 05:19:13 PM by Sam_78 »

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #8 on: September 28, 2019, 05:52:58 AM »
Well, calling my php script from there obviously works. But then, the user will only have the chance to see the code there. If he misses to write it down, he doesn't get another chance.
That's why I thought to either put it into the invoice (but the generation of the code shall only occur once) or into the confirmation email.

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #9 on: September 30, 2019, 09:14:24 AM »
you can use it in mail.tpl

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #10 on: October 03, 2019, 07:12:14 AM »
I see a problem in the fact that if you put the product in the cart twice or choose a completely different product, this will not be handled properly. So there has to be a connection with the order and the products within. I added a column to orders called unlockCodes which will be filled according to what the user purchased. And that needs to be visible in the invoice. Everything else is just hacked  ;D
Example 1:
2x Game PQ, 1x Game HX --> generate 2x PQ code, 1x a HX code and put those 3 into unlockCodes
Example 2:
3x free product --> unlockCodes stays empty

Here is what I did
invoice.php (NOT THIS ONE (this was my mistake) admin/controller/responses/sale)
Code: [Select]
                $this->data['orders'][] = array(
                    'order_id'           => $order_id,
                      .....
                    'unlockCodes'      => $unlockCodes,

order.php (NOT THIS ONE admin/model/sale but storefront/model/account)
Code: [Select]
public function getOrder($order_id) {
   ....
 $order_data = array(
                'order_id'                => $order_row['order_id'],
                  ....
                'unlockCodes'         => $order_row['unlockCodes'],
            );

public function addOrder($data) {
$this->db->query("INSERT INTO `".$this->db->table("orders")."`
                            SET .... unlockCodes = '".$data['unlockCodes']."',

invoice.tpl
Code: [Select]
<?php echo $unlockCodes?>
invoice.php (storefront/controller/pages/account/)
Code: [Select]
$this->data['unlockCodes'] = $myUnlockString
I must say, the folder structure and the fact that variables are named so that the origin is not really clear confused me quite a bit.

Still need help with:
order.php doesn't seem to be quite the right place I noticed. Because the codes are then created before successful payment. Because default_authorizednet build the page with
Code: [Select]
$this->response->setOutput(AJson::encode($json)) , I dont know where to put my code.
Also, how do I call a php script twice? Include('file') can only be used once but my result is just a single code and not multiple ones.
« Last Edit: October 03, 2019, 10:03:58 AM by Evergrowing »

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #11 on: October 03, 2019, 10:12:07 AM »
For every product is code same? for example
"2x Game PQ, 1x Game HX --> generate 2x PQ code, 1x a HX code and put those 3 into unlockCodes"
code for Game PQ will always be same?

If so than you need to add a field in product page (admin side) and then automatically it will be added to that order and you can display it in success.tpl as well as in invoice.
Consider SKU as example.
you can get SKU in invoice and on success page you can print them.
So create a similar field like SKU and it should solve your problem with less amount of code change
Another solution: There are multiple fields on product if you are not using any one of them for example 'length' or 'weight' or any other field just change its name to code and add code to individual product and use it wherever you want.

Offline Evergrowing

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Extend order (add a generated text)
« Reply #12 on: October 03, 2019, 10:19:30 AM »
The code is uniquely generated. Each code can only be used once, then it is invalidated.
Please, no hacks, I thought this through and this way (adding a field to table orders) is the only clean way of doing it. I just cannot find out how to write the code because of
Code: [Select]
$this->response->setOutput(AJson::encode($json))I see the line where the payment was successful, but I don't know how to call my script so that multiple codes get generated and putting this into the right variables

Offline Sam_78

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +42/-1
    • View Profile
Re: Extend order (add a generated text)
« Reply #13 on: October 03, 2019, 10:33:08 AM »
Which table have you added those column in? Have you tried function confirm in order.php and use $this->session->data[ 'codes' ];
« Last Edit: October 03, 2019, 10:36:04 AM by Sam_78 »

 

Powered by SMFPacks Social Login Mod