The Pay Form
The Pay Form is provided by Tyro and used to securely collect customer payment details.
Create the HTML
- Create the container where the Pay Form will be injected into.
- Create the submit button
<!-- HTML sample code -->
<form id="pay-form">
<div id="tyro-pay-form">
<!--Tyro.js injects the Pay Input Fields -->
</div>
<button id="pay-form-submit">Pay</button>
</form>
tyro.createPayForm(config)
This method creates an instance of the Pay Form, and optionally supports a parameter object to customize the appearance.
Please refer to the Style Guide for detailed information about appearance customizations.
Config Parameters
object An optional object containing properties for customizing the Pay Form. | |||||||||||||||||||||||||||||||||||
|
object An optional object containing properties for customizing the appearance of the widget. |
theme | string An optional string to load a set of preset styleProps. Defaults to default. |
Implementation
// Javascript code sample
const payForm = tyro.createPayForm({
// Example properties
theme: 'default',
styleProps: {
bodyMinWidth: 250,
bodyMaxWidth: 980,
labelPosition: 'block',
walletPaymentsButtonsPadding: '30px',
applePayButton: {
buttonStyle: 'black'
}
},
options: {
applePay: {
enabled: true,
totalLabel: 'Example Merchant Name',
supportedNetworks: ['visa', 'mastercard'],
},
googlePay: {
enabled: true,
merchantInfo: {
merchantName: 'Example Merchant Name',
merchantId: 'example-merchant-id-000',
},
supportedNetworks: ['visa', 'mastercard'],
},
creditCardForm: {
enabled: true,
supportedNetworks: ['visa', 'mastercard', 'amex'],
}
}
});
payForm.inject(selector)
Injects the Pay Form into the HTML page. Returns a Promise.
Method Parameters
selector required | string The selector that identifies the HTML container that the Pay Form should be injected into. |
Implementation
// Javascript code sample
await payForm.inject("#tyro-pay-form");
Event Functions
payForm.setReadyListener(listener)
Sets the readyListener
on the Pay Form. This listener is called when the payRequest has loaded and the payForm is ready for processing.
Implementation
// Javascript code sample
payForm.setReadyListener(() => {
// your code to handle presentation, animations or show the pay button
});
Wallet Payment Functions
payForm.isApplePaySupported()
Returns true
if Apple Pay is supported, otherwise false
.
payForm.isGooglePaySupported()
Returns true
if Google Pay is supported, otherwise false
.
payForm.setWalletPaymentBeginListener(listener)
Sets the walletPaymentBeginListener
on the Pay Form. This listener is called at the beginning of a wallet payment before the dialog is shown.
Listener Function Parameters
paymentType | string The payment type used for the wallet payment |
Implementation
// Javascript code sample
payForm.setWalletPaymentBeginListener((paymentType) => {
// your code to show loading state
});
payForm.setWalletPaymentCancelledListener(listener)
Sets the walletPaymentCancelledListener
on the Pay Form. This listener is called when a wallet payment is cancelled by the user.
Listener Function Parameters
paymentType | string The payment type used for the wallet payment |
Implementation
// Javascript code sample
payForm.setWalletPaymentCancelledListener((paymentType) => {
// your code to handle a cancelled wallet payment
});
payForm.setWalletPaymentCompleteListener(listener)
Sets the walletPaymentCompleteListener
on the Pay Form. This listener is called when a wallet payment transaction is complete or errors.
The error object will be defined if there is an error.
Listener Function Parameters
paymentType | string The payment type used for the wallet payment |
error | object Will be defined if there is an error |
Error Object
type | string The error type can be used to determine what is shown to the customer e.g. SERVER_VALIDATION_ERROR |
errorMessage | string The error message for a submitted transaction if available e.g. Insufficient Funds |
errorCode | string The error code for a declined transaction if available e.g. Insufficient Funds |
gatewayCode | string The code returned by the payment gateway for a submitted transaction e.g. DECLINED |
- For more
type
information see Error Types. - Depending on the stage of the particular error, different error codes can be returned. Please refer to full list of supported Error Codes.
Implementation
// Javascript sample code
async function showPaymentResult() {
const payRequest = await tyro.fetchPayRequest();
switch (payRequest.status) {
case "SUCCESS":
return showSuccess();
case "FAILED":
return showFail();
......
}
}
payForm.setWalletPaymentCompleteListener((paymentType, error) => {
if(error) {
const { type, errorMessage, errorCode, gatewayCode } = error;
// handle errors here
switch (type) {
case "SERVER_VALIDATION_ERROR":
return showServerValidationError(errorMessage, errorCode, gatewayCode);
case "CARD_ERROR":
return showCardError(errorMessage, errorCode, gatewayCode);
case "SERVER_ERROR":
return showGenericError(errorMessage, errorCode, gatewayCode);
......
}
} else {
showPaymentResult();
}
});