get_total( 'edit' ), 2, '.', '' ); } /** * @return array{text: string, ts: int} */ public static function expiry( int $now, int $hours ): array { $ts = intdiv( $now + $hours * 3600, 60 ) * 60; return [ 'text' => ( new \DateTimeImmutable( '@' . $ts ) )->setTimezone( new \DateTimeZone( 'Europe/Sofia' ) )->format( 'd.m.Y H:i' ), 'ts' => $ts, ]; } public static function description( \WC_Order $order ): string { $name = html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ); $text = 'Order ' . $order->get_order_number() . ' ' . $name; $text = (string) preg_replace( '/[\x00-\x1F\x7F]/', '', $text ); return mb_substr( $text, 0, 100, 'UTF-8' ); } public static function lang(): string { return str_starts_with( get_locale(), 'bg' ) ? 'bg' : 'en'; } /** * @return array{amount: string, currency: string, expires: int, created: int, encoded: string, checksum: string} */ public static function attempt_entry( \WC_Order $order, string $invoice, int $hours, int $now ): array { $settings = Settings::get(); $amount = self::amount( $order ); $expiry = self::expiry( $now, $hours ); $encoded = Codec::encode( [ 'MIN' => $settings['min'], 'INVOICE' => $invoice, 'AMOUNT' => $amount, 'CURRENCY' => 'EUR', 'EXP_TIME' => $expiry['text'], 'DESCR' => self::description( $order ), 'ENCODING' => 'utf-8', ] ); return [ 'amount' => $amount, 'currency' => 'EUR', 'expires' => $expiry['ts'], 'created' => $now, 'encoded' => $encoded, 'checksum' => Codec::checksum( $encoded, $settings['secret'] ), ]; } public static function ok_url( \WC_Order $order ): string { return $order->get_checkout_order_received_url(); } public static function cancel_url( \WC_Order $order ): string { return add_query_arg( 'syntopia_pay_cancelled', '1', $order->get_checkout_payment_url() ); } /** * @param array $entry * @return array */ public static function form_fields( string $page, \WC_Order $order, array $entry ): array { if ( ! in_array( $page, self::PAGES, true ) ) { throw new \InvalidArgumentException( 'Unknown ePay page.' ); } return [ 'PAGE' => $page, 'ENCODED' => (string) $entry['encoded'], 'CHECKSUM' => (string) $entry['checksum'], 'LANG' => self::lang(), 'URL_OK' => self::ok_url( $order ), 'URL_CANCEL' => self::cancel_url( $order ), ]; } /** * @param array $entry */ public static function easypay_url( array $entry ): string { return Settings::host() . '/ezp/reg_bill.cgi?ENCODED=' . rawurlencode( (string) $entry['encoded'] ) . '&CHECKSUM=' . rawurlencode( (string) $entry['checksum'] ); } }