@causalfoundry/react-native-cf-payments
Version:
causalfoundry.ai SDK for tracking logs for Payments content block.
84 lines (78 loc) • 5.26 kB
text/typescript
import {NativeModules, Platform} from 'react-native';
import {DeferredPaymentProperties, PaymentMethodProperties} from "./typings";
const {CfLogPayments} = NativeModules;
/**
* logDeferredPaymentEvent is to log events for payments when they undergo processing and
* processed completed or canceled. This log is for use cases where we have to Buy Now and Pay Later
* which means that customers can buy the items now and pay the amount at a later date.
*
* @param order_id is required to set the id for the order in the log, Id should be in
* string and must be per the checkout.
* @param id is required to set the id for the payment in the log, Id should be in
* string and must be per the catalog provided.
* @param type is required to set the type of the payment is being selected.
* By default SDK provides an enum class to use as PaymentAction.
* @param action is used to specify the action performed on the payments screen,
* there can be multiple types of actions that can include init payment
* process, cancel payment processing or upload the receipt to a bank deposit.
* By default, SDK provides an enum class to use as PaymentAction.
* @param account_balance is required to log the current balance of the user. Amount format
* Should be per the currency selected.
* @param payment_amount is required to log the total price of the payment being logged.
* Amount format should be per the currency selected.
* @param currency is required to log the currency for the payment logged.
* Currency should be in ISO 4217 format. For ease, SDK provides the
* enums to log the currency code so that it would be easy to log. You can
* also use the string function to provide the currency.
* @param is_successful is required to log if the payment is processed successfully or not.
* @param meta is to send any other data you want to send with the ingest, can be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logDeferredPaymentEvent = (properties: DeferredPaymentProperties) => {
console.log(properties)
if (Platform.OS === 'android') {
CfLogPayments.logDeferredPaymentEvent(properties.order_id, properties.id, properties.type,
properties.action, properties.account_balance, properties.payment_amount,
properties.currency, properties.is_successful, null, false)
}else if (Platform.OS === 'ios') {
CfLogPayments.logDeferredPaymentEvent(properties.order_id, properties.id, properties.type,
properties.action, properties.account_balance, properties.payment_amount,
properties.currency, properties.is_successful, false)
}
}
/**
* logPaymentMethodEvent is to log events for payments. Which method of the payment is
* selected for the order.
*
* @param paymentId is required to set the id for the payment in log, if not
* then provide payment_<user_id>
* @param orderId is required to set the id for the order in log, Id should be in string
* and must be in accordance to the catalog provided.
* @param type is required to set the type of the payment is being selected.
* By default SDK provides an enum class to use as PaymentAction.
* @param action is required to log the action on the payment being logged.
* @param payment_amount is required to log the total price of the payment being logged.
* Amount format should be per the currency selected.
* @param currency is required to log the currency for the payment logged.
* Currency should be in ISO 4217 format. For ease, SDK provides the
* enums to log the currency so that it would be easy to log. You can
* also use the string function to provide the currency.
* @param meta is to send any other data you want to send with the ingest, can also be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logPaymentMethodEvent = (properties: PaymentMethodProperties) => {
console.log(properties)
if (Platform.OS === 'android') {
CfLogPayments.logPaymentMethodEvent(properties.payment_id, properties.order_id,
properties.type, properties.action, properties.payment_amount, properties.currency, null, false)
}else if (Platform.OS === 'ios') {
CfLogPayments.logPaymentMethodEvent(properties.payment_id, properties.order_id,
properties.type, properties.action, properties.payment_amount, properties.currency, false)
}
}
export default {
logDeferredPaymentEvent,
logPaymentMethodEvent
}