import AsyncStorage from '@react-native-community/async-storage' import Base64 from './b64' import MOBILEDB from '../../components/api/endpoints' import DB from '../../components/storage' const env = "SANDBOX" // const env = "PRODUCTION" const sandbox_api_url = "https://pg-sandbox.paymaya.com" const prod_api_url = "https://pg.paymaya.com" const api_url = env == "PRODUCTION" ? prod_api_url : sandbox_api_url const prod_api_key = "pk-gEKuO5fHV3GIUQb0hTh7YChBCMJOgDLRKbw96umP14X:" const prod_secret_key = "sk-wnNgSRT0pPPfFOBto3L2jkNDm77lpaUO00SFVmpuzvW:" // const sandbox_api_key = "pk-pvlwGPQflkjv1J8qZEyUVYW74EPgUf8YQxPgkGl9l9n:" // const sandbox_api_key = "pk-Z0OSzLvIcOI2UIvDhdTGVVfRSSeiGStnceqwUE7n0Ah:" //old // const sandbox_secret_key = "sk-X8qolYjy62kIzEbr0QRK1h4b4KDVHaNcwMYk39jInSl:" //old const sandbox_api_key = "pk-J3AtaSkYMd8MUM5lo9df7Inh6HlITq7W7Q52zBMp8X9:" //new const sandbox_secret_key = "sk-ewLwiZgYZlfp89g0UOwgJQr0bpvqvEOxWDOUhYbByqS:" //new const checkout_api_key = "sk-VoMzbvf3EKPJTZvR4pz9U9wVoI5cqb8YItNT92e1fe4:" //PUBLIC TOKENS let b64 = env == "PRODUCTION" ? Base64.btoa(prod_api_key) : Base64.btoa(sandbox_api_key) let token = "Basic " + b64.substr(0, b64.length-1) + "=" //SECRET TOKENS let sb64 = env == "PRODUCTION" ? Base64.btoa(prod_secret_key) : Base64.btoa(sandbox_secret_key) let stoken = "Basic " + sb64.substr(0, sb64.length-1) + "=" //CHECKOUT TOKEN let csb64 = Base64.btoa(checkout_api_key) let cstoken = "Basic " + csb64.substr(0, csb64.length-1) + "=" let UNIOIL_LOGO = "https://image.winudf.com/v2/image1/Y29tLnByb2plY3QueW9uZHUuYXBwdW5pb2lsbG95YWx0eV9pY29uXzE1NzkyMDAyNDFfMDI3/icon.png?w=170&fakeurl=1"; let UNIOIL_DISPLAY_NAME = "UNIOIL PETROLEUM PHIL"; let UNIOIL_COLOR_SCHEME = "#E74610"; const Service = async (url, method, headers, data, isNewCustomization = false) => { let Options = method == 'POST' ? { method: method, body: JSON.stringify(data), headers: headers.isSecret ? { 'Content-Type': 'application/json', 'Authorization': stoken } : isNewCustomization ? { 'Content-Type': 'application/json', 'Authorization': cstoken } : { 'Content-Type': 'application/json', 'Authorization': token } } : { method: method, headers: headers.isSecret ? { 'Content-Type': 'application/json', 'Authorization': stoken } : { 'Content-Type': 'application/json', 'Authorization': token } } console.log(url) return fetch(url, Options) .then((response) => response.json()) .then((json) => { return json }) .catch((error) => { console.log(json) return error }); } const setDefaultCustomization = async () => { let result = await Service(`${api_url}/checkout/v1/customizations`, "POST", {isSecret: true}, { logoUrl: UNIOIL_LOGO, iconUrl: UNIOIL_LOGO, appleTouchIconUrl: UNIOIL_LOGO, customTitle: UNIOIL_DISPLAY_NAME, colorScheme: UNIOIL_COLOR_SCHEME, showMerchantName: true, hideRecieptInput: false, redirectTimer: 30, skipResultPage: false }) return result } const setHiddenCustomization = async () => { let result = await Service(`${api_url}/checkout/v1/customizations`, "POST", {isSecret: true}, { logoUrl: UNIOIL_LOGO, iconUrl: UNIOIL_LOGO, appleTouchIconUrl: UNIOIL_LOGO, customTitle: UNIOIL_DISPLAY_NAME, colorScheme: UNIOIL_COLOR_SCHEME, redirectTimer: 3, skipResultPage: true }) return result } const setNewDefaultCustomization = async () => { let result = await Service(`${api_url}/checkout/v1/customizations`, "POST", {isSecret: true}, { logoUrl: UNIOIL_LOGO, iconUrl: UNIOIL_LOGO, appleTouchIconUrl: UNIOIL_LOGO, customTitle: UNIOIL_DISPLAY_NAME, colorScheme: UNIOIL_COLOR_SCHEME, showMerchantName: true, hideRecieptInput: false, redirectTimer: 30, skipResultPage: false }, true) return result } const CreateCustomerPayments = async (data) => { let result = await Service(`${api_url}/payments/v1/payments`, "POST", {isSecret: true}, data) return result } const CreateCustomer = async (data) => { let result = await Service(`${api_url}/payments/v1/customers`, "POST", {isSecret: true}, data) return result } const CreateCardToken = async (data) => { let result = await Service(`${api_url}/payments/v1/payment-tokens`, "POST", {isSecret: false}, data) return result } const GetCustomerTransactionDetails = async (paymentId) => { let result = await Service(`${api_url}/payments/v1/payments/${paymentId}`, "GET", {isSecret: true}, {}) return result } const GetCustomerDetails = async () => { let url = api_url + `/payments/v1/customers/CUSTOMER_ID` } const GetCustomerCards = async (id) => { let result = await Service(`${api_url}/payments/v1/customers/${id}/cards`, 'GET', {isSecret: true}, {}) return result } const PerformLink = async (customerId, token) => { let result = await Service(`${api_url}/payments/v1/customers/${customerId}/cards`, "POST", {isSecret: true}, { paymentTokenId: token, isDefault: true, redirectUrl: {success: 'unioil.com/success', failure: 'unioil.com/failure', cancel: 'unioil.com/cancel'} }) return result } const DeleteCard = async (id, token) => { let result = await Service(`${api_url}/payments/v1/customers/${id}/cards/${token}`, 'DELETE', {isSecret: true}, null) return result } const Payout = async (customerId, token, amount) => { let result = await Service(`${api_url}/payments/v1/customers/${customerId}/cards/${token}/payments`, "POST", {isSecret: true}, { totalAmount: {amount: amount, currency: "PHP"}, redirectUrl: {success: 'unioil.com/success', failure: 'unioil.com/failure', cancel: 'unioil.com/cancel'} }) return result } const NewPayout = async (token, amount) => { let clientDetails = { paymentTokenId: token, totalAmount: { amount: amount, currency: "PHP" }, redirectUrl: { success: "unioil.com/success", failure: "unioil.com/failure", cancel: "unioil.com/cancel" } } let payments = await CreateCustomerPayments(clientDetails) payments.status = (payments?.parameters != undefined && payments?.parameters.length > 0) ? 'failed' : 'success' return payments } const Register = async (client) => { let customize = await setDefaultCustomization() let newCustomer = await CreateCustomer(client) let newCardToken = await CreateCardToken(client.card) console.log("CUSTOMER CREATION", newCustomer); console.log("CARD TOKEN CREATION", newCardToken); if(newCustomer.code) { return { status: 'failed', merchant: newCustomer } } if(newCardToken.state == "AVAILABLE") { let newRegisteredCustomer = await PerformLink(newCustomer.id, newCardToken.paymentTokenId) console.log("LINK RESULT", newRegisteredCustomer) if(newRegisteredCustomer) { return { status: 'success', merchant: newCustomer, card: newCardToken, link: newRegisteredCustomer } } else { return { status: 'failed', merchant: newCustomer, card: newCardToken, link: newRegisteredCustomer } } } else { return { status: 'failed', merchant: newCustomer, card: newCardToken } } } const AddCard = async (card, id) => { let customize = await setHiddenCustomization() let newCardToken = await CreateCardToken(card) console.log("CARD TOKEN CREATION", newCardToken); if(newCardToken.state == "AVAILABLE") { let newRegisteredCustomer = await PerformLink(id, newCardToken.paymentTokenId) console.log("LINK RESULT", newRegisteredCustomer) if(newRegisteredCustomer) { let state = await MOBILEDB_SAVE(id, newCardToken.paymentTokenId, {}) if(state.status == 1) { return { status: 'success', card: newCardToken, link: newRegisteredCustomer, mobile: state } } else { return { status: 'failed', card: newCardToken, link: newRegisteredCustomer, mobile: state } } } else { return { status: 'failed', card: newCardToken, link: newRegisteredCustomer } } } else { return { status: 'failed', card: newCardToken } } } const AddNewCardCustomer = async (customer_id, clientDetails) => { let customize = await setHiddenCustomization() let newCustomer = await CreateCustomer(clientDetails) let newCardToken = await CreateCardToken(clientDetails.card) if(newCustomer.code) { return { status: 'failed', merchant: newCustomer } } let newCustomerId = customer_id || newCustomer.id; console.log("newCardToken: " + JSON.stringify(clientDetails)); if(newCardToken.state == "AVAILABLE") { let newRegisteredCustomer = await PerformLink(newCustomerId, newCardToken.paymentTokenId) if(newRegisteredCustomer) { let state = await MOBILEDB_SAVE(newCustomerId, newCardToken.paymentTokenId, {}) if(state.status == 1) { return { status: 'success', card: newCardToken, link: newRegisteredCustomer, mobile: state } } else { return { status: 'failed', card: newCardToken, link: newRegisteredCustomer, mobile: state } } } else { return { status: 'failed', card: newCardToken, link: newRegisteredCustomer } } } else { return { status: 'failed', card: newCardToken } } } const initDeleteCard = async (id, token, uuid) => { let pmdel = await DeleteCard(id, token) let mdbdel = await MOBILEDB_DELETE(uuid) if(pmdel && mdbdel) { return {result: 'OK', paymaya: pmdel, mobile: mdbdel} } else { return {result: 'FAILED', paymaya: pmdel, mobile: mdbdel} } } const MOBILEDB_SAVE = async (id, token) => { let userdata = await DB.profile() let SESSION = await DB.session() console.log("TO SAVE DATA", userdata.data.card_number, id, token, userdata, SESSION) return fetch(MOBILEDB.server + "paymayatokens", { method: "post", body: JSON.stringify({ card_number: userdata.data.card_number, customer_id: id, token: token }), headers: {'Content-Type': 'application/json', 'Authorization': SESSION.token} }) .then((response) => response.json()) .then((json) => { return json; }) .catch((error) => { return error }); } const MOBILEDB_GET = async () => { let userdata = await DB.profile() let SESSION = await DB.session() return fetch(`${mobiledb_get}/${userdata.data.card_number}`, { method: "get", body: {}, headers: {'Content-Type': 'application/json', 'Authorization': SESSION.token} }) .then((response) => response.json()) .then((json) => { return json; }) .catch((error) => { return error }); } const MOBILEDB_DELETE = async (uuid) => { let userdata = await DB.profile() let SESSION = await DB.session() console.log("TO SAVE DATA", userdata.data.card_number, token) return fetch(MOBILEDB.server + "paymayatokens/" + uuid, { method: "DELETE", headers: {'Content-Type': 'application/json', 'Authorization': SESSION.token} }) .then((response) => response.json()) .then((json) => { return json; }) .catch((error) => { return error }); } const AddNewCard = async (clientDetails) => { let customize = await setHiddenCustomization() console.log("Custom", customize) //IF NOT, REGISTER THE CLIENT let newCustomer = await Register(clientDetails) if(newCustomer.status == 'success') { //IF SUCCESSFULLY REGISTERED console.log("REGISTER", newCustomer) return newCustomer } } const CheckOut = async (customerId, paymentToken, clientDetails, amount) => { //CHECK IF CLIENT HAS CUSTOMER DATA if(customerId && paymentToken) { } else { let customize = await setDefaultCustomization() //IF NOT, REGISTER THE CLIENT let newCustomer = await Register(clientDetails) if(newCustomer.status == 'success') { //IF SUCCESSFULLY REGISTERED console.log("REGISTER", newCustomer) //MUST SAVE DETAILS TO DATABASE let state = await MOBILEDB_SAVE(newCustomer.merchant.id, newCustomer.card.paymentTokenId, newCustomer) console.log("SAVING STATE", state) if(state.status == 1){ //AFTER SAVING, PROCEED TO PAYOUT let payout = await Payout(newCustomer.merchant.id, newCustomer.card.paymentTokenId, amount) payout.status = 'success' payout.card_number = newCustomer.link.maskedPan return payout }else if(state.status == 0){ return { status: 'failed', result: newCustomer } } } else { return { status: 'failed', result: newCustomer } } } } const NewCheckout = async (details, amount) => { let customize = await setNewDefaultCustomization() let newCustomer = await CreateCustomer(details) let paymentToken = await CreateCardToken(details.card) var newCustomerResult = null if(newCustomer.code) { newCustomerResult = { status: 'failed', merchant: newCustomer } } if(paymentToken.state == "AVAILABLE") { let newRegisteredCustomer = await PerformLink(newCustomer.id, paymentToken.paymentTokenId) if(newRegisteredCustomer) { newCustomerResult = { status: 'success', merchant: newCustomer, card: paymentToken, link: newRegisteredCustomer } } else { newCustomerResult = { status: 'failed', merchant: newCustomer, card: paymentToken, link: newRegisteredCustomer } } } else { newCustomerResult = { status: 'failed', merchant: newCustomer, card: paymentToken } } if(paymentToken.state == "AVAILABLE" && newCustomerResult.status == "success") { let isSaveNewCustomerCard = await MOBILEDB_SAVE(newCustomerResult.merchant.id, newCustomerResult.card.paymentTokenId, newCustomerResult) if(isSaveNewCustomerCard.status) { let clientDetails = { paymentTokenId: paymentToken.paymentTokenId, totalAmount: { amount: amount, currency: "PHP" }, ...details, redirectUrl: { success: "unioil.com/success", failure: "unioil.com/failure", cancel: "unioil.com/cancel" } } let payments = await CreateCustomerPayments(clientDetails) payments.status = 'success' payments.card_number = newCustomerResult.link.maskedPan return payments } else { return { status: 'failed', result: isSaveNewCustomerCard.message } } } else { return { status: 'failed', result: paymentToken } } } export default { initCheckOut: CheckOut, initNewCheckout: NewCheckout, initPayOut: Payout, initNewPayout: NewPayout, initAdd: AddCard, initAddCard: AddNewCard, initNewAddCard: AddNewCardCustomer, initRemove: initDeleteCard, getAccounts: GetCustomerCards, getTransactionDetails: GetCustomerTransactionDetails }