Hey,
Im trying to progamatically add an automatic discount to Square that includes multiple options (customer group and product category) where have I gone wrong?
'import { axios } from “@pipedream/platform”;
export default defineComponent({
props: {
square: {
type: “app”,
app: “square”,
},
discountName: {
type: “string”,
label: “Discount Name”,
description: “The name of the discount to create”,
default: “Active Member”,
},
discountPercentage: {
type: “integer”,
label: “Discount Percentage”,
description: “The percentage of the discount”,
default: 5,
},
categoryId: {
type: “string”,
label: “Category ID”,
description: “The ID of the category to which the discount should apply”,
},
customerGroupId: {
type: “string”,
label: “Customer Group ID”,
description: “The ID of the customer group to apply the discount to”,
},
},
async run({ steps, $ }) {
// Generate a unique idempotency key to prevent duplicate requests
const idempotencyKey = unique-id-${Date.now()}
;
// Prepare the catalog objects for upsert
const discountObject = {
id: "#Discount",
type: "DISCOUNT",
discount_data: {
name: this.discountName,
discount_type: "FIXED_PERCENTAGE",
percentage: this.discountPercentage.toString(),
},
present_at_all_locations: true,
};
const productSetObject = {
id: "#ProductSet",
type: "PRODUCT_SET",
product_set_data: {
name: "Specific Category Products",
category_ids_any: [this.categoryId],
},
present_at_all_locations: true,
};
const pricingRuleObject = {
id: "#PricingRule",
type: "PRICING_RULE",
pricing_rule_data: {
name: `${this.discountName} Rule`,
discount_id: "#Discount",
match_products_id: "#ProductSet",
customer_group_ids_any: [this.customerGroupId],
application_mode: "AUTOMATIC",
},
present_at_all_locations: true,
};
// Prepare the batch upsert data
const batchUpsertData = {
idempotency_key: idempotencyKey,
batches: [
{
objects: [discountObject, productSetObject, pricingRuleObject],
},
],
};
try {
// Make the BatchUpsertCatalogObjects API call
const response = await axios($, {
method: "POST",
url: `https://connect.squareup.com/v2/catalog/batch-upsert`,
headers: {
Authorization: `Bearer ${this.square.$auth.oauth_access_token}`,
"Content-Type": "application/json",
"Square-Version": "2023-10-18",
},
data: batchUpsertData,
});
// Log the response for debugging
console.log("Batch Upsert Response:", response.data);
return response.data;
} catch (error) {
// Log detailed error information
console.error("Error creating automatic discount:", error.response?.data || error.message);
throw new Error("Failed to create automatic discount.");
}
},
});’