首页 > 解决方案 > Braintree 更新订阅从每年到每月 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);
?>

标签: phpajaxvue.jsupdatesbraintree

解决方案


全面披露:我在布伦特里工作。如果您还有其他问题,请随时联系支持人员

计算按比例分配的金额,用作新月订阅的一次性折扣。

  • 在控制面板中,为月度计划创建折扣。将默认金额设置为 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 美元)。


推荐阅读