Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
2
Indexable
Never
/**
	 * Approve a sales order.
	 *
	 * @param int $so_id sales order id.
	 * @return object $response_body response object.
	 */
	public function approve_a_sales_order( $so_id ) {
		$plugins_log = $this->db->get_field( 'plugins_log' ) ? maybe_unserialize( $this->db->get_field( 'plugins_log' ) ) : array();

		$query_params = array(
			'organization_id' => $this->organization_id,
		);

		try {
			$request = $this->api_client()->request(
				'POST',
				'api/v3/salesorders/' . $so_id . '/approve',
				array(
					'headers'     => $this->api_request_header(),
					'query'       => $query_params,
					'http_errors' => false,
				)
			);

			$status_code = $request->getStatusCode();

			$response      = json_decode( $request->getBody()->getContents() );
			$response_body = '';

			if ( 200 === $status_code ) {
				$response_body = $response;
			}

			// Update log.
			array_push(
				$plugins_log,
				array(
					'time'    => gmdate( 'j F Y g:ia' ),
					'message' => $response->message,
				)
			);
			$this->db->update_field( 'plugins_log', array_slice( $plugins_log, -100, 100 ) );
		} catch ( ClientException $e ) {
			$response_body = $e->getResponse();
		}

		return $response_body;
	}