Hi Team,
Payment is not working in production environment but it is working good in staging/sandbox environment.
Application Flow: we created 4 plans and allowing our application user to select among the plan and then we are taking the cards details and processing payment.
below code flow on the same.
async createCustomerPaymentSubscription(cardNonce, selectedPlan, agentId, amount) {
try {
const idempotency_key = crypto.randomBytes(22).toString("hex");
const customerRes = await axios.post(
`${process.env.SQUARE_URL}/v2/customers`,
{
email_address: process.env.SQUARE_EMAIL
},
this.headers
);
const customerId: string = customerRes?.data?.customer?.id;
const squareClient = new Client({
environment: Environment.Production, // Change to square.Environment.Production for testing for prod Production square.Environment.Production
accessToken: process.env.SQUARE_ACCESS_TOKEN
});
// get selected plan
const plan = await this.getListSubscriptionPlan();
const activePlan = plan?.find((item) => item?.subscription_plan_data?.name === selectedPlan && item?.present_at_all_locations);
console.log('activePlanactivePlan', activePlan);
console.log('Subscription Plan', JSON.stringify(activePlan?.subscription_plan_data?.subscription_plan_variations?.[0]?.subscription_plan_variation_data?.phases?.[0]?.pricing?.price_money?.amount));
// Create a customer card to charge
const cardResult: any = await squareClient.customersApi.createCustomerCard(customerId, {
cardNonce: cardNonce
});
console.log('cardResultcardResult',cardResult);
const cardId = cardResult?.result?.card?.id;
console.log('cardIdcardIdcardId',cardId);
// get exiting subscription
const existSubscription = await this.paymentSubscriptionService.getSubscription(agentId);
let currDate: any = new Date();
currDate = moment(currDate, "YYYY-MM-DD").format("YYYY-MM-DD");
const existPlanCanceledDate = moment(existSubscription.canceledDate, "YYYY-MM-DD").format("YYYY-MM-DD");
let subscriptionStartDate = moment().format("YYYY-MM-DD");
let count = 0;
if (activePlan?.subscription_plan_data?.subscription_plan_variations?.[0]?.subscription_plan_variation_data?.phases?.[0]?.pricing?.price_money?.amount <= existSubscription?.price && existPlanCanceledDate > currDate) {
count = Math.abs(moment(currDate, "YYYY-MM-DD").startOf("day").diff(moment(existPlanCanceledDate, "YYYY-MM-DD").startOf("day"), "days")) + 1;
}
if (count > 0) {
subscriptionStartDate = moment(subscriptionStartDate).add(count, "days").format("YYYY-MM-DD");
}
const subscription: any = {
idempotencyKey: idempotency_key,
locationId: process.env.LOCATION_ID,
customerId: customerId,
planId: activePlan?.id ?? "planId",
cardId: cardId,
startDate: subscriptionStartDate,
timezone: "America/Los_Angeles",
source: {
name: "Navihome"
}
};
//cancel exist Subscription
if (existSubscription?.subscriptionId && !existSubscription?.subscriptionId.includes("free")) {
const { result } = await squareClient.subscriptionsApi.cancelSubscription(existSubscription.subscriptionId);
}
// Create the subscription
const { result }: any = await squareClient.subscriptionsApi.createSubscription(subscription);
const subscriptionData = {
agentId: agentId,
subscriptionId: result?.subscription?.id,
planId: result?.subscription?.planId,
status: count > 0 ? "PENDING" : result?.subscription?.status,
startDate: result?.subscription?.startDate,
canceledDate: moment(result?.subscription?.startDate).add(30, "days").format("YYYY-MM-DD"),
price: activePlan?.subscription_plan_data?.subscription_plan_variations?.[0]?.subscription_plan_variation_data?.phases?.[0]?.pricing?.price_money?.amount,
planName: activePlan?.subscription_plan_data?.name,
activePlan: count > 0 ? existSubscription?.planName : activePlan?.subscription_plan_data?.name
};
await this.paymentSubscriptionService.insertSubsciption(subscriptionData);
return JSON.parse(JSON.stringify(result.subscription, (key, value) => (typeof value === "bigint" ? value.toString() : value)));
} catch (error) {
console.log("ERROR--", error);
return {
error: true,
message: "Payment Failed",
category: 'AUTHENTICATION_ERROR',
code: 'UNAUTHORIZED',
detail: 'This request could not be authorized.'
}
}
}
in Production setup, i am not able to create a customer card line.
kindly request your help to solve this case.
4 posts - 3 participants