Click to See Complete Forum and Search --> : WorldPay pricing code


JonnyWales
04-13-2003, 10:18 AM
My site sells just one type of product of business software with several editions at different prices and uses WorldPay for payment processing. The pricing is currently very simply and if somebody wants one unit then they pay say £500. If they want two units they pay £1,000, three copies then £1,500. Discounts for the purchasing of multiple licences is handled by recrediting their credit card following purchase ie. buy 5 copies for £2,500 and then receive £500 back. It is a bit messy and also confusing for customers.

What I now intend to do is have the first copy at full price and then all subsequent copies at half price. So, if 2 bought the cost would be 500+250=£750 (yep, reducing prices to try and get more multiple sales through). If 5 bought then the cost would be 500+250+250+250+250=£1,500.

The needed logic seems clear enough. The total purchase price that I am trying to achieve is :-

unitprice + ((quantity-1)*unitprice/2)

My problem is that I do not understand the WorldPay code. I can stumble about and know enough to implement the current pricing structure but not the new prices. A snippet of the code is as follows and if anyone can help to decipher it I would be very grateful :-

function calc(x) {
x.amount.value = 0;
var y = x.price.length;
var z = x.qty.length;
var a = Number(x.amount.value);
var b,c,d;
d = true;
while(y > 0) {
b = Number(CheckNull(x.price[y-1].value));
c = Number(CheckNull(x.qty[y-1].value));
if(c < 0) {
d = false;
c = 0;
x.qty[y-1].value = c;
}
a += (b * c);
// alert("x.price["+eval(y-1)+"].value = "+x.price[y-1].value+"\nx.qty["+eval(y-1)+"].value = "+x.qty[y-1].value+"\na = "+a);
y--;
}
if(d == false) {
alert("Negative quantities not permitted; these have been set to zero.");
}
return a;
}

Thanks
Jonny

khalidali63
04-13-2003, 10:41 AM
Look at this piece of code from your code posted above

a += (b * c)

where b is the price and c is the quantity.
you need to implement your formula

unitprice + ((quantity-1)*unitprice/2)

somwhere before it returns a;

a = b + ((c-1)*b/2);

something like that.


hope that helps

Cheers

Khalid

JonnyWales
04-13-2003, 11:29 AM
Khalid,

Excellent, thanks. I have got this to almost work. However, my logic is slightly flawed because if :-

a = b + ((c-1)*b/2);

and if quantity is zero then the price will still be positive.

Any ideas?
:)

khalidali63
04-13-2003, 11:50 AM
something like this may work

if(c==0){
a = -1
}else{
a = b + ((c-1)*b/2);
}

As a matter of fact you have this validation in the code look

if(c < 0) {
d = false;
c = 0;
x.qty[y-1].value = c;
}

Khalid

JonnyWales
04-13-2003, 12:25 PM
Khalid,

Thanks that now does exactly what I need.

An added complication (hopefully the last) is that the page has 5 items for sale and amended the code as you suggest works only for the 1st item. The others simply return a zero value. I'm not sure if the following is the relevent piece of code but any help would be appreciated :-

function add_items(x) {
var y = x.price.length;
var z = x.code.length;
var d = "";
if(y != z) alert("Missing items in form; found "+y+" occurrences of price and "+z+" of code.");
var c = 0;
while (c < y) {
if(Number(CheckNull(x.qty[c].value)) > 0) {
d += add_item(d,x.qty[c].value,x.code[c].value);
}
c++;
}
return d;
}

function add_item(d,q,c) {
var r = "";
if(d != "") {
r += ", ";
}
r += q + " " + c;
if(Number(CheckNull(q)) > 1) {
r += "'s";
}
// alert("q = "+q+"\nc = "+c+"\n r = "+r);
return r;
}