2015-03-02 60 views
-1

我使用Gravity Form和Braintree插件,我想要做的是通過信用卡支付添加訂閱。當我去形式 - > setttings-> braintree只有一個選項在交易類型 - >產品和服務沒有選項的訂閱,我從這裏搜索 - >https://developers.braintreepayments.com/javascript+php/guides/recurring-billingBraintree以重力方式訂閱

但它不適用於我。

請幫我解決這個問題。

謝謝!

+0

如果您正在使用重力形式與開發插件,我建議您提交票給他們,他們的支持非常出色。然後回來並添加你自己的答案:) – McNab 2015-03-02 10:02:33

回答

1

經過很長時間的搜索,我發現了一些很棒的東西。 就打電話給你的環境,商家,公鑰和私鑰的functions.php

Braintree_Configuration::environment($transaction); //Sandbox OR Products 
Braintree_Configuration::merchantId($merchant_key); //Your Braintree Merchant Key 
Braintree_Configuration::publicKey($public_key); //Your Braintree Public Key 
Braintree_Configuration::privateKey($private_key); //Your Braintree Private Key 

add_action("gform_after_submission", "after_submission", 10, 2); 

function after_submission($entry, $form) 
{ 
    //Create Customer 
    $result = Braintree_Customer::create(array(
     'firstName' => 'First Name', 
     'lastName' => 'Last Name', 
     'company' => 'Company Name', 
     'email' => '[email protected]', 
    )); 

    //Get Current Customer ID 
    $customer_id = $result->customer->id; 

    //Add Customer Credit Card Info to Braintree Subscription 
    $result = Braintree_CreditCard::create(array(
     'customerId' => $customer_id, 
     'number' => '4111111111111111', 
     'expirationDate' => '05/20', 
     'cardholderName' => 'Mani' 
    )); 

    try { 

     $customer = Braintree_Customer::find($customer_id); 
     $payment_method_token = $customer->creditCards[0]->token; 

     //You can add Subscription Package From Braintree Account 
     $result = Braintree_Subscription::create(array(
      'paymentMethodToken' => $payment_method_token, 
      'planId' => 'Your_subscription_name_here', 
      'price' => '1000' 
     )); 

     if ($result->success) { 
      echo("Success! Subscription " . $result->subscription->id . " is " . $result->subscription->status); 
     } else { 
      echo("Validation errors:"); 
      foreach (($result->errors->deepAll()) as $error) { 
       echo("- " . $error->message); 
      } 
     } 
    } catch (Braintree_Exception_NotFound $e) { 
    echo("Failure: no customer found with ID " . $customer_id); 
}