开发者

Weighted Payment Gateway Rotator

开发者 https://www.devze.com 2023-01-24 06:24 出处:网络
So my new mission (lol). I\'m looking to create a script that will rotate payment gateways for instance i\'ll add 5 payment gateways in a database and have to give th开发者_如何学Pythonem a weight all

So my new mission (lol). I'm looking to create a script that will rotate payment gateways for instance i'll add 5 payment gateways in a database and have to give th开发者_如何学Pythonem a weight all totaling up to 100% just like a load balancer. What is the best way and how should i structure the database to make this work where one gateway gets whatever weight percentage that is set for that payment gateway id. If you could led me in the right direction or have any good tutorials that would be greatly appreciated


My idea: Give each gateway a "hits" column. Containing the number of times people used it. And a percentage column-the desired weight for each gateway. So the table is: Name: gateways Columns: Gateway,Hits,Percentage

Then before sending the user the gateway, sum up all of the hits for all of the gateways, then divide the total hits to the hits for every gateway and store the values in an array. The results of the array will actually be the current percentage of the gateways. Then send the gateway with the biggest difference between the desired percentage and the current percentage. In PHP this would look something like this, after connecting to the database:

$cur_perc_query=mysql_query("SELECT * FROM gateways");
$totalhits=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$totalhits+=$cur_get['Hits'];
}
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$gate_perc[]=$cur_get['Percentage']-($totalhits/$cur_get['Hits']);
}
$highest=$gate_perc[0];
$gate_number=0;
foreach($gate_perc as $numb => $value) {
if ($highest<$value) { $highest=$value; $gate_number=$numb; }
}
$count=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
if ($count=$gate_number) { $chosen_gate=$cur_get['Gateway']; }
$count++;
}
echo $chosen_gate;

I havent tested it but it should work this way. The other thing you need to do is add 1 to the hits of the current gateway each time a user uses it. Hope this helped.

0

精彩评论

暂无评论...
验证码 换一张
取 消