![](/img/trans.png)
[英]How to displays item names from the database using a monthly or yearly chart PHP MySQL
[英]Braintree update Subscription from yearly to monthly PHP
我有一個Vue應用程序,該應用程序對此頁面進行了ajax調用,以“更新”我說的更新訂閱,但是我實際上正在做的是取消當前訂閱並添加另一個訂閱。 我會使用內置的更新功能,但是根據Braintree的文檔,它當前不支持按比例分配具有不同計費周期的訂閱。 例如,如果要將每年重復出現的訂閱更新為每月重復出現的訂閱,反之亦然,則不能使用braintree :: update()。 該代碼當前有效,但我想按比例分配。 我該如何假冒,以便可以按比例分配這些類型的訂閱。 這是我目前擁有的代碼,有人可以給我發送代碼段嗎?
<?php session_start(); ob_start();
$id=$_SESSION['braintreeid'];
$receiver='creativegroup@codernoob.com';
$username=$_SESSION['username'];
$email = $_SESSION['email'];
require 'PHPMailer/PHPMailerAutoload.php';
date_default_timezone_set('America/Denver');
$request_body = file_get_contents('php://input');
$json = json_decode($request_body);
$oldsubscriptionid=$json->oldsubscriptionid;
require_once 'lib/Braintree.php';
$gateway = new Braintree_Gateway([
'environment' => 'sandbox',
'merchantId' => '********',
'publicKey' => '*******',
'privateKey' => '********'
]);
$cancelresult=$gateway->subscription()->cancel($oldsubscriptionid);
$result = $gateway->subscription()->create([
'paymentMethodToken' => $json->token,
'planId' => $json->plan
]);
if($result){$data['error']=0;
$data['problemsending']=0;
$data['emailsent']=0;
$data['result']=$result;
} else {$data['error']=1;
$data['result']=$result;
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'donotreply@codernoob.com'; // SMTP username
$mail->Password = '*********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('donotreply@codernoob.com');
$mail->addAddress($email); // Add a recipient
$mail->addAddress($receiver); // Name is optional
$mail->addBCC(''); //WARNING: SMTP servers will cap 'potential' spammers to no more than 100 recipients per blast, and no more than 500 per day. If you need more than this get a professional SMTP server.
$mail->addAttachment(''); // Add attachments
$mail->addAttachment(''); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'codernoob: ERROR Updating Subscription!';
$body = "
<html>
<head>
<title>Error updating Subscription!</title>
</head>
<body>
<h1>There was an error updating subscription</h1>
<p>Username: ".$username."</p>
<p>Email: ".$email." </p>";
$body .= '<br><br>Had an error updating a subscription. If this is the first time you have seen this ignore it as a packet probably just dropped. However, if problem persists please have a web developer address the problem.';
$body .= "</body>
</html>
";
$mail->Body = $body;
if($mail->send()) {$data['emailsent']=1;}
else {$data['problemsending']=1;}
}
$customer = $gateway->customer()->find($id);
$plans = $gateway->plan()->all();
$plansArray = array();
foreach ($plans as $plan) {
array_push($plansArray, $plan);
}
$subscriptions = array();
foreach ($customer->creditCards as $card) {
foreach ($card->subscriptions as $subscription) {
array_push($subscriptions, $subscription);
}
}
$data['paymentMethods']=$customer->paymentMethods;
$data['plans']=$plansArray;
$data['subscriptions']=$subscriptions;
echo json_encode($data);
?>
全面披露:我在Braintree工作。 如果您還有其他疑問,請隨時與支持小組聯系。
計算按比例分配的金額,以用作新的每月訂閱的一次性折扣。
使用計算的按比例分配的金額作為折扣的金額來創建訂閱 :
$ result = $ gateway-> subscription()-> create(['paymentMethodToken'=>'the_token','planId'=>'silver_plan','discounts'=> [''update'=> ['existingId'=> 'discountId','amount'=>'proratedAmountToDiscount',]
]]);
筆記-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.