@@ -111,7 +111,7 @@ public function process_payment( $orderId ) {
111111
112112 // Check for existing invoice and redirect instead.
113113 if ( $ this ->validInvoiceExists ( $ orderId ) ) {
114- $ existingInvoiceId = get_post_meta ( $ orderId , 'BTCPay_id ' , true );
114+ $ existingInvoiceId = $ order -> get_meta ( 'BTCPay_id ' );
115115 Logger::debug ( 'Found existing BTCPay Server invoice and redirecting to it. Invoice id: ' . $ existingInvoiceId );
116116
117117 return [
@@ -546,12 +546,13 @@ protected function processOrderStatus(\WC_Order $order, \stdClass $webhookData)
546546 */
547547 protected function validInvoiceExists ( int $ orderId ): bool {
548548 // Check order metadata for BTCPay_id.
549- if ( $ invoiceId = get_post_meta ( $ orderId , 'BTCPay_id ' , true ) ) {
549+ $ order = wc_get_order ($ orderId );
550+ if ( $ invoiceId = $ order ->get_meta ( 'BTCPay_id ' ) ) {
550551 // Validate the order status on BTCPay server.
551552 $ client = new Invoice ( $ this ->apiHelper ->url , $ this ->apiHelper ->apiKey );
552553 try {
553554 Logger::debug ( 'Trying to fetch existing invoice from BTCPay Server. ' );
554- $ invoice = $ client ->getInvoice ( $ this ->apiHelper ->storeId , $ invoiceId );
555+ $ invoice = $ client ->getInvoice ( $ this ->apiHelper ->storeId , $ invoiceId );
555556 $ invalidStates = [ 'Expired ' , 'Invalid ' ];
556557 if ( in_array ( $ invoice ->getData ()['status ' ], $ invalidStates ) ) {
557558 return false ;
@@ -613,25 +614,28 @@ public function updateWCOrderPayments(\WC_Order $order): void {
613614 if ((float ) $ payment ->getPaymentMethodPaid () > 0.0 ) {
614615 $ paymentMethodName = $ payment ->getPaymentMethod ();
615616 // Update order meta data with payment methods and transactions.
616- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_total_paid " , $ payment ->getTotalPaid () ?? '' );
617- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_total_amount " , $ payment ->getAmount () ?? '' );
618- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_total_due " , $ payment ->getDue () ?? '' );
619- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_total_fee " , $ payment ->getNetworkFee () ?? '' );
620- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_rate " , $ payment ->getRate () ?? '' );
617+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_total_paid " , $ payment ->getTotalPaid () ?? '' );
618+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_total_amount " , $ payment ->getAmount () ?? '' );
619+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_total_due " , $ payment ->getDue () ?? '' );
620+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_total_fee " , $ payment ->getNetworkFee () ?? '' );
621+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_rate " , $ payment ->getRate () ?? '' );
621622 if ((float ) $ payment ->getRate () > 0.0 ) {
622623 $ formattedRate = number_format ((float ) $ payment ->getRate (), wc_get_price_decimals (), wc_get_price_decimal_separator (), wc_get_price_thousand_separator ());
623- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_rateFormatted " , $ formattedRate );
624+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_rateFormatted " , $ formattedRate );
624625 }
625626
626627 // For each actual payment make a separate entry to make sense of it.
627628 foreach ($ payment ->getPayments () as $ index => $ trx ) {
628- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_id " , $ trx ->getTransactionId () ?? '' );
629- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_timestamp " , $ trx ->getReceivedTimestamp () ?? '' );
630- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_destination " , $ trx ->getDestination () ?? '' );
631- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_amount " , $ trx ->getValue () ?? '' );
632- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_status " , $ trx ->getStatus () ?? '' );
633- update_post_meta ( $ order ->get_id (), "BTCPay_ {$ paymentMethodName }_ {$ index }_networkFee " , $ trx ->getFee () ?? '' );
629+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_id " , $ trx ->getTransactionId () ?? '' );
630+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_timestamp " , $ trx ->getReceivedTimestamp () ?? '' );
631+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_destination " , $ trx ->getDestination () ?? '' );
632+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_amount " , $ trx ->getValue () ?? '' );
633+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_status " , $ trx ->getStatus () ?? '' );
634+ $ order ->update_meta_data ( "BTCPay_ {$ paymentMethodName }_ {$ index }_networkFee " , $ trx ->getFee () ?? '' );
634635 }
636+
637+ // Save the order.
638+ $ order ->save ();
635639 }
636640 }
637641 } catch (\Throwable $ e ) {
@@ -766,8 +770,10 @@ protected function preparePosMetadata( $order ): string {
766770 */
767771 protected function updateOrderMetadata ( int $ orderId , \BTCPayServer \Result \Invoice $ invoice ) {
768772 // Store relevant BTCPay invoice data.
769- update_post_meta ( $ orderId , 'BTCPay_redirect ' , $ invoice ->getData ()['checkoutLink ' ] );
770- update_post_meta ( $ orderId , 'BTCPay_id ' , $ invoice ->getData ()['id ' ] );
773+ $ order = wc_get_order ($ orderId );
774+ $ order ->update_meta_data ( 'BTCPay_redirect ' , $ invoice ->getData ()['checkoutLink ' ] );
775+ $ order ->update_meta_data ( 'BTCPay_id ' , $ invoice ->getData ()['id ' ] );
776+ $ order ->save ();
771777 }
772778
773779 /**
0 commit comments