Can some one help me to convert this in c# please RRS feed

  • Question

  • User1191534073 posted
    Hi, I have the simple code below here, can anyone help me to write this simple function of php in c#... I will appreciate...

    public function Process()

    $amount = 1;
    $phone = '0720068822';

    $phone2= '254'.substr($phone, 1);
    $url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    $app_consumer_key = 'T3PUFNac2pCObhtQgIStX485Depa4bqR';
    $app_consumer_secret = 'rr3DqquSy3frc3GD';

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$token)); //setting custom header

    $timestamp =date('YmdHis');
    // dd($timestamp);
    $passkey = "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919";
    $short_code = "174379";
    $password = base64_encode($short_code . $passkey . $timestamp);
    $callback_url="enter your callback url here"';
    $curl_post_data = array(
    //Fill in the request parameters with valid values
    "BusinessShortCode" => $short_code,
    "Password" => $password,
    "Timestamp" => $timestamp,
    "TransactionType" => "CustomerPayBillOnline",
    "Amount" => $amount,
    "PartyA" => $phone2,
    "PartyB" => $short_code,
    "PhoneNumber" => $phone2,
    "CallBackURL" => $callback_url,
    "AccountReference" => $account,
    "TransactionDesc" => "Paybill Online"

    $data_string = json_encode($curl_post_data);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

    $curl_response = curl_exec($curl);

    return $merchant_id;

    Thursday, April 5, 2018 6:57 PM

All replies

  • User61956409 posted

    Hi Gich Device,

    Welcome to ASP.NET forums.

    If you’d like to make request in C#, you can use HttpWebRequest class to handle HTTP and HTTPS requests.

    With Regards,

    Fei Han

    Friday, April 6, 2018 5:16 AM
  • User1191534073 posted
    Yea thanks for reply I have tried HttpWebRequest at this point of $curl=curl_init( ) but havent done all...but can you please show me a bit of clues of that code in c# please??
    Friday, April 6, 2018 5:42 AM