Stripe:在收费期间检索客户的电子邮件地址 (Laravel 5.2)

Stripe: Retrieving a customers email address during a charge (Laravel 5.2)

我希望在客户购买产品后检索他们的电子邮件地址,以便我可以向他们发送下载 link。

这是我对收费的处理。

 public function charge()
{
    \Stripe\Stripe::setApiKey("sk_test_key");

    $token = $_POST['stripeToken'];

    dd(\Stripe\Customer::retrieve($token));

    try {
      $charge = \Stripe\Charge::create(array(
        "amount" => 10000, // amount in cents, again
        "currency" => "usd",
        "source" => $token,
        "description" => "Example charge"
        ));
    } catch(\Stripe\Error\Card $e) {
        flashWarning('An error occured');
        return back();
    }

    $data = [];

    Mail::send('emails.download',$data, function($message)
    {
        $message->to(CUSTOMER EMAIL)->subject('thank you for purchasing...');
    });  

}

在方法的下半部分,我想以某种方式找到客户的电子邮件地址,以便我可以向他们发送电子邮件。

编辑:客户不是用户。

使用 Auth 检索用户的详细信息或电子邮件,然后使用 "use" 方法

传递给电子邮件
// get email first. depends on how you store customer or user email
$email = \Auth::user()->email;

Mail::send('emails.download',$data, function($message) use ($email)
{
    $message->to($email)->subject('thank you for purchasing...');
});

您刚刚访问 Customer 成员的 email 属性:

$customer = \Stripe\Customer::retrieve($token);

Mail::send('emails.download',$data, function($message) use ($customer)
{
    $message->to($customer->email)->subject('thank you for purchasing...');
});

旁注

您可以使用 Stripe\Error\Base $e 捕获所有 Stripe 异常,因此您可以正确地 return 一条错误消息。试一试:

$errors = collect([]);
try {
    //...
} catch (Stripe\Error\Base $e) {
    $errors->push($e->getMessage());
} catch (Exception $e) {
    $errors->push($e->getMessage());
}

if ($errors->count() > 0) {
    return back()->withErrors(['message' => implode('. ', $errors->toArray()]);
}