2016-05-08 5 views
1

私はPHPには新しく、支払いが成功したらメールを送る方法を工夫しています。それについてどうすればいいのですか?スクリプトの半分が見える場合は、成功したStripe Payment Callを定義する 'if'ステートメントがあります。それが電子メールを送信するべきポイントです。このような - このPHPスクリプトから電子メールをトリガーするにはどうすればよいですか?

は、私はPOSTをEメールで送信要求が含まれていますか?

($_POST['email'])) { 

$email_to = "[email protected]"; 
$email_subject = "Email subject line"; 

そしてここで、現在動作しますPHPスクリプトのだが - あなたはスクリプトthrought声明「IF」半分が表示されます。お支払いは、あなたが機能を含めることができるだけでユーザに通知する前に、成功しているスクリプトに続いて

function sendEmail() { 
    $email_to = "[email protected]"; 
    $email_subject = "Email subject line"; 
    ... ETC ... 
} 

<?php 
require('config.inc.php'); 
session_start(); 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
$email=$_POST['email']; 
// Stores errors: 
$errors = array(); 
// Need a payment token: 
if (isset($_POST['stripeToken'])) { 

$token = $_POST['stripeToken']; 

// Check for a duplicate submission, just in case: 
// Uses sessions, you could use a cookie instead. 
if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) { 
    $errors['token'] = 'You have apparently resubmitted the form.'; 
} else { // New submission. 
    $_SESSION['token'] = $token; 
} 

} else { 
$errors['token'] = 'Your subscription cannot be processed because you must have JavaScript enabled. Please try again.'; 
} 

// Set the order amount somehow: 
$amount = 2700; // $20, in cents 

// Validate other form data! 

// If no errors, process the order: 
if (empty($errors)) { 

// create the charge on Stripe's servers - this will charge the user's card 
try { 

    // Include the Stripe library: 
    require_once('lib/Stripe.php'); 

    // set your secret key: remember to change this to your live secret key in production 
    // see your keys here https://manage.stripe.com/account 
    Stripe::setApiKey(STRIPE_PRIVATE_KEY); 
    // Charge the order: 
    $charge=Stripe_Customer::create(array(
    "card"=>$token, 
    "email" => $email, 
    "plan" =>"newsletter", 
)); 
    // Check that it was paid: 
    if (!empty($charge)) { 
    //echo $amount; 
    // Store the order in the database. 
    // Send the email. 
    // Celebrate! 
    /*$cookie_name = "success_msg"; 
    $cookie_value = "Your Payment is successfully done"; 
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");*/ 
    $_SESSION['success_msg']="Your subcription was successfull - thank you!<br><br>We will send you further details shortly on how to access your account."; 
    echo "<script>window.location.href='index.php'</script>"; 
    exit(0); 

    } else { // Charge was not paid! 

    echo '<div class="alert alert-error"><h4>Payment System Error!</h4>Your payment could NOT be processed (i.e., you have not been charged) because the payment system rejected the transaction. You can try again or use another card.</div>'; 
    } 

} catch (Stripe_CardError $e) { 
    // Card was declined. 
    $e_json = $e->getJsonBody(); 
    $err = $e_json['error']; 
    $errors['stripe'] = $err['message']; 
} catch (Stripe_ApiConnectionError $e) { 
    // Network problem, perhaps try again. 
} catch (Stripe_InvalidRequestError $e) { 
    // You screwed up in your programming. Shouldn't happen! 
} catch (Stripe_ApiError $e) { 
    // Stripe's servers are down! 
} catch (Stripe_CardError $e) { 
    // Something else that's not the customer's fault. 
} 

} // A user form submission error occurred, handled below. 

} // Form submission. 
?> 

は、あなたが電子メールを送信する機能を作成することができ

+0

を入れたらif(!empty($ charge)){支払いが成功したというメールを送るなど。 – Poria

答えて

0

をasdasd:

... 
if (!empty($charge)) { 
    ... 

    // Include your function if you wrote this to an external script 
    require_once '/path/to/php/sendEmail.function.php'; 
    // Then make use of it: 
    sendEmail(); 

    /*$cookie_name = "success_msg"; 
    $cookie_value = "Your Payment is successfully done"; 
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");*/ 
    $_SESSION['success_msg']="Your subcription was successfull - thank you!<br><br>We will send you further details shortly on how to access your account."; 

    // Redirect the user to a new page 
    echo "<script>window.location.href='index.php'</script>"; 

    // Then you can exit runtime 
    exit(0); 
} 
// Charge was not paid! 
else { 
    echo '<div class="alert alert-error"><h4>Payment System Error!</h4>Your payment could NOT be processed (i.e., you have not been charged) because the payment system rejected the transaction. You can try again or use another card.</div>'; 
} 
... 

メール機能がメールを送信するかilとするか、失敗した電子メールがユーザーエクスペリエンスを妨害しないように通常どおり続行します。あなたはフォールバックを持つことができるので、もし電子メールがそれをデータベースに書き込んだり送信しても、このプロセス中にエラーで終了することはありません。

関連する問題