Skip to content

Instantly share code, notes, and snippets.

View jphungcode's full-sized avatar
🎯
Focusing

John Phung jphungcode

🎯
Focusing
View GitHub Profile
@jphungcode
jphungcode / swift-global-notifications.swift
Created June 2, 2022 10:17
Swift global notifications
// AppDelegate.swift
// subscription listener subscribing to "some-event-name" posted by Electron or other app
func observer() {
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), Unmanaged.passRetained(self).toOpaque(), observerCallback, "some-event-name" as CFString , nil, .deliverImmediately)
}
// the listener callback - do something when you receive "some-event-name" event.
let observerCallback: CFNotificationCallback = { center , observer, name, object, info in
// perform check on incoming data to see if it exists as a dictionary (object sent from javascript)
@jphungcode
jphungcode / electron-notifications.js
Last active June 2, 2022 10:23
electron notifications
// main.js
import {systemPreferences} from "electron"
app.whenReady().then(async () => {
systemPreferences.subscribeNotification(
"subscribeEvent",
(event, userInfo) => {
console.log(userInfo);
exports.wooComPushToPFC = functions.https.onRequest((req, res) => {
const method = req.method;
const headers = req.headers;
const data = req.body;
if (method !== "POST") {
res.status(501).send("Incorrect req method");
}
const secret = functions.config().woocommerce.secret; //secret phrase set in woocommerce
const fetchConfig = {
method: "POST",
headers: {
Authorization: "Bearer " + functions.config().pfc.apikey, //api key is stored as a env variable
"Content-Type": "application/json",
},
body: pfcData,
};
fetch(pfcEndpoint, fetchConfig)
.then((response) => response.json())
const pfcData = JSON.stringify({
Type: 2, // 1 fulfillment, 2 - ordinary
CustomerId: "107085", // 0 account ID for pfc
ChannelId: channelId,
Sender: "huangjia",
SendPhone: "0755–83727415",
SendAddress:
"4th Floor, Building C, №2, Road 1,Shangxue Science and Technology Park,Bantian street, Longgang District, Shenzhen, Guangdong Province, China",
SendCompany: "www.parcelfromchina.com",
SendEmail: "sales@pfcexpress.com",
const pfcEndpoint = "http://www.pfcexpress.com/webservice/v2/CreateShipment.aspx";
let lineItems = [];
// creating an update line items array for the necessary object attributes for PFC
for (let i = 0; i <= data.line_items.length - 1; i++) {
let item = data.line_items[i];
let temp_item = {
SKU: item.sku,
EnName: item.name,
CnName: "",
MaterialQuantity: item.quantity,
@jphungcode
jphungcode / post_pfc_express_1.js
Created October 21, 2020 02:55
Start of function to post to pfc express endpoint
const pushOrder = (data) => {
return new Promise((resolve, reject) => {
if (data.status !== "processing") {
console.log("order not completed"); // we only want to push data when the order transitions to processing (paid)
reject("Order not completed");
}
// construct object for PFC
// POST data to PFC endpoint
}
}
@jphungcode
jphungcode / woocommerce_webhook_payload.json
Created October 21, 2020 02:54
Woocommerce webhook payload data
"id": 52,
"parent_id": 0,
"status": "pending",
"order_key": "order_5836d85314887",
"number": 52,
"currency": "EUR",
"version": "2.6.8",
"prices_include_tax": false,
"date_created": "2016–11–24T12:08:00",
"date_modified": "2016–11–24T12:08:51",
@jphungcode
jphungcode / woocommerce_request.js
Last active October 21, 2020 02:53
Woocommerce integration with PFC express
const crypto = require("crypto");
const fetch = require("isomorphic-fetch");
exports.wooComPushToPFC = functions.https.onRequest((req, res) => {
const method = req.method;
const headers = req.headers;
const data = req.body;
if (method !== "POST") {
res.status(501).send("Incorrect req method");
}
@jphungcode
jphungcode / order_template.js
Last active October 21, 2020 02:44
1_wooCommercePFCIntegration
{
"Type":2, // 1 fulfilment, 2 for ordinary order
"CustomerId":"107085", // your account ID - this is mandatory
"ChannelId":158, // This is mandatory and refers to the base channel ID for shipping providers for a certain country. You can find the channelId from http://www.pfcexpress.com/webservice/APIWebService.asmx/getChannel. You will need to filter based on country code to determine what channelId to use.
"INorOut":0, // 0 for exports - most common
"IDCardNo":"",
"Remark":"",
"Products":[
{
"Price":64,