如何将Chargebee API与Wordpress集成?

时间:2019-08-24 13:35:32

标签: wordpress api

我正在尝试将Chargebee API集成到wordpress中。

我希望用户使用表单注册到我的网站,并且当他们单击“提交”时,表单数据将通过Chargebee API提交,并且用户将订阅计划。

我尝试了

这是我的脚本代码

   $("#creditrepair-form").submit(function (e) {
    e.preventDefault(); 

    var formData = $(this).serialize();
    var ajaxurl = $(this).data('url'); 

    $.ajax({  
        type:'POST',
        url: ajaxurl,
        dataType: "json",
        data:{
            mydata: formData,
            action: 'enrollment_submit'},
        cache: false,
        success: function(result){
              alert(result);
        }
    });

以及在我的functions.php

函数enrollment_submit_ajax_data(){

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    require 'includes/lib/Chargebee-api/ChargeBee.php';
    $name = $_POST['mydata'];
    parse_str($name, $output);

    ChargeBee_Environment::configure("samplesub- 
    test","test_8lw9IR5iBOlycucuy5SUWTvAz7Z7jeBoaI");
    $result = ChargeBee_Subscription::create(array(
      "planId" => $output['plan_id'],
      "autoCollection" => "off",
      "billingAddress" => array(
        "firstName" => $output['fname'],
        "lastName" => $output['lname'],
        "line1" => $output[line1''],
        "city" => $output['city'],
        "state" => $output['state'],
        "zip" => $output['zip'],
        "country" => $output['country']
        ),
      "customer" => array(
        "firstName" => $output['fname'],
        "lastName" => $output['lname'],
        "email" => $output['email']
        )
      ));
    $subscription = $result->subscription();
    $customer = $result->customer();
    $card = $result->card();
    $invoice = $result->invoice();
    $unbilledCharges = $result->unbilledCharges();   
}
die();
    }
    add_action( 'wp_ajax_enrollment_submit', 'enrollment_submit_ajax_data' );
    add_action( 'wp_ajax_nopriv_enrollment_submit', 'enrollment_submit_ajax_data' );

0 个答案:

没有答案