Skip to content

Commit

Permalink
Send emails on various events (closes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Jan 21, 2020
1 parent ec51e74 commit 99c3bfc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
16 changes: 5 additions & 11 deletions Charge/Actions/SendEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ public function __construct()
$this->config = app(Addons::class)->get('charge');
}

/**
* @param $user \Statamic\Data\Users\User
* @param $template string
* @param $data array
*/
public function execute($user, $template, $data)
public function execute(string $email, string $template, array $data)
{
// Config::
Email::to($user->email())
Email::to($email)
->from($this->from())
->in($this->themeFolder())
->template($this->getConfig($template))
->template($this->template($template))
->with($data)
->send();
}
Expand All @@ -42,8 +36,8 @@ private function themeFolder()
return 'site/themes/' . Config::getThemeName() . '/templates';
}

private function template()
private function template($template)
{
return Arr::get($this->config, 'template');
return Arr::get($this->config, $template);
}
}
40 changes: 35 additions & 5 deletions Charge/Traits/HandlesWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function postWebhook(Request $request): Response
});
}

if (!$this->user) {
return $this->successMethod();
}
// if (!$this->user) {
// return $this->successMethod();
// }

$method = 'handle' . Str::studly(str_replace('.', '_', $payload['type']));

Expand All @@ -62,6 +62,17 @@ private function handlePaymentIntentSucceeded($data): Response
$user->save();
}

(new SendEmailAction)->execute(
$data['receipt_email'],
'one_time_payment_email_template',
[
'amount' => Arr::get($data, 'charges.data.0.amount'),
'currency' => Arr::get($data, 'charges.data.0.currency'),
'description' => Arr::get($data, 'charges.data.0.description'),
'receipt_url' => Arr::get($data, 'charges.data.0.receipt_url'),
]
);

return $this->successMethod();
}

Expand All @@ -86,7 +97,7 @@ private function handleCustomerSubscriptionCreated($data): Response
private function handleInvoiceUpcoming($data): Response
{
(new SendEmailAction)->execute(
$this->user,
$this->user->email(),
'upcoming_payment_email_template',
[
'plan' => $this->user->get('plan'),
Expand All @@ -101,14 +112,33 @@ private function handleInvoiceUpcoming($data): Response
return $this->successMethod();
}

private function handleInvoicePaymentSucceeded($data)
{
(new SendEmailAction)->execute(
$this->user->email(),
'payment_succeeded_email_template',
[
'plan' => $this->user->get('plan'),
'first_name' => $this->user->get('first_name'),
'last_name' => $this->user->get('last_name'),
'amount' => $data['amount_due'],
'currency' => $data['currency'],
'attempt_count' => $data['attempt_count'],
'next_payment_attempt' => $data['next_payment_attempt'],
]
);

return $this->successMethod();
}

private function handleInvoicePaymentFailed($data)
{ // @todo should we send an email here?
if ($data['next_payment_attempt']) {
$this->user->set('subscription_status', 'past_due');
$this->user->save();

(new SendEmailAction)->execute(
$this->user,
$this->user->email(),
'payment_failed_email_template',
[
'plan' => $this->user->get('plan'),
Expand Down
34 changes: 28 additions & 6 deletions Charge/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,35 @@ fields:
display: Email Settings
from_email:
type: text
width: 25
canceled_email_template:
width: 33
default: false
one_time_payment_email_template:
type: template
width: 25
payment_failed_email_template:
display: One Time Payment Email
width: 33
show_when:
use_stripe_emails: false
canceled_email_template:
type: template
width: 25
display: Canceled Email
width: 33
show_when:
use_stripe_emails: false
upcoming_payment_email_template:
type: template
width: 25
display: Upcoming Payment Email
width: 33
show_when:
use_stripe_emails: false
payment_succeeded_email_template:
type: template
display: Payment Succeeded Email
width: 33
show_when:
use_stripe_emails: false
payment_failed_email_template:
type: template
display: Payment Failed Email
width: 33
show_when:
use_stripe_emails: false

0 comments on commit 99c3bfc

Please sign in to comment.