середа, 4 травня 2011 р.

Webmoney merchant form

On page:
<asp:Content ID="content" ContentPlaceHolderID="afterform" runat="server">
<uc:WebMoneyForm id="wmpay" runat="server" />
</asp:Content>
In code:
wmpay.Amount = 1.5;
wmpay.Description = "некий товар";
wmpay.OrderID = 1001;
wmpay.Purse = "Uxxxxxxxxxxxx";
wmpay.TestMode = true;


public class WebMoneyForm : System.Web.UI.Control
{
public string Action { get; set; }
public string Method { get; set; }
public double Amount { get; set; }
public string Description { get; set; }
public bool TestMode { get; set; }
public int OrderID { get; set; }
public string Purse { get; set; }

public Dictionary Params { get; private set; }

public WebMoneyForm()
{
Method = "post";
Action = "https://merchant.webmoney.ru/lmi/payment.asp";

Params = new Dictionary<string, string>();
}

public void AppParam(string name, string value)
{
if(Params.ContainsKey(name))
{
Params[name] = value;
}
else
{
Params.Add(name, value);
}
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.AddAttribute("id", ID);
writer.AddAttribute("name", ID);
writer.AddAttribute("action", Action);
writer.AddAttribute("method", Method);
writer.RenderBeginTag(HtmlTextWriterTag.Form);

AppParam("LMI_PAYMENT_AMOUNT", Math.Round(Amount, 2).ToString());
AppParam("LMI_PAYMENT_DESC_BASE64", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Description)));

AppParam("LMI_PAYMENT_NO", OrderID.ToString());
AppParam("LMI_PAYEE_PURSE", Purse);

if(TestMode) AppParam("LMI_SIM_MODE", "0");

foreach(var item in Params)
{
AddHidden(writer, item.Key, item.Value);
}

writer.RenderEndTag();
}

private void AddHidden(System.Web.UI.HtmlTextWriter writer, string name, string value)
{
writer.Write(string.Format("<input type='hidden' name='{0}' value='{1}' />\n", name, value));
}
}

Немає коментарів:

Дописати коментар