简体   繁体   中英

PHP Braintree Gateway API

Be patient with me, I am trying to create a Braintree Gateway for my wordpress booking system with no knowledge of php. i have downloaded the php library files from Braintree and ive implemented the area for api connection and creating a transaction using the following code below. This code was taken and tweaked from another gateway! Is this a good method to use? Would this work?

My Code

                        $config = new Braintree\Configuration();
                        $config->environment($api_keys_merchant_id);
                        $config->merchantId(trim($api_keys_merchant_id));
                        $config->publicKey(trim($api_keys_public_key));
                        $config->privateKey(trim($api_keys_private_key));
                        $gateway = new Braintree\Gateway($config);

                        // Create transaction
                        $result = $gateway->transaction()->sale([
                        'amount' => $price,
                        'paymentMethodNonce' => 'nonceFromTheClient',
                              'options' => [ 'submitForSettlement' => true ]]);

                        if ($result->success) {
                              print_r("success!: " . $result->transaction->id);
                       } else if ($result->transaction) {
                              print_r("Error processing transaction:");
                              print_r("\n  code: " . $result->transaction->processorResponseCode);
                              print_r("\n  text: " . $result->transaction->processorResponseText);
                       } else {
                              print_r("Validation errors: \n");
                              print_r($result->errors->deepAll());
                     } ```

Personally, I do not understand what the problem is, so please state it, if any. In terms of adjustments, from the code sample attached, I would say:

  • don't reference unknown variables or share context about them (eg: $parsedCredentials is initialised but not used; also what is $request?)

  • use "use" statements at the top of the file instead of using the FQCN; it makes it easier to understand what the dependencies are and what the code does.

$gateway = new Braintree_Gateway([
    'environment' => $api_keys_merchant_id,
    'merchantId' => trim($api_keys_merchant_id),
    'publicKey' => 'use_your_public_key',
    'privateKey' => 'use_your_private_key'
]);

$result = $gateway->transaction()->sale([
                        'amount' => $price,
                        'paymentMethodNonce' => 'nonceFromTheClient',
                        'options' => [ 'submitForSettlement' => true ]]);

                        if ($result->success) {
                              print_r("success!: " . $result->transaction->id);
                       } else if ($result->transaction) {
                              print_r("Error processing transaction:");
                              print_r("\n  code: " . $result->transaction->processorResponseCode);
                              print_r("\n  text: " . $result->transaction->processorResponseText);
                       } else {
                              print_r("Validation errors: \n");
                              print_r($result->errors->deepAll());
                     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM