tokenize
The tokenize function is a method in the Pay Theory SDK that tokenizes payment methods for future use. It's called through a configured PayTheoryFragment instance after setting up the necessary tokenization configuration.
Implementation Flow
- Include PayTheoryFragment in your layout:
<fragment
android:id="@+id/payTheoryFragment"
android:name="com.paytheory.android.sdk.fragments.PayTheoryFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
- Configure the tokenization:
// Get submit button reference and the PayTheoryFragment
val submitButton = findViewById<PayTheoryButton>(R.id.submit_button)
val payTheoryFragment = supportFragmentManager.findFragmentById(R.id.payment_fragment) as PayTheoryFragment
// Create configuration
val configuration = PayTheoryConfiguration(
apiKey = resources.getString(R.string.api_key),
payTheoryButton = submitButton
).apply {
paymentMethodType = PaymentMethodType.CARD
}
// Configure the payment after fragment is attached
payTheoryFragment.configurePayment(
merchantActivity = this,
configuration = configuration
)
- Set up tokenization trigger:
// Set up button click listener
submitButton.setOnClickListener {
payTheoryFragment.tokenize()
}
Response Handling
Your activity must extend PayTheoryMerchantActivity and implement the Payable interface to handle tokenization responses. The following methods will be called based on the tokenization outcome:
class YourActivity : PayTheoryMerchantActivity(), Payable {
override fun handleTokenizeSuccess(result: PaymentMethodTokenResults) {
// Handle successful tokenization
println("Payment Method ID: ${result.paymentMethodId}")
println("Last Four: ${result.lastFour}")
println("Brand: ${result.brand}")
}
override fun handleError(error: PTError) {
// Handle system errors
println("Error: ${error.error}")
}
}
Important Notes
- The tokenization configuration must be properly set up before calling tokenize
- Make sure to implement all required Payable interface methods to handle responses
- Cash payment methods cannot be tokenized
- The activity must extend PayTheoryMerchantActivity and implement the Payable interface