簡體   English   中英

Braintree將訂閱從每年更新為每月PHP

[英]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工作。 如果您還有其他疑問,請隨時與支持小組聯系。

計算按比例分配的金額,以用作新的每月訂閱的一次性折扣。

  • 在控制面板中,為每月計划創建一個折扣 將默認金額設置為$ 0,並將計費周期數設置為1。
  • 計算要從原始訂閱價格中扣除的金額作為您的按比例分配金額。 如果您要使用Braintree作為示例,請在此處記錄其按比例分配公式。
  • 使用計算的按比例分配的金額作為折扣的金額來創建訂閱

    $ result = $ gateway-> subscription()-> create(['paymentMethodToken'=>'the_token','planId'=>'silver_plan','discounts'=> [''update'=> ['existingId'=> 'discountId','amount'=>'proratedAmountToDiscount',]
    ]]);

筆記-

  1. 創建訂閱時 ,它將自動繼承與計划相關的所有附加組件和/或折扣。 您可以在創建或更新訂閱時覆蓋這些詳細信息 如果您不需要按比例收費,請不要忽略此折扣,因為默認折扣為$ 0。

  2. 如果折扣金額大於每月費用,則創建的訂閱將具有負余額,將應用於下一個計費周期。 示例 :將創建具有$ 1.50折扣的每月$ 1訂閱,第一個月收取$ 0,下個月余額為-$ 0.50。 下個月的費用為$ 0.50($ 1-$ 0.50)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM