【问题标题】:How to integrate paytm in android using php?如何使用 php 在 android 中集成 paytm?
【发布时间】:2018-10-26 07:24:28
【问题描述】:

我曾尝试使用 php 集成 paytm。我按照这个 url 来实施 paytm https://www.simplifiedcoding.net/paytm-integration-android-example/。 然后下载他们的示例代码进行尝试。更改了所有凭据,如 MID、回调 url 和频道 ID 等。在设备上运行时,点击购买按钮时会打开购买屏幕,加载并显示 oops “糟糕!付款失败”

在 android 控制台中,我可以获取订单 ID。这是constants.java的代码

 package simplifiedcoding.net.paytmpaymentsample;

/**
* Created by Belal on 1/10/2018.
*/

public class Constants {

public static final String M_ID = "xxxxx301208461"; //Paytm Merchand Id 
we got it in paytm credentials
public static final String CHANNEL_ID = "WAP"; //Paytm Channel Id, got it in 
paytm credentials
public static final String INDUSTRY_TYPE_ID = "Retail"; //Paytm industry type 
got it in paytm credential

public static final String WEBSITE = "APP_STAGING";
public static final String CALLBACK_URL = 
"https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp";


}

这里是代码 API.java

   package simplifiedcoding.net.paytmpaymentsample;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

 /**
 * Created by Belal on 1/10/2018.
 */

 public interface Api {

    //this is the URL of the paytm folder that we added in the server
  //make sure you are using your ip else it will not work
  String BASE_URL = "http://10.208.1.229/paytm/";

   @FormUrlEncoded
   @POST("generateChecksum.php")
   Call<Checksum> getChecksum(
        @Field("MID") String mId,
        @Field("ORDER_ID") String orderId,
        @Field("CUST_ID") String custId,
        @Field("CHANNEL_ID") String channelId,
        @Field("TXN_AMOUNT") String txnAmount,
        @Field("WEBSITE") String website,
        @Field("CALLBACK_URL") String callbackUrl,
        @Field("INDUSTRY_TYPE_ID") String industryTypeId
   );

  }

MainActivity.java 代码

    package simplifiedcoding.net.paytmpaymentsample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.paytm.pgsdk.PaytmOrder;
 import com.paytm.pgsdk.PaytmPGService;
 import com.paytm.pgsdk.PaytmPaymentTransactionCallback;

import java.util.HashMap;
 import java.util.Map;

 import retrofit2.Call;
import retrofit2.Callback;
 import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//implementing PaytmPaymentTransactionCallback to track the payment result.
 public class MainActivity extends AppCompatActivity implements 
 PaytmPaymentTransactionCallback {

//the textview in the interface where we have the price
TextView textViewPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //getting the textview
    textViewPrice = findViewById(R.id.textViewPrice);


    //attaching a click listener to the button buy
    findViewById(R.id.buttonBuy).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //calling the method generateCheckSum() which will generate the paytm checksum for payment
            generateCheckSum();
        }
    });

}

private void generateCheckSum() {

    //getting the tax amount first.
    String txnAmount = textViewPrice.getText().toString().trim();

    //creating a retrofit object.
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    //creating the retrofit api service
    Api apiService = retrofit.create(Api.class);

    //creating paytm object
    //containing all the values required
    final Paytm paytm = new Paytm(
            Constants.M_ID,
            Constants.CHANNEL_ID,
            txnAmount,
            Constants.WEBSITE,
            Constants.CALLBACK_URL,
            Constants.INDUSTRY_TYPE_ID
    );

    //creating a call object from the apiService
    Call<Checksum> call = apiService.getChecksum(
            paytm.getmId(),
            paytm.getOrderId(),
            paytm.getCustId(),
            paytm.getChannelId(),
            paytm.getTxnAmount(),
            paytm.getWebsite(),
            paytm.getCallBackUrl(),
            paytm.getIndustryTypeId()
    );

    //making the call to generate checksum
    call.enqueue(new Callback<Checksum>() {
        @Override
        public void onResponse(Call<Checksum> call, Response<Checksum> response) {

            //once we get the checksum we will initiailize the payment.
            //the method is taking the checksum we got and the paytm object as the parameter
            initializePaytmPayment(response.body().getChecksumHash(), paytm);
        }

        @Override
        public void onFailure(Call<Checksum> call, Throwable t) {

        }
    });
}

