Sending Money with Dwolla's API and using PHP to do it?

1.9k views Asked by At

Hello everyone I'm back again, In my last post I was attempting to use the SOAP api (Integrating Dwolla with PHP with their API) but I found out the SOAP API is deprecated and apparently Dwolla has more efficient way such as the REST/oAuth2.0 which is why I'm here today asking how to use the rest API as its been almost 2 weeks and I'd really like to learn this.

First off I'll say that I've successfully been able to get an access_token I have no problem doing that. The issue is that when I try to use an a Send Endpoint(https://www.dwolla.com/developers/endpoints/accountapi/send) basically trying to send money to and account. My exact issue is that I can never get a successful response; only false or error message responses.

So on the index page I have "Add funds to your account" link. Users will click that link and it will take them to the Dwolla Page that will accept them to Sign in to their Dwolla account an then accept the permissions the website is asking for. After the user presses "Accept" it will redirect to the selected URL that I chose and send back an access_token to use for authorization purposes. Here is my code (This is the page that Dwolla redirects too and sends the access_token too)

<?php
//Define variables
    $key            = 'redacted';
    $secret         = 'redacted';
    $dwolla_client_id   = urlencode($key);
    $dwolla_secret_key  = urlencode($secret);
$code = urlencode($_GET["code"]);
//get token
    $retireve_token = file_get_contents("https://www.dwolla.com/oauth/v2/token?client_id=".$dwolla_client_id."&client_secret=".$dwolla_secret_key."&grant_type=authorization_code&redirect_uri=http://localhost/purchase_order.php&code=".$code);


    $decoded_json = json_decode($retireve_token, true);


        var_dump($decoded_json);
        if($decoded_json["access_token"]){
                    $arr = '{
                            "oauth_token": "'.$decoded_json["access_token"].'",
                            "fundsSource": "balance",
                            "pin": "1111",
                            "notes": "Payment for services rendered",
                            "amount": 1.01,
                            "destinationId": "812-111-1111",
                            "assumeCosts": false,
                            "facilitatorAmount": 0,
                            "destinationType": "dwolla"
                    }';
                    $opts = array('http'=>array('method'=>"POST",'content'=> $arr, 'header' => 'Content-Type: application/json'));

                    $ctx = stream_context_create($opts);
            $send_request = file_get_contents('https://www.dwolla.com/oauth/rest/accountapi/send', false, $ctx);

            var_dump(json_decode($send_request));
        }

?>

I receive messages like this for example

array(1) { ["access_token"]=> string(50) "redacted" } Warning: file_get_contents(https://www.dwolla.com/oauth/rest/accountapi/send): failed to open stream: HTTP request failed! HTTP/1.1 503 Service Unavailable in /home/swiftbitcoins/purchase_order.php on line 47 NULL

1

There are 1 answers

0
akmsharma On

what you are trying to make is a get request whereas Dwolla documentation refers to this as post request.

better you can do is user their php Library with built in methods to make calls. this is a standard library for making restful calls and much better than writing the way you have written in the code snippet above.

https://github.com/Dwolla/dwolla-php