65.9K
CodeProject 正在变化。 阅读更多。
Home

用于通过 Authorize 网关提交信用卡交易的 .NET 封装

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (9投票s)

2008 年 4 月 11 日

CPOL

1分钟阅读

viewsIcon

42804

downloadIcon

343

Authorize 的 .NET 封装

引言

最近,我需要编写一个处理信用卡交易的网站,令我震惊的是,Authorize 作为较大的支付网关之一,并没有提供任何形式的 .NET API。他们支持发布定义交易的字符串的 HTTP Posting,并发送一个描述结果的字符串。因此,我编写了一个用于我们网站的封装类,并决定在这里提供给任何可能需要它的人。(这也是获取我正在使用的代码的同行评审的好方法……)

Using the Code

这旨在用作一个库。该解决方案有两个项目,一个是作为 DLL 的 Authorize 代码,另一个是演示如何使用该库的控制台应用程序。Authorize 需要三条信息,即您登录到他们的系统。这些信息由静态属性提供,然后您构建一个请求对象,设置其属性,添加行项目,然后发送交易,获取一个响应,该响应将告诉您该过程是成功还是失败,以及原因。

我没有公开的一个属性是这个

 private bool _testRequest = false; 

设置此属性将在测试服务器和正式服务器之间切换,因此我预计用户将在测试期间将其设置为 true,然后在发布时将其改回。Authorize 文档指定了测试服务器和正式服务器的几个 URL,我代码中的 URL 在编写时是正确的,如果它们发生更改,我显然会知道,并努力使其保持最新。

以下是我的示例应用程序的代码,显示了如何使用该库

    //first set the properties
    AuthorizeRequest.Login = "myLogin";
    AuthorizeRequest.Password = "MyPassword";
    AuthorizeRequest.TransactionKey = "whatever";

    // optionally set other settings
    AuthorizeRequest.Operation = AuthorizeRequest.Type.AUTH_CAPTURE;
    AuthorizeRequest.TransactionType = AuthorizeRequest.Method.CC;

    // now build the request
    AuthorizeRequest request = new AuthorizeRequest();

    request.ClientFirstName = "fred";
    request.ClientLastName = "Jones";
    request.CompanyName = "Acme";
    request.Address = "Address";
    request.City = "city";    
    request.Country = "India";

    request.CardNumber = "number";
    request.ExpDate = "12/2008";
    request.Ccv = "CCV is not used unless you set it up on the Authorize end (" +
                  " this costs extra )";
    
    request.ClientEmail = "this is for a receipt emailed by Authorize";
    request.MerchantEmail = "this is for an email sent to the merchant";

    // Now add the items to be purchased.
    request.LineItems.Add(new AuthorizeRequest.LineItem("001", "product standard", 
                                              "product standard", 1, 19.95, false));
    request.LineItems.Add(new AuthorizeRequest.LineItem("002", "product upgrade", 
                                              "product upgrade", 1, 9.95, false));

    // finally add the price
    request.Freight = 9.95;

    // Note the shipping is specified separately for invoicing purposes, but Amount is 
    // the exact amount that will be charged. This includes tax, you need to calculate 
    // it separately, if need be.
    request.Amount = (Decimal)(19.95 + 9.95 + 9.95); 

    // Now send the transaction
    AuthorizeResponse response = AuthoriseTransaction.SendRequest(request);

    if (response.TransactionResult != AuthorizeResponse.Result.Approved)
    {
        // work out what went wrong
    }
    else
    {
        // provide the transaction Id to the user
    }
}

历史

  • 2008 年 4 月 11 日:版本 1.0 - 初始发布
© . All rights reserved.