mailchimp api delete user from list - mailchimp

when user unsubscribes from list he can't subscribe again with the same email address. How can allow user to resubscribe to list?
$unsubscribe = $mailChimp->call('lists/unsubscribe',array(
'id' => $list_id,
'email' => array('email' => $email),
true,
true
));

If you want to resubscribe the user later, you need to delete him, see the documentation:
When you need to remove a few subscribers, decide whether you want to delete
them yourself or unsubscribe them. Deleted subscribers can be added
back to your list, so if you need to make sure a subscriber isn't
accidentally re-added, unsubscribe them instead.
If you are using the latest mailchimp-api then you can delete the user as follows:
include 'Mailchimp.php';
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('your**api***key');
function deleteUser($email){
global $MailChimp;
//your list_id from Mailchimp
$list_id = 'your***list**id';
$subscriber_hash = $MailChimp->subscriberHash($email);
$MailChimp->delete("lists/$list_id/members/$subscriber_hash");
}
If no user with that email exists, then $MailChimp->delete() will return an array like this:
Array ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Resource Not Found [status] => 404 [detail] => The requested resource could not be found. [instance] => )
If the user was found, then the method will not return anything. Note that this does not imply that the user has been deleted, because if the user has been unsubscribed previously, then its not possible to delete him.
If you do not want to use the api, then you can also write your own custom curl command using the delete verb.

Try deleting the email from list and re-subscribe the same.
You can delete the member by setting delete_member property to true in unsubscribe method

You can do it by updating the user to a status of "pending"
Unfortunately my code is the Python api, but you should be able get the idea.
def mcResendSubscriptionSignup(self,email,audienceId):
# calculate the hash of the email address
emailHash = hashlib.md5(email.lower().encode()).hexdigest()
# get existing user, and all their data
data = self.mcClient.lists.members.get(list_id=audienceId, subscriber_hash=emailHash)
# set the user status to pending to resend subscription email
data['status'] = 'pending'
# update the data back to the user record in the audience
data = self.mcClient.lists.members.update(list_id=audienceId, subscriber_hash=emailHash, data=data)
print(f'Sent a resubscription email to {email}')
This function will resend a confirmation email to the user which he has to click on to resubscribe. Note, you need to also find your audienceId.
This is the only way that Mailchimp will allow you to re-add the user to an audience as of 2020, after an unsubscribe.
Yes, this is a pain when testing, which is why I worked on this function. Tiny bit better than doing it in the GUI interface.

Related

Create new user in database but keep password if it exists in Laravel

I am wondering if the updateOrCreate method in Laravel has a way to skip some information in case the record exists in the database. For example, I wanna be able to search for users, and assign a password if the user is to be created, but not if the user already exists.
Say, I have 3 data for a user, $email, $info and $password. The search criteria would of course be $email, then maybe $info needs to be updated for whatever reason, while $password should only be given if the record is new.
Is there a 'do not update' parameter for updateOrCreate where I can put the password?
User::updateOrCreate(
[
'email' => $email // These parameters are the search criteria, but also used on create
],
[
'data' => $info, // These parameters are the values that will be updated if exist
],
[
'password' => $password, // These parameters are used only in create but are not search criteria
]
);
There are so many questions I would have love to ask to fully understand your organization's process.
1.How did the information get to the db at the first place if the user has not be created?
2. Why can't the password be generated when creating the info into the db at the first place
But I guess all these might bore down to design issue so, I think would say you can create a regular expression that would match your organization document number pattern and use it to check for new user through a loop and the assign password to such ones.

Getting Stripe Upcoming Invoice Line Items with Laravel Cashier

