Braintree以重力形式订阅

时间:2015-03-02 06:44:56

标签: php wordpress subscription braintree

我正在使用带有Braintree附加组件的Gravity Form,我想要做的是通过信用卡付款添加Subscription。当我去表格 - >设置 - > braintree时,交易类型中只有一个选项 - >产品和服务没有订阅选项,我从这里搜索了这个 - > https://developers.braintreepayments.com/javascript+php/guides/recurring-billing

但它对我不起作用。

请帮我解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分: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@email.com',
    ));

    //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);
 }