Using HTTPSecureSocket within HandleSpecialURL

Hi,

I’m trying to implement a stripe payment system. I successfully implemented a stripe custom form using stripe elements which captures the card information and returns the token back to my application through the HandleSpecialURL method. But I’m now having trouble getting the second part to work. Within HandleSpecialURL, I need to make a REST call using HTTPSecureSocket back to Stripe to create the charge using the token. But I can’t get any results. Even though I added event handlers for PageReceived and AuthenticationRequired, neither gets called because I think the HandleSpecialURL is ending the session before a response comes back from the secure socket call. How do I pick up the response from the Secure Socket request?

Here’s my code withing the HandleSpecialURL method,…

// Grab the key value pairs from the url string
Dim varsTemp() as String = Split(Request.Entity,"&")
dim vars as new dictionary
For i as Integer = 0 to ubound(varsTemp)
dim key as string = nthField(varsTemp(i),"=",1)
dim value as string = nthField(varsTemp(i),"=",2)
vars.value(key) = value
Next i

// Pull out token and session from url – THIS PART WORKS!
Dim stripeKey as string = “pk_test_g6do5S237ekq10r65BnxO6S0” // hard coding the test key for now
Dim stripeToken as string = vars.value(“stripeToken”)
Dim SessionID as string = vars.value(“session”)
Dim sessionContext As New WebSessionContext(Self.SessionWithIdentifier(sessionID))

// Try submitting the payment request – THIS PART DOES NOT WORK
request.print(“Trying payment request…”)

Dim req As String
Dim form As Dictionary
Dim HTTPS1 as HTTPSecureSocket = new HTTPSecureSocket

req = “https://api.stripe.com/v1/charges
form = New Dictionary
form.Value(“amount”) = “400”
form.Value(“currency”) = “usd”
form.Value(“card”) = stripeToken
form.Value(“description”) = “Charge for test@example.com

// Set up handlers – THESE NEVER SEEM TO GET CALLED
AddHandler HTTPS1.PageReceived, AddressOf HandlePageRecieved
AddHandler HTTPS1.AuthenticationRequired, AddressOf HandleAuthReq

HTTPS1.SetFormData(form)
HTTPS1.Secure = True
HTTPS1.SetRequestHeader(“Authorization”, “pk_test_g6do5S237ekq10r65BnxO6S0”)
HTTPS1.ConnectionType = SSLSocket.SSLv23
HTTPS1.Post(req)

return true // I believe the session is ending here, so the handlers never get triggered

First of all, there’s no “Session” at this point HandleSpecialUrl and HandleUrl are just raw http requests.

That said, what’s happening is that HTTPS1 is going “out of scope” at the end of the event which is probably what’s causing your problem.

To solve this, you’ll need to put the instance into an array somewhere so you can hold onto the reference until PageReceived or Error event is fired. Once this happens, you’ll need to remove the handlers and remove the instance from the array.

Thanks Greg. I’ll try that.

BTW, you are correct that by itself HandleSpecialURL does not have a session.

But I’m running the Stripe custom form html/javascript code within the htmlviewer of a valid Xojo session. When the Strip form is submitted, I pass the session id and pick it up in HandleSpecialURL. You can then access the session properties by calling Dim sessionContext As New WebSessionContext(Self.SessionWithIdentifier(sessionID)). This works perfectly.

Hmm, maybe I can use the session to store the reference to the HTTPS1 instance.

Hi All, I got it to work! It’s a little complicated to explain. But basically, I put the HTTPS variable. along with the handlers, in the instance of the original webpage and added a method to handle the payment request. I also put a reference to the page in the session. So, when HandleSpecialURL receives the Token, it pulls the session as I describe above, and calls the method in the webpage to handle the payment request.

If anyone wants more details please let me know.

Now, because you’ve added a reference to the webpage on the Session, don’t forget to set that to Nil when you’re done with it or it won’t get destroyed when it’s closed.