id, [ $this, 'scheduled_subscription_payment' ], 10, 2 ); add_action( 'wcs_resubscribe_order_created', [ $this, 'delete_resubscribe_meta' ], 10 ); } public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { if ( ! $renewal_order instanceof WC_Order ) { $renewal_order = wc_get_order( $renewal_order ); } if ( ! $renewal_order ) { return; } $paymentSource = $renewal_order->get_meta( '_achp_payment_source', true ); if ( empty( $paymentSource ) ) { $renewal_order->update_status( 'failed', __( 'Renewal failed: no saved ACH payment source on the order.', ACHP_TEXT_DOMAIN ) ); return; } $individualName = $renewal_order->get_meta( '_achp_individual_name', true ); if ( empty( $individualName ) ) { // Fall back to the billing name for subscriptions created before // the holder name was persisted as meta. $individualName = trim( $renewal_order->get_billing_first_name() . ' ' . $renewal_order->get_billing_last_name() ); } $paymentRequestArray = [ 'paymentMethodObjectId' => $paymentSource, 'Amount' => $amount_to_charge, 'AccountName' => $individualName, ]; $accountType = $renewal_order->get_meta( '_achp_account_type', true ); if ( ! empty( $accountType ) ) { $paymentRequestArray['actionType'] = '2'; } $remoteDataPay = ACHP_Main_Class::curlPost( 'CreatePayment', $paymentRequestArray ); $responseCode = $remoteDataPay['status']['responsecode'] ?? ''; if ( 0 === strcasecmp( $responseCode, 'Ok' ) ) { if ( ! empty( $remoteDataPay['paymentRefID'] ) ) { $renewal_order->set_transaction_id( $remoteDataPay['paymentRefID'] ); $renewal_order->update_meta_data( '_paymentRefID', $remoteDataPay['paymentRefID'] ); $renewal_order->update_meta_data( 'custom_transaction_id', $remoteDataPay['paymentRefID'] ); $renewal_order->save(); } $renewal_order->payment_complete(); } else { $renewal_order->update_status( 'failed' ); } } public function delete_resubscribe_meta( $resubscribe_order ) { if ( ! $resubscribe_order instanceof WC_Order ) { $resubscribe_order = wc_get_order( $resubscribe_order ); } if ( ! $resubscribe_order ) { return; } $resubscribe_order->delete_meta_data( '_achp_payment_source' ); $resubscribe_order->delete_meta_data( '_achp_account_type' ); $resubscribe_order->delete_meta_data( '_achp_individual_name' ); $resubscribe_order->save(); } } new ACHP_Subscription_Compact();