private void initializePaytmPayment(String checksumHash, Paytm paytm) {

    //getting paytm service
    PaytmPGService Service = PaytmPGService.getStagingService();

    //use this when using for production
    //PaytmPGService Service = PaytmPGService.getProductionService();

    //creating a hashmap and adding all the values required
    Map<String, String> paramMap = new HashMap<>();
    paramMap.put("MID", Constants.M_ID);
    paramMap.put("ORDER_ID", paytm.getOrderId());
    paramMap.put("CUST_ID", paytm.getCustId());
    paramMap.put("CHANNEL_ID", paytm.getChannelId());
    paramMap.put("TXN_AMOUNT", paytm.getTxnAmount());
    paramMap.put("WEBSITE", paytm.getWebsite());
    paramMap.put("CALLBACK_URL", paytm.getCallBackUrl());
    paramMap.put("CHECKSUMHASH", checksumHash);
    paramMap.put("INDUSTRY_TYPE_ID", paytm.getIndustryTypeId());


    //creating a paytm order object using the hashmap
    PaytmOrder order = new PaytmOrder(paramMap);

    //intializing the paytm service
    Service.initialize(order, null);

    //finally starting the payment transaction
    Service.startPaymentTransaction(this, true, true, this);

}

//all these overriden method is to detect the payment result accordingly
@Override
public void onTransactionResponse(Bundle bundle) {

    Toast.makeText(this, bundle.toString(), Toast.LENGTH_LONG).show();
}

@Override
public void networkNotAvailable() {
    Toast.makeText(this, "Network error", Toast.LENGTH_LONG).show();
}

@Override
public void clientAuthenticationFailed(String s) {
    Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}

@Override
public void someUIErrorOccurred(String s) {
    Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}

@Override
public void onErrorLoadingWebPage(int i, String s, String s1) {
    Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}

  @Override
  public void onBackPressedCancelTransaction() {
      Toast.makeText(this, "Back Pressed", Toast.LENGTH_LONG).show();
  }

   @Override
  public void onTransactionCancel(String s, Bundle bundle) {
    Toast.makeText(this, s + bundle.toString(), Toast.LENGTH_LONG).show();
  }
 }

【问题讨论】:

  • 将 WEBSITE = "APP_STAGING" 更改为 "APPSTAGING"

标签: java android android-studio payment-gateway paytm


【解决方案1】:

对于 paytm 集成,您应该遵循 4 个步骤。 Paytm 官方文档中提供了一些误导性信息,但您可以关注this reference tutorial for lastest Paytm SDK integration 2019

  1. 添加 Paytm 依赖项
  2. 添加运行时权限和 Paytm 活动
  3. 获取商户 ID 和密钥
  4. 在服务器上上传校验和文件
  5. 生成 Paytm 校验和
  6. 开始 Paytm 支付交易

添加支付宝依赖

   // paytm dependency
implementation('com.paytm:pgplussdk:1.2.3') {
   transitive = true;
}

添加运行时权限

<uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.READ_SMS"/>
  <uses-permission android:name="android.permission.RECEIVE_SMS"/>

在主Activity类中也加入这一行

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS}, 101);
      }

生成 Paytm 校验和,您首先需要知道订单 ID 在每笔交易中都必须是唯一的,即使在测试模型交易中也是如此。

  1. 订单 ID 和客户 ID 只能从您的服务器生成。
  2. 对于测试模型,您可以手动提供订单 ID 和客户 ID。

android代码流程如下-

Main Activity – 从 App UI 中获取订单 ID 和客户 ID。

将订单 ID 和客户 ID 作为 checksum.java 传递给另一个活动名称

通过传递参数从 URL 获取校验和。

  String url ="https://www.blueappsoftware.com/payment/payment_paytm/generateChecksum.php";
        String varifyurl = "https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp";
                           // "https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID"+orderId;

 String param=
                "MID="+mid+
                "&ORDER_ID=" + orderId+
                "&CUST_ID="+custid+
                "&CHANNEL_ID=WAP&TXN_AMOUNT=100&WEBSITE=WEBSTAGING"+
                        "&CALLBACK_URL="+ varifyurl+"&INDUSTRY_TYPE_ID=Retail";

通过调用

开始支付交易
 Service.startPaymentTransaction(checksum.this, true, true,
                    checksum.this  );

请阅读this reference tutorial for complete process

【讨论】:

    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 2016-01-27
    • 2020-06-22
    • 2016-07-07
    • 2019-07-26
    相关资源
    最近更新 更多