Stripe (On Successful Payment)

Hello,

So I integrated Stripe with Wix Code and I am getting a Charge ID on my Developers Console.


I have a Question.

If I get a successful Payment and a Charge ID is generated is there any way to set a condition that will retrieve the user information such as Booking Details, Name & Email stored with wix-storage (which were entered in the previous page & are entered in Hidden Input Boxes on this page) and generate an email with sendFormData . On the alternative, if Stripe returns a unsuccessful payment, is there a way to notify on screen & Not Send any Email.

Because right now if I test it on the Live site, there is no way to find out if Payment was successful or not.

Is something like onSuccess or something possible ?

anyone ?

Disclaimer: I have zero coding knowledge, so I’m guilty of coming up with messy work-arounds for most of the things I need . . .
That said, I have not been able to figure out how to prompt an ‘on-screen’ message for unsuccessful payments. However, what I’ve done instead, is added a ‘receipt_email’ parameter to the page code so that users receive an emailed receipt from Stripe when their payment is successful.
I use SendGrid for the SendFormData email that is generated when users submit the payment form. The user is advised within that email that they will receive a Stripe receipt when the payment has been authenticated. You could attach the hidden fields as elements to be included in a similar email to your users if you wanted to take that route.

Hey, Thanks so much for your advice :slight_smile:

While it can work for most, It will not be accepted in a Corporate Environment. My boss will never agree to send our clients an Email like

The user is advised within that email that they will receive a Stripe receipt when the payment has been authenticated.
I have to show it on screen whether the payment was successful or not… and if successful then the client needs to be redirected to a Order Summary Page

I really hope FutureCode comes out with his Part 2 soon

Anyone Can Help ?

Hi,

Wix is currently working on a new wix code based feature named wix-pay which will help you integrate Wix Cashier to your wixCode site.

please share more context on the issue you are trying to solve, the details are too much stripe specific which i am not sure really matter to the question.
you are executing a code in the site page in the browser which will invoke a payment with stripe using ajax and i assume backend code as well.
if the payment was successful (is the result done synch or asynch?) you want to either send an email or present a message in the browser to the user?

Shlomi

if the payment was successful you want to either send an email or present a message in the browser to the user?
Yes Shlomi. This is exactly what i want to do. I would prefer showing a message or payment summary.
Below I am sharing with you my codes.

PAGE CODE

//paymentAPI

import {createToken, encodeCard}  from "public/stripeAPI.js";
import {charge}  from 'backend/stripeProxy';

var payment;

export function payNow(event) {
    createToken(encodeCard(createCard()))
        .then((token) => {
            console.log("Card token: " + token);
            charge(token, payment)
                .then((chargeResponse) => {
                    console.log("Charge ID: " + chargeResponse.id);
                });
        });
}

function createCard() {
    return {
        "name": $w("#input1").value,
        "number": $w("#input4").value,
        "cvc": $w("#input5").value,
        "exp_year": $w("#input3").value,
        "exp_month": $w("#input2").value
    };
}

function changeState() {
    payment = {
        "amount": ($w("#input8").value * 100),
        "currency": "EUR",
        "description": $w("#input9").value
    };
}

export function button1_click(event, $w) {
payNow();
}

export function input8_change(event, $w) {
	//Add your code for this event here: 
changeState();	
}

export function input9_change(event, $w) {
	//Add your code for this event here: 
changeState();
}

BACKEND FILE

//stripeProxy.jsw

import {fetch} from 'wix-fetch';

export async function charge(token,payment) {
    
  const cart = payment;
    
 
  const apiKey = "sk_xxxxxxxxxxxxxxxxxxxx"; 

  const response = await fetch("https://api.stripe.com/v1/charges", {
    method: 'post',
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Bearer " + apiKey
    },
    body: encodeBody(token, cart)
  });

  if (response.status >= 200 && response.status < 300) {
    return await response.json();
  }
  return await response.text();
}

function encodeBody(token, cart){
  let encoded = "";
        
  for (let [k, v] of Object.entries(cart)) {
    encoded = encoded.concat(k,"=", encodeURI(v), "&"); 
  }
  encoded = encoded.concat("source=", encodeURI(token));
  return encoded;
}

PUBLIC

//stripeAPI.js

import {fetch} from 'wix-fetch';

export async function createToken(card) {
    
  
  const apiKey = "pk_test_xxxxxxxxxxxxxxx"; 

  const response = await fetch("https://api.stripe.com/v1/tokens", {
    method: 'post',
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Bearer " + apiKey
    },
    body: card
  });

  if (response.status >= 200 && response.status < 300) {
    const json = await response.json()
    return json.id;
  }
  const responseText = await response.text();
  console.log(responseText);
  return response.status;
}

export function encodeCard(card){
  let encoded = "";
    
  for (let [k, v] of Object.entries(card)) {
    encoded = encoded.concat("card[", k, "]=", encodeURI(v), "&"); 
  }
    
  return encoded.substr(0, encoded.length - 1);
}

Thank you for taking out time to help me

Hi,

there seems to be something basic i am missing in your question -
in this part of the code -

.then((chargeResponse) => {                     
  console.log("Charge ID: " + chargeResponse.id); });

you can code the behavior, either call a backend function to send an email in case of success or redirect to failure page or show a message in case of failure.
what specifically are you trying to achieve? display the message on screen? you mean prepare a hidden text message in advance, empty, then inject the correct message and make the text visible?
now i am not sure i understand what you are asking specifically :slight_smile:

Shlomi

Sorry for the poor explanation Shlomi.

If you could guide me on how to do this:

  • Show a Failure Message on the same page in the event of a failed transaction (On the same page because if the payment fails, I would like the user to be able to try the payment again)

  • Redirect to a Success Page on the Event of a Successful Transaction (Where I can display their Order Details stored with wix {session} wix-storage, something like a order summary page you see in e-commerce websites after you purchase something)
    I apologize for the poor explanation, I am trying to learn javascript as fast as I can but its a vast topic.

Thank you for taking out time to help me Shlomi.

Hi,

thanks for the clarifications, the stripe integration was a bit confusing, what you basically want is configuring the text in the style you choose, locate it in the page and configure it as hidden on load.
then dynamically change the text context and show it when needed.
please see an example of page content manipulation here:

redirect can be done using the following wix-location-frontend - Velo API Reference - Wix.com

good luck!
Shlomi

Shantanu Kumar, were you able to resolve this ? if yes can you please share how you did that. thanks Raj

Hey Rajesh,

No I could not.