条带化付款集成到wix网站

时间:2019-11-28 06:00:56

标签: stripe-payments wixcode react-native-stripe-api

我不是编码员,我是编码的新手,我想在我的wix网站中添加条带化支付集成,所以我在youtube上出现了一些视频,我决定自己做,我将条带化支付集成到wix中,但是在测试时我正在获取费用ID-未定义错误

在下面,我提供了我的主页代码,公共/前端代码和后端代码,因此请仔细查看并告知任何帮助,谢谢

主页代码-

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);  -THIS IS CHARGE ID ERROR I AM GETTING 
});
});
}
function createCard() {

    return {

        "name": $w("#cname").value,
        "nuumber": $w("#cnumber").value,
        "exp_month": $w("#month").value,
        "exp_year": $w("#year").value,
        "cvc": $w("#cvc").value,
};

}

function chnageState() {

payment = {
    "amount": ($w("#amt").value*100),
    "currency":"INR",}
}
export function chargeCard_click(event) {
    payNow(); }

前端/公共代码-

import {fetch} from 'wix-fetch';
export async function createToken(card) {
//Go to stripe.com to create a test key and replace the one in the example
const apiKey = "YOUR_API_HERE";
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);
}

后端代码-

import {fetch} from 'wix-fetch';
export async function charge(token,payment) {
const cart = payment;
//Go to stripe.com to create a test key and replace th eone in the example
const apiKey = "YOUR_API_HERE";
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;
}

0 个答案:

没有答案