Coupon Code

I wonder if this code is right or not

import wixPay from ‘wix-pay’;
import { createPaymentForProduct1 } from ‘backend/EK_PayAPI’;
import { createPaymentForProduct2 } from ‘backend/EKdisccount_PayAPI’;

let couponCode = $w(‘#input7’).value;

export function button1_click(event) {
if (couponCode = “FCES1ST”) {
createPaymentForProduct2().then(payment => {
wixPay.startPayment(payment.id, { “termsAndConditionsLink”: “https://ek4art.wixsite.com/ekart/terms-and-conditions” });
});
}else if (couponCode = " "){
createPaymentForProduct1().then(payment => {
wixPay.startPayment(payment.id, { “termsAndConditionsLink”: “https://ek4art.wixsite.com/ekart/terms-and-conditions” });
});
}
}

Because the (couponCode = “FCES1ST”) and (couponCode = " ") are give me error

Looking at your previous post for this, so I could get the backend files info.
https://www.wix.com/corvid/forum/community-discussion/coupon-code-help

Instead of having to simply use the backend files from the tutorial used.
https://www.wix.com/corvid/forum/community-discussion/coupon-code-help

Try this code below.

EK_payAPI.jsw code

import wixPay from 'wix-pay-backend';

export function createPaymentForProduct1() {
return wixPay.createPayment( {
amount: 120,
items: [{name: 'Portrait With Frame', price: 120}],
} );
}

EKdiscount_payAPI.jsw code

import wixPay from 'wix-pay-backend';

export function createPaymentForProduct2() {
return wixPay.createPayment({
amount: 60,
items: [{ name: 'Portrait With Frame', price: 60 }],
});
}

Page Code

import wixPay from 'wix-pay';
import {createPaymentForProduct1} from 'backend/EK_PayAPI';
import {createPaymentForProduct2} from 'backend/EKdiscount_PayAPI';

let couponCode = 'FCES1ST';

$w.onReady(function () {
});

export function button1_click(event) {
if ($w('#input7').value === couponCode) {
createPaymentForProduct2().then(payment => {
wixPay.startPayment(payment.id, { "termsAndConditionsLink": "https://ek4art.wixsite.com/ekart/terms-and-conditions" });
});
}

if ($w('#input7').value === "") {
createPaymentForProduct1().then(payment => {
wixPay.startPayment(payment.id, { "termsAndConditionsLink": "https://ek4art.wixsite.com/ekart/terms-and-conditions" });
});
}
}

Please note that I have made two small changes in the spellings for portrait and discount in the backend jsw files.

Also note that this was just done on a free test site and no payment providers could be connected to test if it all fully works, however there are no errors.

You will have to test it on your end to see if it fully works with your payment already setup etc.

Finally, just checking, do you have the coupon code setup in the coupon section in the Wix Dashboard, as if you did and you used the coupon code input in the checkout, then you would not need the second payment option as the coupon would deduct it automatically for you.

Okay thanks but there is another question can I but more than two payment in the EKdiscount_APL.jws

@eyadkhfarah
There is noting stopping you adding more than the createPaymentForProduct2 in the backend file.

You will just need to create a new name for each export, say createPaymentForProduct3. createPaymentForProduct4. createPaymentForProduct5 etc and make sure that you import those too.

So something like this in your backend file.

import wixPay from 'wix-pay-backend';

export function createPaymentForProduct2() {
return wixPay.createPayment({
amount: 60,
items: [{ name: 'Portrait With Frame', price: 60 }],
});
}

export function createPaymentForProduct3() {
return wixPay.createPayment({
amount: 60,
items: [{ name: 'Portrait Without Frame', price: 60 }],
});
}

export function createPaymentForProduct4() {
return wixPay.createPayment({
amount: 60,
items: [{ name: 'Portrait With Glass', price: 60 }],
});
}

export function createPaymentForProduct5() {
return wixPay.createPayment({
amount: 60,
items: [{ name: 'Portrait Without Glass', price: 60 }],
});
}

With this on your page code.

import wixPay from 'wix-pay';
import {createPaymentForProduct1} from 'backend/EK_PayAPI';
import {createPaymentForProduct2} from 'backend/EKdiscount_PayAPI';
import {createPaymentForProduct3} from 'backend/EKdiscount_PayAPI';
import {createPaymentForProduct4} from 'backend/EKdiscount_PayAPI';
import {createPaymentForProduct5} from 'backend/EKdiscount_PayAPI';

Assuming that you have read all about Wix Pay API and Wix Pay Backend API.
https://www.wix.com/corvid/reference/wix-pay.html
https://www.wix.com/corvid/reference/wix-pay-backend.html

You can do multiple products in a static data option through the backend file.
https://support.wix.com/en/article/corvid-tutorial-processing-payments
https://support.wix.com/en/article/corvid-tutorial-processing-payments#payment-code-example

Also, if you are looking at using more, you might want to look at the dataset example.
https://support.wix.com/en/article/corvid-tutorial-using-the-corvid-pay-api-to-collect-payments-for-products-in-a-database-collection