尝试关注.unbind()
method。
我已经尽力修复它,但仍然会遇到很多与缺少功能等相关的错误。这是我到目前为止所做的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PayPal.Api;
using System.Configuration;
using PayPal.Sample.Utilities;
using System.Web.Providers.Entities;
/// <summary>
/// Summary description for OAuthTokenCredential
/// </summary>
public class CredentialManager
{
Dictionary<string, string> _Config = null;
string _AccessToken = string.Empty;
APIContext _APIConText = null;
public CredentialManager()
{
// Get a reference to the config
var config = ConfigManager.Instance.GetProperties();
_Config = config;
// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
_AccessToken = accessToken;
//API Context
var apiContext = new APIContext(accessToken);
_APIConText = apiContext;
// Initialize the apiContext's configuration with the default configuration for this application.
apiContext.Config = ConfigManager.Instance.GetProperties();
// Define any custom configuration settings for calls that will use this object.
apiContext.Config["connectionTimeout"] = "1000"; // Quick timeout for testing purposes
// Define any HTTP headers to be used in HTTP requests made with this APIContext object
//if (apiContext.HTTPHeaders == null)
//{
// apiContext.HTTPHeaders = new Dictionary<string, string>();
//}
//apiContext.HTTPHeaders["some-header-name"] = "some-value";
}
public Payment GetPAyment(APIContext apiContext, string paymentid)
{
var payment = Payment.Get(apiContext, paymentid);
return payment;
}
public bool CreatePayment()
{
try
{
bool Success = false;
//var apiContext = Configuration.GetAPIContext();
string payerId = Request.Params["PayerID"];
if (string.IsNullOrEmpty(payerId))
{
var itemList = new ItemList()
{
items = new List<Item>()
{
new Item()
{
name = "Item Name",
currency = "USD",
price = "15",
quantity = "5",
sku = "sku"
}
}
};
var payer = new Payer() { payment_method = "paypal" };
var baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/PaymentWithPayPal.aspx?";
var guid = Convert.ToString((new Random()).Next(100000));
var redirectUrl = baseURI + "guid=" + guid;
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl + "&cancel=true",
return_url = redirectUrl
};
var details = new Details()
{
tax = "15",
shipping = "10",
subtotal = "75"
};
var amount = new Amount()
{
currency = "USD",
total = "100.00", // Total must be equal to sum of shipping, tax and subtotal.
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "Transaction description.",
invoice_number = Common.GetRandomInvoiceNumber(),
amount = amount,
item_list = itemList
});
var payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
var createdPayment = payment.Create(_APIConText);
var links = createdPayment.links.GetEnumerator();
while (links.MoveNext())
{
var link = links.Current;
if (link.rel.ToLower().Trim().Equals("approval_url"))
{
this.flow.RecordRedirectUrl("Redirect to PayPal to approve the payment...", link.href);
}
}
Session.Add(guid, createdPayment.id);
Session.Add("flow-" + guid, this.flow);
}
else
{
var guid = Request.Params["guid"];
var paymentId = Session[guid] as string;
var paymentExecution = new PaymentExecution() { payer_id = payerId };
var payment = new Payment() { id = paymentId };
var executedPayment = payment.Execute(apiContext, paymentExecution);
}
return Success;
}
catch(Exception ex)
{
return false;
}
}
}
我在以下内容中收到随机错误:
using PayPal.Sample.Utilities;
string payerId = Request.Params["PayerID"];
var baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/PaymentWithPayPal.aspx?";
invoice_number = Common.GetRandomInvoiceNumber(),
Session.Add(guid, createdPayment.id);
Session.Add("flow-" + guid, this.flow);
var guid = Request.Params["guid"];
var paymentId = Session[guid] as string;
var executedPayment = payment.Execute(_APIConText, paymentExecution);
错误:
我想我只是错过了一个参考或其他东西。我试图为C#ASP .Net(不是MVC)
做这件事答案 0 :(得分:3)
你会发现&#34; Common&#34;班here
Common.GetRandomInvoiceNumber()只是生成一个随机数。我想你只需用你自己生成的发票号替换它。