Automated Email Responses for programatically added subscriptions

I'm programatically adding subscriptions and adding payments to the invoice when an IPN of txn_type = 'web_accept' is submitted. I add the user, subscription, invoice and payment, however, the Automated Email Responses for that subscription are not sending. What triggers these emails to be sent? I am also wanting to have the "renew" email triggered when I add a payment but I can't find where to trigger these.

  • Adam Czajczyk

    Hello Cagle Webmaster,

    I hope you're well today and thank you for your question!

    If you are using Membership 2 Pro API (described on "Membership 2 Pro -> Help -> API Docs") page those mails should be coming out automatically. If you are however using some hooks only and/or your very custom solution that may be a bit different.

    Would you mind sharing a bit of your code with us or just tell me what methods of API (or what other way, if not API) are you using to add members?

    Best regards,
    Adam

  • Cagle Webmaster

    I intercept the web_accept transaction type by implementing a callback through the 'ms_gateway_handle_payment_return_paypalstandard' hook. I successfully validate the IPN call, create a user and then execute the following function, which handles creating the membership/subscription. This all works fine. However, what's missing is bubbling up the event that sends the automated email responses associated with the subscription creation and/or a subscription renewal.

    This processes one-time donations. It creates a "Donor" account, which we would like to renew each time a donation is made, thus allowing the automated email response to be sent out each time a donation comes in.

    function dcpci_paypal_donor_add_subscription($user_id, $posted ) {
    //-------------------------------------------------------------------
    // ADD DONOR SUBSCRIPTION
    //-------------------------------------------------------------------
    //$grant_membership_id = 704342;
    	$gateway_id = MEMBERSHIP_WEB_ACCEPT_GATEWAY;
    	$membership_id = MEMBERSHIP_DEFAULT_GRANT;
    	$amount = $posted['mc_gross'];
    	$transaction_id = $posted['txn_id'];
    	$checkout_date = $posted['payment_date'];
    	$payer_id = $posted['payer_id'];
    	$expires = date("Y-m-d", strtotime("+30 days"));
    
    	$m2p = ms_api();
    
    	if ( !$m2p->get_subscription( $user_id, $membership_id  ) ) {
    
    		//auto login - required if membership is paid
    		wp_set_auth_cookie( $user_id, false, '' );
    		$subscription = $m2p->add_subscription( $user_id, $membership_id  );
    
    		// add expiration date to user meta data
    		add_user_meta($user_id, PAYPAL_USER_META_PREFIX.'expires', $expires);
    
    	} else {
    		// update and extend the current member's subscription data
    		$subscription = $m2p->get_subscription( $user_id, $membership_id );	
    
    	}	
    
    	//set subscription to active status
    	$status = 'active';
    	$subscription->status = $status;
    	$subscription->save();
    
    	// add a payment record
    	$subscription->add_payment( $amount, $gateway_id, $transaction_id );
    
    	$subscription_id = $subscription->id;
    	$invoice = $subscription->get_current_invoice(false);
    
    	if ( !$invoice ) { $invoice = $subscription->get_previous_invoice(false); } 
    
    	if ( $invoice ) {
    		$invoice_id = $invoice->id;
    
    		//$invoice->pay_it($gateway_id, $transaction_id);
    		$notes = 'PayPal Donation: '.$transaction_id;
    		$invoice->add_notes( $notes );
    
    		//save the changes to the invoice
    		$invoice->amount = $invoice->amount + $amount;
    		$invoice->gateway_id = $gateway_id;
    		$invoice->checkout_date = $checkout_date;
    
    		//new fields to update
    		$invoice->external_id = $transaction_id;
    		$invoice->gateway_id = $gateway_id;
    		$invoice->ms_relationship_id = $subscription_id;
    		$invoice->pay_date = $checkout_date;
    		$invoice->description = 'Single Contribution(s)';
    		$invoice->short_description = $invoice->description;
    
    		//TODO: Verify saving of invoice
    		$invoice->save(); 
    
    		//save all transaction data for payment. Data is currently logged by forward callback to the CagleDB.IPN database.
    		$method = 'handle';
    		$success = true;
    
    		$log = new MS_Controller_Gateway;
    		$log->log_transaction( $gateway_id, $method, $success, $subscription_id, $invoice_id, $amount, $notes, $transaction_id );
    
    	} else {
    		dcpci_ipn_error_notice(null, __FUNCTION__, 'Subscription (user_id: ['.$user_id.']) Unable to add subscription on gateway_id ['.$gateway_id.'] for amount ['.$amount.']: with notes ['.$posted.']');
    	}
    
    	// reset subscription expiration
    	// set expire date after saving active status
    	$subscription->expire_date = $expires;
    	$subscription->save();
    
    	return true;
    
    }

    A bit of a side note, this isn't the baseline service that Membership is designed for but you should consider this as a standard option because it works great for long-term crowd-funding rather than the short-term one-off solutions. It just lacks handling PayPal Donations.

  • Dimitris

    Hey there Cagle Webmaster,

    hope you're doing good and don't mind chiming in here! :slight_smile:

    I appreciate the code you shared, I've already pinged our SLS team about this and me or another colleague of mine will keep you posted here about this as soon as we've got some valuable insights.

    As for the additional communication part, here's a demo implementation made by our colleague Ashok:
    https://gist.github.com/bappi-d-great/b21d8837854f32dd1d8567e59c38ed58

    Hope that was some help, let me know if you need anything else until we've got some feedback from devs.

    Warm regards,
    Dimitris

  • Dimitris

    Hey there Cagle Webmaster,

    hope you're doing good! :slight_smile:

    I just had some feedback from our SLS team and seems that emails should auto-trigger via cron job
    'ms_cron_process_communications' => 'hourly'
    so you shouldn't do anything more, rather than setting the actual emails of course.

    This also reminded me a similar issue for some members/cases, could you please try the hotfix I shared in my reply in this thread here:
    https://premium.wpmudev.org/forums/topic/membership-2-pro-wont-sent-auto-emails#post-1137089
    and let me know if that makes any difference in your end.

    Warm regards,
    Dimitris

  • Lindeni Mahlalela

    Hi Cagle Webmaster,

    I hope you are doing great today. As I was going through the forum I noticed that this thread is still open and that we haven't had a response from you since my colleague Dimitris suggested a fix for you to try. I was wondering if you had a chance to try that fix or not. Please let us know whether the fix worked for you or not and whether you still need help with this issue or not.

    We will be happy to hear from you and we will be happy to help in any way possible should you still need help.

    I hope you enjoy your day further.

    Cheers,
    Mahlamusa

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.