* @param UpsertCatalogObjectRequest $request Create subscription plan request object.
* @return CatalogObject|null
private function send_create_plan_request( $request ) {
$this->response = $this->client->getCatalogApi()->upsertCatalogObject( $request );
if ( ! $this->response->isSuccess() ) {
$this->errors[] = esc_html__( 'Square fail: subscription plan was not created.', 'wpforms-lite' );
return $this->response->getResult()->getCatalogObject();
} catch ( ApiException $e ) {
$this->errors[] = esc_html__( 'Square fail: subscription plan was not created.', 'wpforms-lite' );
* Send a create subscription plan variations request to API.
* @param UpsertCatalogObjectRequest $request Create subscription plan request object.
* @return CatalogObject|null
private function send_create_plan_variations_request( $request ) {
$this->response = $this->client->getCatalogApi()->upsertCatalogObject( $request );
if ( ! $this->response->isSuccess() ) {
$this->errors[] = esc_html__( 'Square fail: subscription plan variation was not created.', 'wpforms-lite' );
return $this->response->getResult()->getCatalogObject();
} catch ( ApiException $e ) {
$this->errors[] = esc_html__( 'Square fail: subscription plan variation was not created.', 'wpforms-lite' );
* Send a create subscription request to API.
* @param CreateSubscriptionRequest $request Create subscription request object.
* @return Subscription|null
private function send_create_subscription_request( $request ) {
$this->response = $this->client->getSubscriptionsApi()->createSubscription( $request );
if ( ! $this->response->isSuccess() ) {
$this->errors[] = esc_html__( 'Square fail: something went wrong during subscription process.', 'wpforms-lite' );
return $this->response->getResult()->getSubscription();
} catch ( ApiException $e ) {
$this->errors[] = esc_html__( 'Square fail: something went wrong during subscription process.', 'wpforms-lite' );
* Retrieve a update subscription request object.
* @param Subscription $subscription Subscription object.
* @param array $args Subscription arguments.
* @return UpdateSubscriptionRequest|null
private function get_update_subscription_request_object( $subscription, array $args ) {
if ( ! $subscription instanceof Subscription ) {
$subscription->setSource( new SubscriptionSource() );
$subscription->getSource()->setName( Square::APP_NAME . ' Payment #' . $args['payment_id'] );
$request = new UpdateSubscriptionRequest();
$request->setSubscription( $subscription );
* Send a create subscription request to API.
* @param string $subscription_id Subscription id.
* @param UpdateSubscriptionRequest $request Update subscription request object.
* @return Subscription|null
private function send_update_subscription_request( string $subscription_id, $request ) {
$this->response = $this->client->getSubscriptionsApi()->updateSubscription( $subscription_id, $request );
if ( ! $this->response->isSuccess() ) {
$this->errors[] = esc_html__( 'Square fail: something went wrong during subscription update.', 'wpforms-lite' );
return $this->response->getResult()->getSubscription();
} catch ( ApiException $e ) {
$this->errors[] = esc_html__( 'Square fail: something went wrong during subscription update.', 'wpforms-lite' );
* Retrieve card details from specific transaction in object format.
* @param string $transaction_id The ID of the order to retrieve card details from.
public function get_card_details_from_transaction_id( string $transaction_id ) {
$response = $this->client->getPaymentsApi()->getPayment( $transaction_id );
if ( ! $response->isSuccess() ) {
$payment = $response->getResult()->getPayment();
$card_details = $payment->getCardDetails();
$card = $card_details->getCard();
// Create a temporary object to mimic Square's payment_method_details structure.
$details = new stdClass();
$details->source_type = 'card';
$details->card_details = new stdClass();
$details->card_details->card = new stdClass();
$details->card_details->card->last_4 = $card->getLast4();
$details->card_details->card->card_brand = $card->getCardBrand();
$details->card_details->card->exp_month = $card->getExpMonth();
$details->card_details->card->exp_year = $card->getExpYear();
* Retrieve the latest invoice transaction_id.
* @param Invoice $invoice Invoice object.
public function get_latest_invoice_transaction_id( $invoice ): string {
$order_id = $invoice->getOrderId();
$order_response = $this->client->getOrdersApi()->retrieveOrder( $order_id );
if ( ! $order_response->isSuccess() ) {
$order = $order_response->getResult()->getOrder();
$tenders = $order->getTenders();
if ( empty( $tenders ) ) {
// Assuming the last tender represents the final transaction.
return end( $tenders )->getPaymentId();
* Retrieve the invoice by order ID.
* @param string $order_id Order ID.
public function get_invoice_by_order_id( string $order_id ) {
$invoices_api = $this->client->getInvoicesApi();
$response = $invoices_api->listInvoices( Helpers::get_location_id() );
if ( ! $response->isSuccess() ) {
$invoices = $response->getResult()->getInvoices();
if ( empty( $invoices ) ) {
foreach ( $invoices as $invoice ) {
if ( $invoice->getOrderId() === $order_id ) {
} catch ( ApiException $e ) {