Thursday, April 2, 2020

LSL - Vendor Script with Google Docs Tracking

Better late than never I suppose.  Last post, like 2 years ago, I talked about how to set up a Google Form to track data in a Google Sheet for use in LSL scripts.  It's very similar to how you would make a GET call in a PHP web form. 

Anyway, I'm here to post a Vendor Script that is designed to work with the Google set up to track sales.  The form values you want to create in the Google Form are: Avatar, Location, Price, and Item.

The script is available below, you can also have one delivered through the Marketplace.  I have used this vendor script in world but I didn't build the base Vendor functionality, so I would test it out with a cheap price and a friend or an alt to make sure it's working properly.  I take no responsibility if this screws up and you lose L$. 

This script should be placed in the root prim of a vendor along with an informational Notecard of some kind, and a single item to be distributed on payment.  You'll need to box up items that have several sizes or components, the vendor will only distribute ONE item even if you put several items inside of it.


// Vendor Script with Google Spreadsheet Sales Tracking
// This script was altered fromt he original (Info below) by Ramen Jedburgh (SL, OSGrid)
// 2018.01.21
// It has been altered to record sales in a Google Spreadsheet.

// Original Boilerplate

// Script for Vendor Posters. Gives Pre-purchase Notecard & Track Sales Information
//
// I do not have the original script author on this script.  Please notify me
//   if you know who that is so that appropriate credit can be given.
//   I, Chimera Firecaster, made some very minor changes in Feb, 2011, but the major
//   credit needs to go to a very generous person out there who made this freely
//   available to others in the Second Life Community.  This should remain free.
//   Please do not sell it.  That will most certainly bring bad Karma upon you.
//
// What this script does:
//
//   1. When the customer left clicks, this script provides them with a notecard with
//      information about the item they are thinking about purchasing
//      When the owner (you) left click, it provides information on how many of the
//      of the items have been sold and total sales.  You also get the notecard, but
//      just disregard that.
//
//   2. When the customer right clicks, they have the option of selecting "PAY" to
//      purchase the Item
//
// What you need to do:
//
//  1. Below you will see enter your price here.  Change the number to the amount you are
//     charging for the product.
//
//  2. This script requires that you include a notecard.  The notecard should include
//     information on the product.  It's the same sort of text that you would use for
//     selling the product on Second Life Marketplace.  Name your note card something like
//     Pre-purchase Information on Such-and-such product.  The note card should be placed
//     in the vendor poster, along with a box containing the product.
//





//----------------------------------------------------------------------------------
//              !!! EDIT STUFF HERE !!!
//    Edit Google Form Info and the price of your item, defaul 100L
//    For Information on Setting up Google Docs to recieve Data please visit:
//  https://allaroundthegrid.blogspot.com/2018/02/setting-up-google-docs-form.html
//----------------------------------------------------------------------------------
// Add Your Docs URL
string FrontPart = "https://docs.google.com/forms/d/EDIT_THIS_TO_ADD_THE_LONG_RANDOM_STRING";
//Google Form Key for Location
string LocationKey = "";
// Google Form Key for Avatar
string AvatarKey = "";
// Google Form Key for Item
string ItemKey = "";
// Google form key for Price
string PriceKey = "";
// This can be anything, it's for your reference ie "My Store"
string GridName = "";

// Enter Your Price here
integer gCorrectAmount = 100; //Replace the number with the price of your items
// Replace only the number, be sure to leave the semicolon after the number


// The Vendor should have this script, a notecard of somekind (with store info or whatever) and a boxed/bagged item inside.
// This Vendor Script will only deliver one item, so items must be boxed/bagged


//-------------------------------------------------------
//          !!! YOU SHOULD NOT EDIT BELOW THIS!!!
//-------------------------------------------------------



// Used for Google Forms
string Location;
string Avatar;
string URL;
       
integer totalsold = 0;
integer totalamount = 0;

default
{
    state_entry()
    {
        llRequestPermissions(llGetOwner(),PERMISSION_DEBIT);
        llSetPayPrice(PAY_HIDE, [gCorrectAmount, PAY_HIDE, PAY_HIDE, PAY_HIDE]);
        Location = llGetRegionName()+" on " + GridName;
    }
    
    touch_start(integer total_number)
    {
        llOwnerSay((string)totalsold +" units have been sold since object rez, L$" + (string)totalamount +" collected so far.");
        llInstantMessage(llDetectedKey(0), "Pre-purchase information on this item is being provided to you in a notecard. To purchase, RIGHT CLICK and select PAY."); //remove if no notecard
        llGiveInventory(llDetectedKey(0),llGetInventoryName(INVENTORY_NOTECARD, 0)); //remove if no notecard
    }
   
    money(key id, integer amount)
    {
        if (amount == gCorrectAmount)
        {
            // correct amount paid
            llInstantMessage(id, "Thank you for your purchase! Accept your product by selecting OK.");
            Avatar = llKey2Name(id);
            string itemsoldname=llGetInventoryName(INVENTORY_OBJECT, 0);
            llGiveInventory(id, itemsoldname);
            totalsold = totalsold + 1;
            totalamount = amount * totalsold;
            llInstantMessage(llGetOwner(), (string)llKey2Name(id) + " has paid " +  (string)amount + " in "+ llGetRegionName());
            string URL = FrontPart + "/formResponse?ifq&entry." + LocationKey + "=" + Location + "&entry." + AvatarKey + "=" + Avatar + "&entry." + ItemKey + "=" + itemsoldname + "&entry." + PriceKey + "=" + (string)amount + "&submit=Submit";
          
            key httpkey=llHTTPRequest(URL, [] ,"");
        }
       
        else if (amount < gCorrectAmount)
        {
            llSay(0,"You didn't pay enough, " + llKey2Name(id) + ". Refunding your payment of L$" + (string)amount + ".");
            llGiveMoney(id, amount);
        }
       
        else
        {
            integer refund = amount - gCorrectAmount;
            llSay(0,"You paid too much, " + llKey2Name(id) + ". Your change is L$" + (string)refund + ".");
            llGiveMoney(id, refund);
        }
    }
}