答案 2:但是如果您可以编辑 PayPal 的代码,这将可以正常工作...
(PayPal:如果您正在阅读本文,请实现此功能!)
编辑:不再需要 - 请参阅 Simon Labrecque 的回答
因此,在花费了 HOURS 并围绕我可以在实时和沙盒之间切换端点的期望编写了我的应用程序之后(就像我以前直接调用 SOAP 服务时一样),我决定继续深入研究源代码并弄清楚。
这是我为使其正常工作所做的更改。
假设:
步骤:
对 SimonsSOAPAPICallHandler.cs 的更改
更改构造函数以添加布尔值useSandbox:
注意:它必须是第一个参数,因为我们很快就会做一些搜索和替换魔法。
public SimonsSOAPAPICallHandler(bool useSandbox, string rawPayLoad, string attributesNamespace,
string headerString)
: base()
{
this.rawPayLoad = rawPayLoad;
this.nmespceAttributes = attributesNamespace;
this.headElement = headerString;
// you can get these from config if you wish but I doubt they'll ever change
this.endpoint = useSandbox ? "https://api-3t.sandbox.paypal.com/2.0" : "https://api-3t.paypal.com/2.0";
}
更改GetEndPoint():
/// <summary>
/// Returns the endpoint for the API call
/// </summary>
/// <returns></returns>
public string GetEndPoint()
{
return this.endpoint;
}
添加对应的成员:
/// <summary>
/// Endpoint
/// </summary>
private string endpoint;
对 SimonsPayPalAPIInterfaceServiceService.cs 的更改
修改构造函数添加useSandbox参数
public SimonsPayPalAPIInterfaceServiceService(bool useSandbox)
{
this.useSandbox = useSandbox;
}
添加对应的成员
private bool useSandbox;
对此文件进行两次搜索和替换。每个人将有大约 100 个替代品
- 将
new DefaultSOAPAPICallHandler( 替换为new SimonsSOAPAPICallHandler(useSandbox,
- 将
DefaultSOAPAPICallHandler defaultHandler 替换为var defaultHandler
您刚刚所做的是将useSandbox 作为参数添加到SimonsSOAPAPICallHandler(谢天谢地实现了IAPICallPreHandler)的构造函数中,您最终会为每个方法得到这个:
public DoExpressCheckoutPaymentResponseType DoExpressCheckoutPayment(DoExpressCheckoutPaymentReq doExpressCheckoutPaymentReq, string apiUserName)
{
IAPICallPreHandler apiCallPreHandler = null;
string portName = "PayPalAPIAA";
setStandardParams(doExpressCheckoutPaymentReq.DoExpressCheckoutPaymentRequest);
var defaultHandler = new SimonsSOAPAPICallHandler(useSandbox, doExpressCheckoutPaymentReq.ToXMLString(null, "DoExpressCheckoutPaymentReq"), null, null);
apiCallPreHandler = new MerchantAPICallPreHandler(defaultHandler, apiUserName, getAccessToken(), getAccessTokenSecret());
((MerchantAPICallPreHandler) apiCallPreHandler).SDKName = SDKName;
((MerchantAPICallPreHandler) apiCallPreHandler).SDKVersion = SDKVersion;
((MerchantAPICallPreHandler) apiCallPreHandler).PortName = portName;
string response = Call(apiCallPreHandler);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(response);
XmlNode xmlNode = xmlDocument.SelectSingleNode("*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='DoExpressCheckoutPaymentResponse']");
return new DoExpressCheckoutPaymentResponseType(xmlNode);
}
就是这样!
现在当你调用一个方法时你可以说...
bool useSandbox = true; // or false
var service = new SimonsPayPalAPIInterfaceServiceService(useSandbox);
然后正常调用方法
caller.DoExpressCheckoutPayment(pp_request, config.AccountName);
注意:它仍然会在您的配置中查找帐户名称以找到相应的密钥。显然请小心更新到更高版本的 Merchant SDK,因为您将不得不重新执行此操作。
我希望有人觉得这很有用:-)