namespace WPForms\Integrations\Square;
use WPForms\Vendor\Square\Environment;
use WPForms\Helpers\Crypto;
* Valid connection status.
const STATUS_VALID = 'valid';
* Invalid connection status.
const STATUS_INVALID = 'invalid';
* Determine if a connection for production mode.
* Square-issued ID of an application.
* Square-issued ID of the merchant.
* Currency associated with a merchant account.
* Date when tokens should be renewed.
* Determine if connection tokens are encrypted.
* Determine if scopes were updated.
private $scopes_updated = 0;
* Connection constructor.
* @param array $data Connection data.
* @param bool $encrypted Optional. Default true. Use false when connection tokens were not encrypted.
public function __construct( array $data, bool $encrypted = true ) {
if ( ! empty( $data['access_token'] ) ) {
$this->access_token = $data['access_token'];
if ( ! empty( $data['refresh_token'] ) ) {
$this->refresh_token = $data['refresh_token'];
if ( ! empty( $data['client_id'] ) ) {
$this->client_id = $data['client_id'];
if ( ! empty( $data['merchant_id'] ) ) {
$this->merchant_id = $data['merchant_id'];
if ( ! empty( $data['scopes_updated'] ) ) {
$this->scopes_updated = $data['scopes_updated'];
$this->set_status( empty( $data['status'] ) ? self::STATUS_VALID : $data['status'] );
$this->currency = empty( $data['currency'] ) ? '' : strtoupper( (string) $data['currency'] );
$this->renew_at = empty( $data['renew_at'] ) ? time() : (int) $data['renew_at'];
$this->live_mode = ! empty( $data['live_mode'] );
$this->encrypted = (bool) $encrypted;
* Retrieve a connection instance if it exists.
* @param string $mode Square mode.
* @param bool $encrypted Optional. Default true. Use false when connection tokens were not encrypted.
* @return Connection|null
public static function get( string $mode = '', bool $encrypted = true ) {
$mode = Helpers::validate_mode( $mode );
$connections = (array) get_option( 'wpforms_square_connections', [] );
if ( empty( $connections[ $mode ] ) ) {
return new self( (array) $connections[ $mode ], $encrypted );
* Save connection data into DB.
$connections = (array) get_option( 'wpforms_square_connections', [] );
$connections[ $this->get_mode() ] = $this->get_data();
update_option( 'wpforms_square_connections', $connections );
* Delete connection data from DB.
public function delete() {
$connections = (array) get_option( 'wpforms_square_connections', [] );
unset( $connections[ $this->get_mode() ] );
empty( $connections ) ? delete_option( 'wpforms_square_connections' ) : update_option( 'wpforms_square_connections', $connections );
public function revoke_tokens() {
$connections = (array) get_option( 'wpforms_square_connections', [] );
$mode = $this->get_mode();
$connections[ $mode ] = $this->get_data();
$connections[ $mode ]['access_token'] = '';
$connections[ $mode ]['refresh_token'] = '';
update_option( 'wpforms_square_connections', $connections );
* Retrieve true if a connection for production mode.
public function get_live_mode(): bool {
* Retrieve a connection mode.
public function get_mode(): string {
return $this->live_mode ? Environment::PRODUCTION : Environment::SANDBOX;
* Retrieve an un-encrypted access token.
public function get_access_token(): string {
return $this->encrypted ? Crypto::decrypt( $this->access_token ) : $this->access_token;
* Retrieve an un-encrypted refresh token.
public function get_refresh_token(): string {
return $this->encrypted ? Crypto::decrypt( $this->refresh_token ) : $this->refresh_token;
public function get_client_id(): string {
* Retrieve an ID of the authorized merchant.
public function get_merchant_id(): string {
return $this->merchant_id;
* Retrieve a currency code of the authorized merchant.
public function get_currency(): string {
* @param string $code Currency code.
public function set_currency( string $code ) {
$this->currency = strtoupper( $code );
* Retrieve a connection status.
public function get_status(): string {
* Set a connection status if it valid.
* @param string $status The connection status.
public function set_status( string $status ) {
if ( in_array( $status, $this->get_statuses(), true ) ) {
* Retrieve a renewal timestamp.
public function get_renew_at(): int {
* Set/update a renewal timestamp.
public function set_renew_at() {
// Tokens must automatically renew every 7 days or less.
$this->renew_at = time() + wp_rand( 5, 8 ) * DAY_IN_SECONDS;
* Retrieve a scopes updated timestamp.
public function get_scopes_updated(): int {
return $this->scopes_updated;
* Set/update a scopes updated timestamp.
public function set_scopes_updated() {
$this->scopes_updated = time();
* Encrypt tokens, if it needed.
public function encrypt_tokens() {
// Bail if tokens have already encrypted.
if ( $this->encrypted ) {
// Bail if tokens are not passed.
if ( empty( $this->access_token ) || empty( $this->refresh_token ) ) {
// Prepare encrypted tokens.
$encrypted_access_token = Crypto::encrypt( $this->access_token );
$encrypted_refresh_token = Crypto::encrypt( $this->refresh_token );
// Bail if encrypted tokens are invalid.
if ( empty( $encrypted_access_token ) || empty( $encrypted_refresh_token ) ) {
$this->access_token = $encrypted_access_token;
$this->refresh_token = $encrypted_refresh_token;
* Retrieve available statuses.
private function get_statuses(): array {
return [ self::STATUS_VALID, self::STATUS_INVALID ];
* Retrieve a connection in array format, simply like `toArray` method.
private function get_data(): array {
'live_mode' => $this->live_mode,
'access_token' => $this->access_token,
'refresh_token' => $this->refresh_token,
'client_id' => $this->client_id,
'merchant_id' => $this->merchant_id,
'currency' => $this->currency,
'status' => $this->status,
'renew_at' => $this->renew_at,
'scopes_updated' => $this->scopes_updated,
* Determine whether connection tokens is encrypted.
private function is_encrypted(): bool {
* Determine whether a connection is configured fully.
public function is_configured(): bool {
return ! empty( $this->get_access_token() ) && ! empty( $this->get_refresh_token() ) && ! empty( $this->client_id ) && ! empty( $this->merchant_id );