* @param array $fields Final/sanitized submitted field data.
* @param array $entry Copy of original $_POST.
* @param array $form_data Form data and settings.
* @param string $entry_id Entry ID.
public function update_entry_meta( $fields, $entry, $form_data, $entry_id ) {
if ( empty( $entry_id ) || $this->errors || ! $this->api ) {
$resource = $this->api->get_response_resource();
if ( empty( $resource ) ) {
wpforms()->obj( 'entry' )->update(
* Fire when entry details and add meta was successfully updated.
* @param array $fields Final/sanitized submitted field data.
* @param array $form_data Form data and settings.
* @param string $entry_id Entry ID.
* @param array $resource Response resource data.
* @param Process $process Process class instance.
do_action( 'wpforms_square_process_update_entry_meta', $fields, $form_data, $entry_id, $resource, $this ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
* Add details to payment data.
* @param array $payment_data Payment data args.
* @param array $fields Final/sanitized submitted field data.
* @param array $form_data Form data and settings.
public function prepare_payment_data( $payment_data, array $fields, array $form_data ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$payment_data = (array) $payment_data;
// If there are errors or API is not initialized, return the original payment data.
if ( $this->errors || ! $this->api ) {
$resource = $this->api->get_response_resource();
// If the resource is empty, return the original payment meta.
if ( empty( $resource ) ) {
$type = Helpers::array_key_first( $resource );
$payment = $resource[ $type ];
$is_subscription = $type === 'subscription';
$payment_data['status'] = 'processed';
$payment_data['gateway'] = 'square';
$payment_data['mode'] = Helpers::is_sandbox_mode() ? 'test' : 'live';
$payment_data['customer_id'] = sanitize_text_field( $payment->getCustomerId() );
$payment_data['title'] = $this->get_payment_title( $payment );
if ( $is_subscription ) {
$payment_data['subscription_id'] = sanitize_text_field( $payment->getId() );
$payment_data['subscription_status'] = 'not-synced';
$payment_data['transaction_id'] = sanitize_text_field( $payment->getId() );
* @param object $payment Payment object.
* @return string Payment title.
private function get_payment_title( $payment ): string {
// Look for the cardholder name.
$card = $this->get_card();
$customer_name = $card ? $this->get_card_holder( $card ) : '';
return sanitize_text_field( $customer_name );
$customer_name = $this->get_customer_name();
return sanitize_text_field( implode( ' ', array_values( $customer_name ) ) );
$customer_email = $this->get_customer_email();
return sanitize_email( $customer_email );
* Add payment meta for a successful one-time or subscription payment.
* @param array $payment_meta Payment meta.
* @param array $fields Final/sanitized submitted field data.
* @param array $form_data Form data and settings.
public function prepare_payment_meta( $payment_meta, array $fields, array $form_data ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$payment_meta = (array) $payment_meta;
// If there are errors or API is not initialized, return the original payment meta.
if ( $this->errors || ! $this->api ) {
$resource = $this->api->get_response_resource();
// If the resource is empty, return the original payment meta.
if ( empty( $resource ) ) {
$type = Helpers::array_key_first( $resource );
$credit_card_details = $this->get_card();
$is_subscription = $type === 'subscription';
if ( $is_subscription ) {
$payment_meta['subscription_period'] = $this->subscription_settings['phase_cadence']['slug'];
$payment_meta['method_type'] = 'card';
if ( ! empty( $credit_card_details ) ) {
$payment_meta['credit_card_last4'] = $credit_card_details->getLast4();
$payment_meta['credit_card_expires'] = $credit_card_details->getExpMonth() . '/' . $credit_card_details->getExpYear();
$payment_meta['credit_card_method'] = strtolower( $credit_card_details->getCardBrand() );
$payment_meta['credit_card_name'] = $this->get_card_holder( $credit_card_details );
// Add a log indicating that the charge was successful.
$payment_meta['log'] = $this->format_payment_log( 'Square payment was created.' );
* Add payment info for successful payment.
* @param string $payment_id Payment ID.
* @param array $fields Final/sanitized submitted field data.
* @param array $form_data Form data and settings.
public function process_payment_saved( $payment_id, array $fields, array $form_data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$payment_id = (string) $payment_id;
// If there are errors or API is not initialized, return the original payment meta.
if ( $this->errors || ! $this->api ) {
$resource = $this->api->get_response_resource();
// If the resource is empty, return the original payment meta.
if ( empty( $resource ) ) {
$type = Helpers::array_key_first( $resource );
if ( $type === 'subscription' ) {
$this->api->update_subscription(
'id' => $resource[ $type ]->getId(),
'payment_id' => $payment_id,
wpforms()->obj( 'payment_meta' )->add_log(
'Square payment was processed. (Receipt ID: %s)',
$resource[ $type ]->getReceiptNumber()
* Return payment log value.
* @param string $value Log value.
private function format_payment_log( string $value ): string {
'value' => sanitize_text_field( $value ),
'date' => gmdate( 'Y-m-d H:i:s' ),
private function get_customer_name(): array {
if ( ! empty( $this->fields[ $this->settings['billing_name'] ]['first'] ) ) {
$customer_name['first_name'] = $this->fields[ $this->settings['billing_name'] ]['first'];
if ( ! empty( $this->fields[ $this->settings['billing_name'] ]['last'] ) ) {
$customer_name['last_name'] = $this->fields[ $this->settings['billing_name'] ]['last'];
// If a Name field has the `Simple` format.
empty( $customer_name['first_name'] ) &&
empty( $customer_name['last_name'] ) &&
! empty( $this->fields[ $this->settings['billing_name'] ]['value'] )
$customer_name['first_name'] = $this->fields[ $this->settings['billing_name'] ]['value'];
private function get_customer_email(): string {
return ! empty( $this->fields[ $this->settings['buyer_email'] ]['value'] ) ? $this->fields[ $this->settings['buyer_email'] ]['value'] : '';
* Retrieve a Cardholder Name.
* @param Card $card Card object.
private function get_card_holder( $card ): string {
if ( $card instanceof Card ) {
$holder = $card->getCardholderName();
if ( empty( $holder ) && isset( $this->cc_field['cardname'] ) ) {
$holder = $this->cc_field['cardname'];