meta_array( Payment::META_EMAILED ); if ( in_array( $status, $sent, true ) ) { return; } $sent[] = $status; $payment->set_meta( Payment::META_EMAILED, $sent ); $to = Settings::email_to(); if ( '' === $to ) { return; } $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $amount = Amounts::fiat( $payment->amount(), $payment->currency() ); $label = Payment::status_label( $status ); $manager = new PaymentManager( $payment ); /* translators: 1: site name, 2: amount with currency, 3: status label */ $subject = sprintf( __( '[%1$s] Crypto payment %2$s — %3$s', 'cryptoway-button' ), $site, $amount, $label ); $lines = []; $lines[] = sprintf( /* translators: 1: status label, 2: amount with currency, 3: what the payment is for */ __( '%1$s: %2$s — %3$s', 'cryptoway-button' ), $label, $amount, $payment->title() ); $lines[] = ''; foreach ( $manager->amount_rows() as $row_label => $value ) { $lines[] = $row_label . ': ' . $value; } foreach ( $payment->payer() as $field => $value ) { $name = match ( $field ) { 'name' => __( 'Name', 'cryptoway-button' ), 'email' => __( 'E-mail', 'cryptoway-button' ), default => __( 'Message', 'cryptoway-button' ), }; $lines[] = $name . ': ' . $value; } $history = $payment->history(); if ( $history ) { $lines[] = ''; $lines[] = (string) end( $history )['m']; } $lines[] = ''; $lines[] = __( 'Invoice', 'cryptoway-button' ) . ': ' . $payment->invoice_id(); $lines[] = __( 'Payment', 'cryptoway-button' ) . ': ' . $payment->admin_url(); self::send( $to, $subject, $lines, sprintf( 'the e-mail about payment #%d (%s)', $payment->id(), $status ) ); } /** * wp_mail() with a log line when it fails: a site without working mail otherwise loses every * notice without a trace. The address is not logged. * * @param string[] $lines */ private static function send( string $to, string $subject, array $lines, string $what ): void { if ( ! wp_mail( $to, $subject, implode( "\n", $lines ) ) ) { Log::write( sprintf( 'Could not send %s: wp_mail() failed. Check that this site can send e-mail.', $what ) ); } } /** * To the payer, once, when a payment for paid content is confirmed and the payer left an * e-mail: the link that opens the content again on any device. */ public static function payer( Payment $payment, string $status ): void { if ( Payment::STATUS_PAID !== $status || Button::AFTER_CONTENT !== $payment->after() ) { return; } $to = (string) ( $payment->payer()['email'] ?? '' ); if ( '' === $to || ! is_email( $to ) ) { return; } $sent = $payment->meta_array( Payment::META_EMAILED ); if ( in_array( 'payer', $sent, true ) ) { return; } $sent[] = 'payer'; $payment->set_meta( Payment::META_EMAILED, $sent ); self::access_link( $payment ); } /** * The access-link mail itself: on confirmation, and again whenever the payer asks for it * under the button ("Already paid?"). The payer's e-mail was checked when the payment started. * * @param bool $again True when the payer asked for the link again: a subject that says so. */ public static function access_link( Payment $payment, bool $again = false ): void { $to = (string) ( $payment->payer()['email'] ?? '' ); $until = Button::access_until( $payment ); if ( '' === $to || ! is_email( $to ) || $until <= time() ) { return; } $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $subject = $again /* translators: %s: site name */ ? sprintf( __( '[%s] Your access link', 'cryptoway-button' ), $site ) /* translators: %s: site name */ : sprintf( __( '[%s] Your payment is confirmed', 'cryptoway-button' ), $site ); $lines = []; $lines[] = sprintf( /* translators: 1: what the payment is for, 2: amount with currency */ __( 'Thank you! Your payment for "%1$s" (%2$s) is confirmed.', 'cryptoway-button' ), $payment->title(), Amounts::fiat( $payment->amount(), $payment->currency() ) ); $lines[] = ''; $lines[] = __( 'Open what you paid for with this link (it works on any device):', 'cryptoway-button' ); $lines[] = Checkout::done_url( $payment ); $lines[] = ''; /* translators: %s: date */ $lines[] = sprintf( __( 'The link stays open until %s.', 'cryptoway-button' ), wp_date( (string) get_option( 'date_format' ), $until ) ); self::send( $to, $subject, $lines, sprintf( 'the access link of payment #%d', $payment->id() ) ); } }