FoxPro and .NET Interop
Braintree integration via wwdotnetbridge
Gravatar is a globally recognized avatar based on your email address. Braintree integration via wwdotnetbridge
  Doug
  All
  Oct 24, 2019 @ 04:33pm

All,

Integrating with Braintree Payments using their .Net SDK. I have written the code in Visual Studio Code and it works. Now need some guidance on how to call that from VFP using wwdotnetbridge.

Here is the VSC :

using System;
using Braintree;

namespace Braintree_vsc
{
    class Program
    {
        static string Main(string[] args)
        {
            var gateway = new BraintreeGateway
            {
                Environment = Braintree.Environment.SANDBOX,
                MerchantId = "2394bzj6xknd5fm5",
                PublicKey = "664gtkzv2mngtsb9",
                PrivateKey = "19fa709f9dfb34766bbdcd5eb51f1ae5"
            };
            var clientToken = gateway.ClientToken.Generate();

            // Console.WriteLine(clientToken);
            return clientToken;

In VFP I ran this:

do wwdotnetbridge.prg
LOCAL loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4")
loBridge.LoadAssembly("C:\Data\GRAYbase\VSCode\Braintree_vsc\bin\Debug\netcoreapp3.0\braintree_vsc.dll")
logateway = loBridge.CreateInstance("?")

But I'm not sure what to pass into the CreateInstance() call.

NOTE: I did try accessing the Braintree.dll directly with wwdotnetbrige, but ran into trouble setting this: Environment = Braintree.Environment.SANDBOX,

For reference, this is the direct code:

loBridge = CreateObject("wwDotNetBridge","V4")
loBridge.LoadAssembly("braintree.dll")
logateway = loBridge.CreateInstance("Braintree.BraintreeGateway")

This worked, I could set properties, well all except the "Environment" value. But I read here that it would be better to build your own and call that.

So any advice on either the direct route and how to set the 'Environment' variable in the VSC example or what to pass to the createinstance in the selfmade version would be appreciated.

Doug

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Rick Strahl
  Doug
  Oct 24, 2019 @ 09:39pm

If you can create a .NET wrapper assembly that'll probably be easier. Create a .NET method that handles the processing and pass in a message object that you create that has all the values you need for processing.

Failing that you can access that enum value using:

lnEnvironment = loBridge.GetEnumValue("BrainTree.Environment.SANDBOX")

Here's documentation:

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Doug
  Rick Strahl
  Oct 25, 2019 @ 02:25pm

Yeah, the .net code method is already done (first code block above). But from the VFP side when calling it using wwdotnetbridge, I'm getting a NULL value back on the .CreateInstance() call (```foxpro logateway = loBridge.CreateInstance("Braintree_vsc.Program")

I'm passing the Namespace.class ("Braintree_vsc.Program").
Why am I getting a null back?  

Thanks,
Doug
Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Doug
  Rick Strahl
  Oct 29, 2019 @ 09:22am

This is also returning a null

lnEnvironment = loBridge.GetEnumValue("BrainTree.Environment.SANDBOX")
Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Rick Strahl
  Doug
  Oct 29, 2019 @ 05:45pm

Look at the error message in .cErrorMsg.

Most likely your assembly isn't loaded or you're not fully qualifying the type (ie. wrong namespace.classname). Make sure you call .LoadAssembly() before instantiating.

Check errors along the way for all the intermediate calls.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Doug
  Rick Strahl
  Nov 1, 2019 @ 12:46am

I've got the "direct" method working. But you mentioned it would be easier to create a .net wrapper and interact with that instead. I've done that, kinda, I created a console app, but that only returns int or 0.

Is there some documentation on how to create a .net wrapper, with methods that wwdotnetbridge could call from vfp?

Thanks, Doug

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Doug
  Doug
  Nov 1, 2019 @ 12:56am

Regarding the last post, would I create a class library with the embedded methods and build?

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Rick Strahl
  Doug
  Nov 1, 2019 @ 11:54am

Yes.

  • Create a class library
  • Build the .dll into your project's folder (so you don't have an external reference you have to later copy)
  • Call loBridge.LoadLibrary("yourDll.dll")
  • Create a type from the library with CreateInstance()
  • Make method calls, prop access etc.

Same as what you're already doing with the BrainTree interface.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Doug
  Rick Strahl
  Nov 1, 2019 @ 01:12pm

Yes, after having the class lib thought, I ran through it and it works just fine. I think this approach (wrapper) will save type conversion issues.

wwdotnetbridge and this approach will solve a lot of issues and create a lot of options.

Thanks, Doug

Gravatar is a globally recognized avatar based on your email address. re: Braintree integration via wwdotnetbridge
  Rick Strahl
  Doug
  Nov 1, 2019 @ 01:32pm

Yes this is the best way to build anything that has more than a few lines of .NET code that you need to execute.

Once you have that one external .NET assembly you can add anything you do with .NET to that - it's just much easier to write the .NET code in .NET with full support for Intellisense and all the language features available to you, and just have one small interface point that you call from FoxPro.

+++ Rick ---

© 1996-2019