I'm attempting to use Laravel Cashier to retrieve line items of the client's upcoming invoice. I'm having difficulty obtaining the invoice in the first place. Though I've found the undocumented public function "upcomingInvoice," I can't get any of its protected properties out.
Even with it, I'm having trouble understanding how to use the poorly documented "asStripe..." functions, presumably asStripeInovice(), to return the line items.
I've tried a whole host of things and it would muddle things up to write them all out, so I figure it might be better just to ask how to go about it.
Thanks!
Try this one liner code in laravel 7 or 8 with cashier.
$user->upcomingInvoice()
And you're good getting all upcoming invoices details.
As up update, I was able to get this to work just using the Stripe client, like so:
$stripe = new StripeClient(env('STRIPE_SECRET'));
$upcomingLineItems = $stripe->invoices
->upcomingLines(['customer' => $customer['stripe_id']]);
I'll still leave this open in case there's a way to do it with Cashier's methods, but it now doesn't seem necessary.
Similarly, I wanted to be able to display the "amount due today" to a user prior to switching subscription plans (which takes into account proration as well as any credit applied to the customer's account). I was able to do this in Laravel using the following:
$stripe_client = new StripeClient(config('stripe.secret'));
$items = [
[
'id' => $user->subscription()->items()->first()->stripe_id,
'price' => $selected_plan, # Switch to new price
],
];
$invoice = $stripe_client->invoices->upcoming([
'customer' => $user->stripe_id,
'subscription' => $user->subscription()->stripe_id,
'subscription_items' => $items,
'subscription_proration_date' => Carbon::now()->timestamp
]);
$amount_due = $invoice->amount_due; // and there it is

Braintree: What is paymentMethodToken required in creating subscriptions?

https://developer.paypal.com/braintree/docs/guides/recurring-billing/create
result = Braintree_Subscription::create(array(
'paymentMethodToken' => 'the_token',
'planId' => 'silver_plan'
));
Is it a random generated string in my code? Or is it something else?
The paymentMethodToken is a unique identifier for the customer's credit card to use. After you create a Customer with a stored payment method you can use the returned token to subscribe a user to a plan. This article explains it well:
https://developer.paypal.com/braintree/docs/guides/recurring-billing/overview

CakePHP: How can I read in controller the string value of a flash message if set?

I need to know this so I can append messages (flashes) if needed.
This way I can give the user the full feedback and avoid one flash being overwritten (in a redirect, for ex, where the last controller, usualy, can do that).
I read the documentation and I did't find any option to be given in setFlash() in order to require this appending.
I know there is a Session::read(), but I do not know what key to search for..
Thank you!
What you are looking for is:
$this->Session->read('Message');
Message is the key that hold the session messages for the current user, be it flash messages or auth messages. A simple pr($this->Session->read()) will give you output similar to:
Array(
['Auth'] => array(
... your auth keys and values here
),
['Message'] => array(
['flash'] => ... your current flash message array (if any)
['auth'] => ... your current auth message array (if any)
)
)
Although I am not sure why you are worried. When you do
$this->Session->setFlash('your message');
$this->redirect('/');
Even if you do have a redirect, the session message will persist and will display on the redirected page. You just need to make sure you are outputting the flash messages.
The flash message could be retrieved by using this :
$message = $this->Session->read('Message.flash.message');
echo $message;

How to validate variable in Symfony 1.4

I use Symfony 1.4.11. I have two tables "companies" and "ads". When user add new ad, he can connect ad with his company.Before it I check, if user have company, for example I have variable $has_company, if $has_company==1 - user has company, if $has_company==0 he has not company. If user want connect company with ad, he must check checkbox :-) So I want to validate checkbox, If user check checkbox, and he has not company,I want to show messages, that first he must create company.... Is it possible? Can I use sfValidatorBoolean ? If yes, how to validate variable has_company? Thank you!
I think you can create a method in myUser class to check if the current user has a company (if your models user and company are linked).
And then, you can pass the result of this method in option of your form.
For validation, you can use a callback validator : http://www.symfony-project.org/forms/1_4/en/B-Validators#chapter_b_sub_sfvalidatorcallback
you can use halt_on_error option like
$v = new sfValidatorAnd(
array(
new sfValidatorString(array('max_length' => 255)),
new sfValidatorEmail(),
),
array('halt_on_error' => true),
array('invalid' => 'The input value must be an email with less than 255 characters.')
);

Resources